Button for closing a JDialog

后端 未结 5 873
[愿得一人]
[愿得一人] 2020-12-18 17:59

I want to add a button (JButton) at the bottom of a JDialog which should close the JDialog when pressed. The problem is I don\'t know what to write in the ActionListener of

5条回答
  •  北海茫月
    2020-12-18 18:38

    import java.awt.event.*;
    import javax.swing.*;
    
    public class YourDialog extends JDialog implements ActionListener {
    
      JButton button;
    
      public YourDialog() {
         button = new JButton("Close");
         button.addActionListener(this);
         add(button);
         pack();
         setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e) {
          dispose();
      }
    }
    
    • close only dialolg using dispose() method parent frame not closed. reason that JVM not terminated.

提交回复
热议问题