您好,我正在使用 Java Swing 开发一个程序,并且设置了 4 个选项 Pane 来获取某些输入,但是当我运行该程序时,它会显示选项窗口,但当我关闭选项 Pane 时它运行并运行最后一个按钮,我必须执行程序的其余部分。我目前很困惑为什么。以下是 actionPerformed() 方法和文件选择器方法的代码。请注意,选项 Pane 用于从单选按钮获取除"is"或“否”选择之外的输入,因此 4 个检查* 方法用于查看按下了哪个单选按钮以及如何处理该信息。
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("settings")) {
JOptionPane.showOptionDialog(null, encryptPanel,
"Settings Choices", JOptionPane.NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
}
if (e.getActionCommand().equals("paths")) {
JOptionPane.showOptionDialog(null, pathsPanel,
"Paths Options", JOptionPane.NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
}
if (e.getActionCommand().equals("tools")) {
JOptionPane.showOptionDialog(null, toolsPane,
"Tools Options", JOptionPane.NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
}
if (e.getActionCommand().equals("techniques")) {
JOptionPane.showOptionDialog(null, methodPane,
"Choose your encryption method", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
}
checkEncrypt(e.getActionCommand());
checkPaths(e.getActionCommand());
checkTools(e.getActionCommand());
checkTech(e.getActionCommand());
if (e.getActionCommand().equals("go")) ;
{
runLauncher();
}
}
private void runLauncher()
{
directory.makeDir("PEP");
JFileChooser getFile = new JFileChooser();
getFile.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = getFile.showOpenDialog(this);
String str;
int numWheels = Integer.getInteger(wheels.getText());
if (result == JFileChooser.APPROVE_OPTION) {
str = getFile.getSelectedFile().getAbsolutePath();
int result2 = getFile.showOpenDialog(this);
if (result2 == JFileChooser.APPROVE_OPTION) {
String endFilePath = getFile.getSelectedFile().getAbsolutePath();
if(gOn)
{
launcher go = new launcher(str, endFilePath, numWheels, 5);
go.run();
}
else
{
launcher go = new launcher(str, endFilePath, numWheels, selection);
go.run();
}
}
selection = 0;
}
}
最佳答案
if (e.getActionCommand().equals("settings")) {
JOptionPane.showOptionDialog(null, encryptPanel,
"Settings Choices", JOptionPane.NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
}
...
checkEncrypt(e.getActionCommand());
JOptionPane.showOptionDialog(...)
方法不会更改 actionCommand
的值。它只是返回一个代表单击了哪个按钮的 int 值。
所以基本上,您无缘无故地显示选项 Pane ,因为您的代码从不使用从选项 Pane 返回的值。
所以也许你的代码应该是这样的:
int option = JOptionPane.showOptionDialog(...);
然后根据返回的值进行处理。
checkEncrypt( option );
或者也许代码应该是这样的:
if (e.getActionCommand().equals("settings"))
{
int option = JOptionPane.showOptionDialog(...):
checkEncrypt( option );
}
我不知道为什么要分别调用这四个方法。
无论如何,您的代码都需要重组。
关于java - JOptionPane 运行 FileChooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33987768/