簡體   English   中英

使用JButton和ActionListener啟動記事本

[英]Launching notepad using JButton and ActionListener

我做了大約1-2個小時的谷歌搜索,這應該是一個非常簡單的問題

我需要使用程序GUI中的按鈕來啟動txt文檔,例如在我的桌面(C:\\ Users \\ Computer \\ Desktop \\ text.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");
    }

嘗試類似...

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

看看如何與桌面類集成以獲得更多詳細信息...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM