我有一段代码,一旦用户单击按钮,就会打开 JOptionPane 对话框。我想做的第一件事是在用户点击其中一个按钮后立即关闭第一个 JFrame。我尝试过这样做
setVisible(false); // Delete visibility
dispose(); //Delete window
但是按下按钮后原始 JFrame 不会立即关闭。目标是有两个按钮。当按下一个按钮时,显示一个 JOptionPane 框,同时同时关闭第一个 JFrame 窗口。我该如何做到这一点?接下来,在新的 JOptionPane 中按下“确定”后,我不想刺激 Action 监听器。我通过调用方法来做到这一点
sinceyoupressedthecoolbutton();
就在我的 JOptionPane 声明之后。这是第二个问题开始的地方,它完美地显示了 JOptionPane,但没有转到方法
sinceyoupressedthecoolbutton();
不知道问题是出在方法调用上还是方法内容上。基本上,在 JOptionPane 上按下 ok 后,我不想移动到另一个打开新 JLabel 的方法。
这是代码:
package Buttons;
import java.awt.Dimension;
import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JLabel;
import javax.swing.JButton; //BUTTON!!!
import javax.swing.JDialog;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box
public class ButtonClass extends JFrame {
private JButton regular;
private JButton custom;
public ButtonClass() { // Constructor
super("The title"); // Title
setLayout(new FlowLayout()); // Default layout
regular = new JButton("Regular Button");
add(regular);
custom = new JButton("Custom", b);
add(custom);
Handlerclass handler = new Handlerclass();
Otherhandlerclass original = new Otherhandlerclass();
regular.addActionListener(handler);
custom.addActionListener(original);
//THIS WAS MY FIRST PROBLEM, I WANT TO CLOSE THE FIRST JFRAME WINDOW AS THE USER HITS OK
setVisible(false); // Close the show message dialog box
dispose();
}
public class Handlerclass implements ActionListener { // This class is
// inside the other
// class
public void actionPerformed(ActionEvent eventvar) { // This will happen
// when button is
// clicked
JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand())); //opens a new window with the name of the button
}
}
public class Otherhandlerclass implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"Since you pressed that button, I will open a new window when you press ok, okay?");
//Code works up until here
sinceyoupressedthecoolbutton(); //THIS METHOD SHOULD OPEN A NEW WINDOW!
}
public void sinceyoupressedthecoolbutton() {
JDialog YES = new JDialog();
JLabel label = new JLabel("Here is that new window I promised you!");
add(label);
YES.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
YES.setSize(500, 500);
YES.setVisible(true);
}
public class okay implements ActionListener {
public void actionPerformed(ActionEvent ok) {
}
}
}
}
帮助将不胜感激!
最佳答案
您正在事件驱动的环境中工作,而不是线性处理环境。这意味着您运行一些代码,然后等待(等待已经为您完成)某个事件的发生,然后您对其做出响应...
在用户按下自定义
按钮之前不会发生任何事情,因此在此之前尝试关闭框架是没有意义的
当您的 Otherhandlerclass
的 actionPerformed
被触发时,显然您会显示 JOptionPane
Pane ,这将阻止执行流程,直到它被触发为止。关闭。此时,您应该有机会处理原始窗口。
因此,与其在构造函数中调用 setVisible(false)
和 dispose
,最好将其移至 Otherhandlerclass
public class Otherhandlerclass implements ActionListener {
public void actionPerformed(ActionEvent e) {
dispose();
JOptionPane.showMessageDialog(null, "Since you pressed that button, I will open a new window when you press ok, okay?");
sinceyoupressedthecoolbutton();
}
public void sinceyoupressedthecoolbutton() {
JDialog YES = new JDialog();
JLabel label = new JLabel("Here is that new window I promised you!");
YES.add(label);
YES.setSize(500, 500);
YES.setVisible(true);
}
public class okay implements ActionListener {
public void actionPerformed(ActionEvent ok) {
}
}
}
话虽如此,我鼓励您阅读 The Use of Multiple JFrames, Good/Bad Practice?也许可以考虑使用类似 How to Use CardLayout 的东西相反
关于java - 按下“确定”后如何打开新窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085208/