使用按钮关闭 JFrame 而不终止程序
有两个框架,当您单击第一个框架上的按钮时,它会打开第二个框架。在第二帧上,我试图创建一个按钮,按下该按钮会关闭 JFrame 而不终止程序,但我没有运气。这是我尝试用于第二帧的代码,如果没有按钮,它可以正常编译:
class Time_First_Depot extends JFrame
{
Time_First_Depot()
{
Container 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();
}
}
}
编译时,我收到以下错误消息:(
cannot find symbol
symbol : method dispose()
location: class ExitButtonListener
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 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()
{
Container 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想处置 Time_First_Depot 框架,那么你必须在该类的实际活动实例上调用 dispose() ;因此事件处理程序需要访问该实例。有多种方法可以做到这一点。一种方法:将“this”传递给 ExitButtonListener 的构造函数;让构造函数接受 JFrame 参数并将其存储在成员变量中,然后通过该成员调用 dispose()。
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.
dispose
方法是JFrame
的一个方法。因此,我建议传递对ActionListener
的引用。The method
dispose
is a method of yourJFrame
. Therefore I suggest to pass over a reference to yourActionListener
.如果您可以将第二个
JFrame
交换为JOptionPane
(或模态 < code>JDialog)大部分问题都会得到解决。If you can swap the 2nd
JFrame
for aJOptionPane
(or modalJDialog
) most of the problems will be solved.您应该考虑触发
DefaultCloseOperation
,它只会触发与按下 JFrame 右上角的红色 x 相同的过程。然后,处理将由 JFrame 本身或者更确切地说是已经指定的来完成:
您可以通过多种方式实现这一点,如此问题的答案中所述。
可能性一:您将 WindowEvent 排入队列,从而伪造 CloseOperation:
可能性二:您处置 Window,而不将事件排入队列,但因此跳过您指定的所有 EventListener:
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:
You can accomplish that, as outlined in this question's answers in multiple ways.
Possiblity one: You enqueue a WindowEvent, thereby faking a CloseOperation:
Possibility two: You dispose of the Window, without enqueueing an event, but thereby skip all EventListeners you specified: