我有一个JFrame主窗口,其中有一个注册按钮。点击注册按钮,弹出JDialog窗口。
public void mouseClicked(MouseEvent e) {
Reg new1=new Reg(users);
new1.setVisible(true);
}
JDialog窗口有2个按钮 - >注册,取消。它们必须做某事并关闭对话框窗口。
这就是我尝试过的。 在Reg(对话窗口)---> btnCancel:
public void mouseClicked(MouseEvent e) {
dialog.dispose();
System.out.println("Reg disposed by cancel button");
}
当只运行D窗口时,这会关闭D窗口,但我想当从主窗口执行时(单击按钮),它仍然像主fraim"类"中的对象一样存在。并且不会关闭。我该怎么办?如何让它关闭?
答案 0 :(得分:1)
您需要某种方式来确定对话框的关闭方式
// Why are you using a `MouseListener` on buttons??
// User use keyboards to, use an ActionListener instead
public void mouseClicked(MouseEvent e) {
Reg new1=new Reg(users);
new1.setVisible(true);
switch (new1.getDisposeState()) {
case Reg.OK:
// Clicked Ok
break;
case Reg.CANCEL:
// Clicked cancel or was closed by press [x]
break;
}
}
然后在Reg
课程中,您需要维护有关州的信息......
public class Reg extends JDialog {
public static final int OK = 0;
public static final int CANCEL = 1;
private int disposeState = CANCEL;
//...
public int getDisposeState() {
return disposeState
}
public void setDisposeState(int state) {
disposeState = state;
}
然后你改变状态
// Why are you using a `MouseListener` on buttons??
// User use keyboards to, use an ActionListener instead
public void mouseClicked(MouseEvent e) {
setDisposeState(CANCEL);
dialog.dispose();
System.out.println("Reg disposed by cancel button");
}
这一切都假设您的对话框当然是模态的......
现在,说了这么多,就个人而言,我会让你的Reg
课程为JPanel
,然后将其添加到JOptionPane
或使用CardLayout
看看:
...了解更多详情