How do I close a frame with a button?
I built a GUI that creates a frame with a bunch of buttons, lists, etc. I have all of the components (buttons, lists, comboboxes, etc) working correctly with action listeners. I'm actually only having an issue with the Exit button. I want to have an Exit button that has the same effect as closing the program by hitting the X button on the frame in the top right corner.
I've been able to get sub-frames to close with action listeners, but since the main frame is static... how do I close the frame from another action listener?
I also would like to make the main frame disabled when another window opens, but this brings me back to the same problem.
Code:
public class DungeonEditor extends JPanel {
// declarations
public DungeonEditor () throws IOException, BiffException, WriteException {
super(new BorderLayout());
// constructor
public static void buildGUI() throws IOException, BiffException, WriteException {
JFrame frame = new JFrame("Dungeon Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1120, 700));
JComponent newContentPane = new DungeonEditor();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
buildGUI();
} catch (IOException ex) {
Logger.getLogger(DungeonEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (BiffException ex) {
Logger.getLogger(DungeonEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (WriteException ex) {
Logger.getLogger(DungeonEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}