0

我已经做了大约 1 到 2 个小时的谷歌搜索,这应该是一个非常简单的问题

我需要使用程序 GUI 中的按钮在我的桌面 (C:\Users\Computer\Desktop\text.txt) 上启动一个 txt 文档,例如 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);

和动作监听器:

 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");
    }
4

1 回答 1

6

尝试类似...

Desktop.getDesktop().open(new File("C:/Users/Computer/Desktop/text.txt")));

查看如何与桌面类集成以获取更多详细信息...

于 2013-06-10T07:13:13.283 回答