我用谷歌搜索了大约 1 到 2 个小时,才找到一个非常简单的问题
我需要使用程序 GUI 中的按钮启动 txt 文档,例如桌面上的 text.txt (C:\Users\Computer\Desktop\text.txt)。
按钮代码很简单。
JButton btnAddButton1 = new JButton("Display ShopEmployee"); // start of button
btnAddButton1.setVisible( false); // removing from sight
contentPane.add(btnAddButton1);
btnAddButton.addActionListener(new creatingOpenFileListener()); // the button is going to create a Person
btnAddButton.setVisible(true);
和 Action 监听器:
class creatingOpenFileListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
我已经尽了一切努力,但我只是没有想法,我什至到谷歌搜索的第 5 页寻找应该是一个简单问题的答案,任何帮助都会很棒,谢谢。
编辑: 我的意思是“启动”,而不是打开 GUI 中的内容,我要启动它,就好像我要自己点击它一样。
回答
try
{
Desktop.getDesktop().open(new File("C://Users//Computer//Desktop//text.txt"));
}
catch(java.io.IOException e)
{
System.out.println("IOError");
}
最佳答案
尝试类似...
Desktop.getDesktop().open(new File("C:/Users/Computer/Desktop/text.txt")));
看看How to Integrate with the Desktop Class了解更多详情...
关于java - 使用 JButton 和 ActionListener 启动记事本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17018566/