如何使用 JButton 打开文件
How to open a file using a JButton
我有一个文件,我们可以调用它 'example.txt' 并且我已经在我的程序中调用了该文件。我想添加在单击 JButton myButton
时打开文件的功能。到目前为止,这是我的代码:
myButton.setText("Button");
File example_file = new File("~/path/to/example.txt");
myButton.addActionListener(new java.awt.event ActionListener(){
public void actionPerformed (java.awt.event.ActionEvent evt){
myButtonActionPerformed(evt);
}
});
private void myButtonActionPerformed(java.awt.event.ActionEvent evt){
//open the file
}
我确定有一个非常简单的方法可以做到这一点,但我还没有找到任何东西。
Desktop.getDesktop().open(File file);
这可能对您有帮助:
Related Question and Answer.
Desktop.getDesktop().open(new java.io.File("path to file"));
只是一个建议:
为这类工作学习 C\C++。我做到了。
我有一个文件,我们可以调用它 'example.txt' 并且我已经在我的程序中调用了该文件。我想添加在单击 JButton myButton
时打开文件的功能。到目前为止,这是我的代码:
myButton.setText("Button");
File example_file = new File("~/path/to/example.txt");
myButton.addActionListener(new java.awt.event ActionListener(){
public void actionPerformed (java.awt.event.ActionEvent evt){
myButtonActionPerformed(evt);
}
});
private void myButtonActionPerformed(java.awt.event.ActionEvent evt){
//open the file
}
我确定有一个非常简单的方法可以做到这一点,但我还没有找到任何东西。
Desktop.getDesktop().open(File file);
这可能对您有帮助:
Related Question and Answer.
Desktop.getDesktop().open(new java.io.File("path to file"));
只是一个建议: 为这类工作学习 C\C++。我做到了。