JDialog模态问题java gui
我的代码运行时没有任何错误,但它没有按我计划的方式运行。我试图找出为什么当我单击标有“重要!!”的按钮时没有显示“继续”按钮。
唯一显示的是一个空白的弹出窗口,它是代码 JDialog 的一部分,并且设置为模态且可见。我就是想不通。如果有人可以帮助我,我将非常感激。
JPanel hehePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20));
JDialog dialog = new JDialog((JFrame)null);
dialog.getContentPane().add(hehePanel,BorderLayout.CENTER);
JButton hButton = new JButton("important!!");
JButton fButton = new JButton(" on construction !!");
JButton exitButton = new JButton("EXIT CAW ");
hehePanel.add(hButton);
hButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JPanel hehePanel = new JPanel();
final JDialog dialog = new JDialog();
dialog.getContentPane().add(hehePanel,BorderLayout.PAGE_END);
dialog.toFront();
dialog.setModal(true);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
JButton closebutton = new JButton("Continue");
closebutton.setActionCommand("continue");
closebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("continue")) {
dialog.dispose();
}
}
});
hehePanel.add(closebutton);
}
});
hehePanel.add(fButton);
hehePanel.add(exitButton);
I have this code which runs without any errors but it isn't running the way I planned. I'm trying to figure out why my "Continue" button is not being displayed when I click the button labeled "important!!".
The only thing which is being displayed is a blank pop up window which is part of the code JDialog and it set to modal and visible. I just can't figure it out. If anyone can help me I would appreciate it a lot.
JPanel hehePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20));
JDialog dialog = new JDialog((JFrame)null);
dialog.getContentPane().add(hehePanel,BorderLayout.CENTER);
JButton hButton = new JButton("important!!");
JButton fButton = new JButton(" on construction !!");
JButton exitButton = new JButton("EXIT CAW ");
hehePanel.add(hButton);
hButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JPanel hehePanel = new JPanel();
final JDialog dialog = new JDialog();
dialog.getContentPane().add(hehePanel,BorderLayout.PAGE_END);
dialog.toFront();
dialog.setModal(true);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
JButton closebutton = new JButton("Continue");
closebutton.setActionCommand("continue");
closebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("continue")) {
dialog.dispose();
}
}
});
hehePanel.add(closebutton);
}
});
hehePanel.add(fButton);
hehePanel.add(exitButton);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在使对话框可见之前以及
pack()
对话框之前将所有组件添加到对话框中。dialog.setVisible(true) 之后的所有代码在对话框关闭之前不会执行。
You need to add all the components to the dialog before making the dialog visible and before you
pack()
the dialog.All the code after
dialog.setVisible(true)
does not execute until the dialog is closed.