如何理解 jFileChooser1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION
How to understand jFileChooser1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION
我试着理解“jFileChooser1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION
”是什么意思?
我认为 jFileChooser1.showSaveDialog(this)
returns 一个 SaveDialog
但 JFileChooser.APPROVE_OPTION
returns yes
或 ok
。
他们怎么能相等?
private void save() {
if (jFileChooser1.showSaveDialog(this) ==
JFileChooser.APPROVE_OPTION) {
save(jFileChooser1.getSelectedFile());
}
}
/** Save file with specified File instance */
private void save(File file) {
try {
// Write the text in jta to the specified file
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
byte[] b = (jta.getText()).getBytes();
out.write(b, 0, b.length);
out.close();
// Display the status of the save file operation in jlblStatus
jlblStatus.setText(file.getName() + " Saved ");
}
catch (IOException ex) {
jlblStatus.setText("Error saving " + file.getName());
}
}
参考 JavaDocs API
public int showSaveDialog(Component parent) throws HeadlessException
弹出 "Save File" 文件选择器对话框。请注意,批准按钮中显示的文本由 L&F 确定。
参数:
parent - 对话框的父组件,可以为空;详见 showDialog
Returns:
弹出时文件选择器的 return 状态:
- JFileChooser.CANCEL_OPTION`
- JFileChooser.APPROVE_OPTION
- JFileChooser.ERROR_OPTION 如果发生错误或对话框被关闭
JavaDocs 可以在这里找到:
我试着理解“jFileChooser1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION
”是什么意思?
我认为 jFileChooser1.showSaveDialog(this)
returns 一个 SaveDialog
但 JFileChooser.APPROVE_OPTION
returns yes
或 ok
。
他们怎么能相等?
private void save() {
if (jFileChooser1.showSaveDialog(this) ==
JFileChooser.APPROVE_OPTION) {
save(jFileChooser1.getSelectedFile());
}
}
/** Save file with specified File instance */
private void save(File file) {
try {
// Write the text in jta to the specified file
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
byte[] b = (jta.getText()).getBytes();
out.write(b, 0, b.length);
out.close();
// Display the status of the save file operation in jlblStatus
jlblStatus.setText(file.getName() + " Saved ");
}
catch (IOException ex) {
jlblStatus.setText("Error saving " + file.getName());
}
}
参考 JavaDocs API
public int showSaveDialog(Component parent) throws HeadlessException
弹出 "Save File" 文件选择器对话框。请注意,批准按钮中显示的文本由 L&F 确定。 参数: parent - 对话框的父组件,可以为空;详见 showDialog Returns: 弹出时文件选择器的 return 状态:
- JFileChooser.CANCEL_OPTION`
- JFileChooser.APPROVE_OPTION
- JFileChooser.ERROR_OPTION 如果发生错误或对话框被关闭
JavaDocs 可以在这里找到: