使用JButton和ActionListener启动记事本

use*_*932 0 java swing jbutton actionlistener bluej

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

我需要使用程序GUI中的按钮在我的桌面上启动一个txt doc,例如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);
Run Code Online (Sandbox Code Playgroud)

和动作监听器:

 class creatingOpenFileListener implements ActionListener
        {
           public void actionPerformed(ActionEvent event)
           {

           }
        }
Run Code Online (Sandbox Code Playgroud)

我尽我所能,但我只是出于想法,我甚至前往第5页谷歌搜索寻找答案应该是一个简单的问题,任何帮助都会很棒,谢谢.

编辑:我的意思是启动,而不是打开GUI中的内容,我发布它就好像我要去点击它自己.

回答

     try
    {
        Desktop.getDesktop().open(new File("C://Users//Computer//Desktop//text.txt"));
    }
    catch(java.io.IOException e)
    {
        System.out.println("IOError");
    }
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 6

试试像......

Desktop.getDesktop().open(new File("C:/Users/Computer/Desktop/text.txt")));
Run Code Online (Sandbox Code Playgroud)

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