[英]how to get buttons and actionlisteners to the JoptionPane attached in Java Swing?
Here is the simple code that has two JoptionPane
. 这是具有两个
JoptionPane
的简单代码。 currently, it has no buttons, but I want to attach button to the second JOptionPane
for Yes or NO event. 当前,它没有按钮,但是我想将按钮附加到第二个
JOptionPane
以进行Yes或NO事件。 Moreover, when the two JOptionPanes
are closed, the Frame
does not close.Is there a way to force close Frame
when JoptionPanes
are closed. 而且,当关闭两个
JOptionPanes
时, Frame
不会关闭。是否有一种方法可以在关闭JoptionPanes
时强制关闭Frame
。
here is my current code 这是我当前的代码
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestJoption {
public static void main(String[] args){
JFrame frame = new JFrame("Game");
JOptionPane.showMessageDialog(frame, "You Won!", "Winner", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(frame, "yes No", "play again", JOptionPane.INFORMATION_MESSAGE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
You can do it like this: 您可以这样做:
JOptionPane.showConfirmDialog(frame, "yes No", "play again", JOptionPane.YES_NO_OPTION);
this is popup the box with yes and no options.. 这是弹出框,是,没有选项。
I think you need next code: 我认为您需要下一个代码:
JOptionPane.showMessageDialog(frame, "You Won!", "Winner", JOptionPane.INFORMATION_MESSAGE);
int result = JOptionPane.showConfirmDialog(frame, "yes No", "play again",JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.NO_OPTION){
frame.dispose();
}
For closing the window you can either use 要关闭窗口,您可以使用
frame.setVisible(false);
or you can simply call 或者你可以简单地打电话
System.exit(0);
this will terminate your process. 这将终止您的过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.