[英]How to close multiple JFrame and JDialog windows?
我正在開發一個具有多個JFrame
和JDialog
窗口的程序。
我有一個包含按鈕的 JFrame,當我單擊此按鈕時,會打開一個 JDialog 窗口。 在此 JDialog 窗口中還有另一個按鈕,單擊該按鈕會打開第二個 JDialog 窗口。 在第二個 JDialog 窗口中,我有一個最后一個按鈕。
我想要做的是在單擊最后一個按鈕時關閉JDialog
窗口和JFrame
窗口。
開倉順序是這樣的:
JFrame Frame1;
JButton Button1;
JDialog Dialog1;
JButton Button2;
JDialog Dialog2;
JButton Button3;
Button1ActionPerformed(ActionEvent e){
new Dialog(Frame1Frame);
}
Button2ActionPerformed(ActionEvent e){
new Dialog2(Dialog1Frame)
}
Button3ActionPerformed(ActionEvent e){
//Here I wnat to add the code that closes JDialog2 JDialog1 and JFrame1 windows.
}
我試過super.dispose();
但它不起作用。 有任何想法嗎?
也許有更好的方法可以做到這一點,但這是一種可能有用的通用方法。
在代碼中,您創建了窗口,但沒有將對創建的窗口的引用存儲到變量中。 例如,您有:
JDialog Dialog1;
然后,當您創建Dialog1的實例時,您將具有以下代碼:
Button1ActionPerformed(ActionEvent e){
new Dialog(Frame1Frame);
}
這意味着您已經創建了對話框,但是尚未保留對對話框的引用以供以后的代碼操作。 如果在此處分配此值,則以后應該可以對其進行操作。
如果將實現更改為:
Button1ActionPerformed(ActionEvent e){
Dialog1 = new Dialog(Frame1Frame);
}
然后在您的代碼的后面,您將引用該對話框以進行操作,
Button3ActionPerformed(ActionEvent e){
Dialog1.dispose();
// you can manipulate the variables of the class from here and close other windows etc.
}
如果有對象引用,則可以執行以下操作:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class Main
{
private static JFrame frame;
private static JButton buttonFrame;
private static JDialog dialog1;
private static JButton buttonDialog1;
private static JDialog dialog2;
private static JButton buttonDialog2;
public static void main(String[] args) {
/* frame */
frame = new JFrame("Main Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
buttonFrame = new JButton("open dialog 1");
buttonFrame.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
dialog1.setVisible(true);
}
});
frame.add(buttonFrame);
/* dialog 1 */
dialog1 = new JDialog(frame, "Dialog 1");
dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog1.setSize(300, 300);
dialog1.setLocationRelativeTo(null);
buttonDialog1 = new JButton("open dialog 2");
buttonDialog1.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
dialog2.setVisible(true);
}
});
dialog1.add(buttonDialog1);
/* dialog 2 */
dialog2 = new JDialog(dialog1, "Dialog 2");
dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog2.setSize(200, 200);
dialog2.setLocationRelativeTo(null);
buttonDialog2 = new JButton("close all");
buttonDialog2.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
dialog2.dispose();
dialog1.dispose();
frame.dispose();
}
});
dialog2.add(buttonDialog2);
/* show frame */
frame.setVisible(true);
}
}
否則,您可以使用System.exit(0);
:
buttonDialog2.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
如此處所示,使用Action
,您的actionPerformed()
實現可以將WINDOW_CLOSING
事件調度到所需的Window
實例。
@Override
public void actionPerformed(ActionEvent e) {
d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
d2.dispatchEvent(new WindowEvent(d2, WindowEvent.WINDOW_CLOSING));
f1.dispatchEvent(new WindowEvent(f1, WindowEvent.WINDOW_CLOSING));
}
關閉多個 JFrame-
可以關閉多個窗口,甚至是非靜態 Java 擺動窗口。 輸入下面的小代碼,一步一步。 只有一個彈出窗口會告訴,再次打開 class2,只有在光標移動到 class1 的情況下。
所有方法將僅在 Class1 中鍵入。
在第一類中創建第二類全局變量。
` Class2 NiceApp1;
//按鈕名稱將為 jToggleButton1。 您需要順時針單擊按鈕並選擇操作執行然后鍵入以打開第二個類。
私人無效jToggleButton1 ActionPerformed(java.awt.event.ActionEvent evt){
NiceApp1 = new Class2();
NiceApp1.setVisible(true);
//JOptionPane.showMessageDialog(null, "Test");//在這種情況下使用此彈窗不起作用,再次使用后僅注釋此彈窗。
}
//現在您將需要使用鼠標退出單擊同一個 jToggleButton1 選擇 //MouseExited。
private void jToggleButton1MouseExited(java.awt.event.MouseEvent evt) {
JOptionPane.showMessageDialog(null, "Open class2button again");
CloseClass1();
if(NiceApp1 instanceof Class2){
CloseClass2();
}
Class1 nice = new Class1();
nice.setVisible(true);
}
//現在您將需要使用焦點丟失單擊相同的jToggleButton1選擇FocusLost。
私人無效jToggleButton1FocusLost(java.awt.event.FocusEvent evt){
JOptionPane.showMessageDialog(null, "Affable to use Nice Application 1");
}
//現在只輸入你的類 Class1 和 Class2 關閉方法。
public void CloseClass1() {
WindowEvent winclosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winclosing);
}
public void CloseClass2() {
WindowEvent winclosing = new WindowEvent(NiceApp1,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winclosing);
}
`
請注意,如果您沒有在中心設置 Class2 位置,則需要在 Class2-this.setLocationRelativeTo(null); 中找到您的班級中心類型;
如果你做了上面的過程,你現在就完成了。
如果你不能順時針點擊按鈕來選擇執行的動作等,那么使用 jToggleButton1.addActionListener。
您也可以訪問 NiceApplication1.BlogSpot.Com。
您可以使用上面的代碼。 從 Class2 關閉窗口,您將看到之前的窗口也將關閉,並且會看到一個新的新 class1 窗口,僅使用比第一個類小的第二個類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.