开发者

closing a JFrame with a button without terminating the program

开发者 https://www.devze.com 2023-02-28 18:50 出处:网络
There are two frames and when you click on a button on the first frame it opens the second frame. On the second frame I\'m trying to create a button which when pressed closes the JFrame without termin

There are two frames and when you click on a button on the first frame it opens the second frame. On the second frame I'm trying to create a button which when pressed closes the JFrame without terminating the program but I'm having no luck. This is the code that I'm trying to use for the second frame, which without the button compiles fine:

class Time_First_Depot extends JFrame
{   
    Time_First_Depot()
    {
        Contain开发者_运维技巧er c = getContentPane(); \\ creates content pane
        c.setLayout ( null );

        Color b = new Color(100,200,255); \\ set colour of JFrame 
        c.setBackground( b );

        JButton exit = new JButton("EXIT"); \\creats button
        exit.addActionListener(new ExitButtonListener()); \\ adds listener to button

        exit.setForeground(Color.BLUE); \\edits buton
        exit.setFont(new Font("Time", Font.BOLD, 12));

        c.add(exit);\\adds button

        exit.setBounds(250, 375, 90, 30);\\ sets location of button

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450);  // set position and size
        this.setResizable(false);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        this.setTitle("Time");
        this.setVisible(true);
        this.setResizable(false);
    }
}

class ExitButtonListener implements ActionListener
{
    ExitButtonListener(){}
            public void actionPerformed(ActionEvent e)
            {
                    if (e.getActionCommand().equals("EXIT"))
                    {

                            dispose();
                    }
            }

}

While compiling I get the following error message:

cannot find symbol
symbol  : method dispose()
location: class ExitButtonListener
                            dispose();
                            ^

(Note: I've removed bits or irrelevant code that has nothing to do with the question.)

Thanks in advance for help anybody can give me.


If you want to dispose the Time_First_Depot frame, then you have to call dispose() on the actual active instance of that class; so the event handler needs access to that instance. There are all kinds of ways you could do this. One way: pass "this" to the ExitButtonListener's constructor; have the constructor accept a JFrame argument and store it in a member variable, then call dispose() through that member.


The method dispose is a method of your JFrame. Therefore I suggest to pass over a reference to your ActionListener.

class ExitButtonListener implements ActionListener {
    private final JFrame frame;

    ExitButtonListener(JFrame frame) {
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("EXIT")) {
            frame.dispose();
        }
    }
}


There are two frames and when you click on a button on the first frame it opens the second frame. On the second frame I'm trying to create a button which when pressed closes the JFrame without terminating ..

If you can swap the 2nd JFrame for a JOptionPane (or modal JDialog) most of the problems will be solved.


You should consider triggering the DefaultCloseOperation that would do nothing else than trigger the same process as if you were pressign the red x in the top right corner of your JFrame.

The disposal would then be done by the JFrame itself or rather the already specified:

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

You can accomplish that, as outlined in this question's answers in multiple ways.

Possiblity one: You enqueue a WindowEvent, thereby faking a CloseOperation:

public void actionPerformed (Event e) {
    WindowEvent wev = new WindowEvent(
         SwingUtilities.windowForComponent((Component) e.getSource()),
         WindowEvent.WINDOW_CLOSING
    );
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}

Possibility two: You dispose of the Window, without enqueueing an event, but thereby skip all EventListeners you specified:

public void actionPerformed (Event e) {
     JFrame rootWindow = ((JFrame) SwingUtilities.getRoot((Component) e.getSource()));
     rootWindow.dispose();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消