我正在使用JFileChooser
,在按关闭按钮后不想关闭它。问题是,我按下关闭按钮后,会再次打开3次以上,最后关闭。JFileChooser不想关闭
我的代码:
javaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save");
int option = fileChooser.showSaveDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
String filename = fileChooser.getFileFilter().getDescription();
try {
ChartUtilities.saveChartAsPNG(new File(filename), chart, getWidth(), getHeight());
} catch (java.io.IOException exc) {
System.err.println("Error writing image to file");
}
}
if (option == JFileChooser.CANCEL_OPTION) {
System.out.println("Task canceled!");
//tried: fileChooser.setVisible(false); // >> same problem
}
}
});
有什么建议?
1)为了更好地帮助越早,张贴[SSCCE(http://sscce.org/)。 2)请为代码块使用一致的逻辑缩进。 –
'if(option == JFileChooser.CANCEL_OPTION){'应该只是'else {'。 –
这可能是也可能不是你的问题,但把你的右花括号放在与你的代码块相同的行上(参见catch块),或者将它们缩进到与花括号相同的位置{{(参见try块)非常混乱,并且很容易导致逻辑错误。相反,您可能会发现它更容易(我们一定会更清楚地理解您的代码),将'}'放在一行上,与块的开头对齐('try'中的字符't',例如)你打算关闭。 – dimo414