gpt4 book ai didi

java - WindowEvent.WINDOW_CLOSING 提前终止程序

转载 作者:行者123 更新时间:2023-11-29 03:19:24 24 4
gpt4 key购买 nike

我正在尝试使用 GUI 写入文本文件。我在执行此操作时遇到了问题,并且能够弄清楚 WindowEvent 出于某种原因以某种方式提前终止了程序。

最初我在 WindowEvent 行之后有 outFile.close();,因此 JTextArea 中的文本不会传输到文本文件。切换几行代码后,我意识到当尝试使用 WindowEvent 自动关闭 JFrame 时,之后的代码都没有执行。

我该如何解决这个问题?

import ...

public class TxtManager {
static Input input;

public static void overwriteCurrentFile() throws IOException {
TxtManager txt = new TxtManager();
input = new Input(txt);
}

public synchronized void sendData() throws IOException {
try {
BufferedWriter outFile = new BufferedWriter(new FileWriter(file1));
String data = input.textArea.getText();
outFile.write(data);
outFile.close();
input.frame.dispatchEvent(new WindowEvent(input.frame,WindowEvent.WINDOW_CLOSING));
// Code from this point on in the try block does not execute.
System.out.println("Finished with the write out..."); // Used for pinpointing the problem
JOptionPane.showMessageDialog(null,"Data in " + TxtManager.file1 + " has been overwritten successfully.");
TxtManager.showData = true;
TxtManager.menu2();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Error: " + ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
}

class Input extends Thread {
TxtManager txt;
public JTextArea textArea;
public JFrame frame;
private JButton button;

public Input(TxtManager txt) throws IOException {
this.txt = txt;
JOptionPane.showMessageDialog(null,"Enter desired data into the GUI screen.\n" +
"Press the <Done> button once you are finished.");
frame = new JFrame("Prompt");
JPanel panel = new JPanel(new BorderLayout());
frame.add(panel);
JLabel label = new JLabel("Enter desired data.");
panel.add(label,BorderLayout.NORTH);
textArea = new JTextArea(15,80);
panel.add(textArea,BorderLayout.CENTER);
panel.add(new JScrollPane(textArea));
button = new JButton("Done");
panel.add(button,BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

start();
}

public void run() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
txt.sendData();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"Error: " + ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
});
}
}

最佳答案

我相信你的问题源于这一行(假设 input.frame 等同于你在这里创建的框架)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

来自API

EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

这一切意味着当您抛出 WindowClosing 事件时,框架(理所当然地)会尝试自行关闭。这又会调用默认的关闭操作,恰好是 EXIT_ON_CLOSE,它会在那里调用 System.exit()。这将结束您的程序而不执行任何更多代码行。您可能正在寻找的是 WindowConstants.DISPOSE_ON_CLOSE 而不是 Frame.EXIT_ON_CLOSE,它应该关闭窗口并释放其资源而不退出您的程序。

老实说,通过 Frame.setVisible(false); 隐藏窗口可能更有意义。启动它的开销。

关于java - WindowEvent.WINDOW_CLOSING 提前终止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24642394/

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