So come gestire gli eventi con bottoni e simili. Mi serve sapere come faccio ad avere un riferimento al pulsante in alto a destra (quello con la crocetta rossa) di un JFrame.
So come gestire gli eventi con bottoni e simili. Mi serve sapere come faccio ad avere un riferimento al pulsante in alto a destra (quello con la crocetta rossa) di un JFrame.
Che cosa devi fare? :master:
Ciao.![]()
Volevo aggiungere un messaggio tipo JOptionPane.showConfirmDialog che chiedesse la conferma di voler chiudere il frame.
Beh puoi farlo mettendo una WindowListener al JFrame...guarda la documentazione di questa classe.
Ciao.![]()
Ecco un esempio:
codice:import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import javax.swing.*; public class FrameTest extends JFrame { public FrameTest () { super ("Frame Test"); setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE); setSize (300, 300); addWindowListener (new CloseWarning ()); } public static void main (String[] args) { FrameTest f = new FrameTest (); f.setVisible (true); } class CloseWarning extends WindowAdapter { public void windowClosing (WindowEvent e) { int ret = JOptionPane.showConfirmDialog (FrameTest.this, "Chiudere la finestra?", "Frame Test", JOptionPane.YES_NO_OPTION); if (ret == JOptionPane.YES_OPTION) System.exit (0); } } }
Andrea, andbin.dev – Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet