gpt4 book ai didi

java - 系统退出而不是JFrame关闭

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:58 26 4
gpt4 key购买 nike

private void close () {

WindowEvent winClosing;
winClosing = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosing);
}

我试图在上面提到的关闭功能的帮助下关闭以前的 Jframe,方法是将这段代码放在附图中突出显示的后退按钮中

    this.close();
MainMenu obj = new MainMenu();
obj.setVisible(true);

问题是上面的代码关闭了我的整个应用程序,而不是关闭现在激活的框架

enter image description here

最佳答案

这是您正在尝试做的(我认为的)快速演示。
请注意:
a.我认为这不是实现此功能的正确方法。考虑使用JDialog或辅助窗口的内部框架。
b.将其视为 MCVE 的示例。事实上,使用一个辅助框架而不是两个辅助框架来演示该问题要好得多。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MainWindow extends JFrame {

private JFrame frame1, frame2;

public MainWindow() {

super("Main");

//make 2 other frames
frame1 = new Frame1(this);
frame2 = new Frame2(this);

setSize(400, 300);

//add listener to quit if when window closes
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {

System.exit(0);
}
});

//add buttn top open one frame
JButton btnFrame1 = new JButton("Frame 1");
btnFrame1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {

//make frame 1 visible
frame1.setVisible(true);
//make this invisible
setVisible(false);
}
});
getContentPane().add(btnFrame1, BorderLayout.NORTH);

//add button to open a second frame
JButton btnFrame2 = new JButton("Frame 2");
btnFrame2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

//make frame 1 visible
frame2.setVisible(true);
//make this invisible
setVisible(false);
}
});
getContentPane().add(btnFrame2, BorderLayout.SOUTH);

//make main frame visible
setVisible(true);
}

public static void main(String[] args) {
new MainWindow();
}

public void showMain() {

//make this one invisible
frame1.setVisible(false);
frame2.setVisible(false);

//make main window visible
setVisible(true);
}
}


class Frame1 extends JFrame {

public Frame1(MainWindow mainWindow) {

super("Frame 1");
setSize(300, 200);

//add listener to show main this window closes
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {

mainWindow.showMain();
}
});

//add btn to hide this window and show main
JButton btnFrame1 = new JButton("Back");
btnFrame1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

mainWindow.showMain();
}
});
getContentPane().add(btnFrame1, BorderLayout.SOUTH);
getContentPane().setBackground(Color.CYAN);

//make it invisible
setVisible(false);
}
}

class Frame2 extends JFrame {

public Frame2(MainWindow mainWindow) {

super("Frame 2");
setSize(300, 200);

//add listener to show main this window closes
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {

mainWindow.showMain();
}
});
JButton btnFrame1 = new JButton("Back");
btnFrame1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

mainWindow.showMain();
}
});

//add btn to hide this window and show main
JButton btnMain = new JButton("Back");
btnMain.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

mainWindow.showMain();
}
});
getContentPane().add(btnMain, BorderLayout.SOUTH);
getContentPane().setBackground(Color.YELLOW);

//make it invisible
setVisible(false);
}
}

如有需要,请随时要求澄清。

关于java - 系统退出而不是JFrame关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39178418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com