gpt4 book ai didi

java - 如何等待 FileChooser 选择?

转载 作者:行者123 更新时间:2023-12-01 06:12:30 24 4
gpt4 key购买 nike

我想要做的是使用FileChooser来选择路径。

选择后,该路径应由以下实例使用。

我的问题是如何强制所有内容在路径上等待,因为否则程序只是运行而不等待。

    //GUI 
JFrame frame = new JFrame("Window");
FileChooser panel = new FileChooser();

frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
if(panel.getPath() == null){

}
String path = panel.getPath();

//some additional stuff that does not need any pathinformation
.......
//next step calculation which runs without waiting
Calculation calc = new Calculation();
calc.run(path);

提前致谢

附注
这是我的 ActionListner 包含的内容

        if (result == JFileChooser.CANCEL_OPTION) {
System.out.println("Cancel was selected");
}
else if (result == JFileChooser.APPROVE_OPTION) {
path = chooser.getSelectedFile().getAbsolutePath();
System.out.println("getCurrentDirectory(): "

+ chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "
+ chooser.getSelectedFile());
}
else {
System.out.println("No Selection ");
}

最佳答案

您可以使用偶数监听器:

panel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
if (e.getActionCommand() == JFileChooser.APPROVE_OPTION) {
file = panel.getSelectedFile();
// Do what you want with selected file
} else {
// When user pressed close or "X" in the corder.
}
}
});

代替String path = panel.getPath();

希望这有帮助。

关于java - 如何等待 FileChooser 选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32433739/

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