2017-05-10 116 views
-3

我正在爲我的Minecraft服務器開發一個Screen Share工具,我希望你能夠點擊一個按鈕,打開一個像\ program.exe這樣的應用程序,另一個按鈕打開一個目錄,如%appdata%.minecraft單擊按鈕時如何打開文件/文件夾?

單擊按鈕時如何打開文件/文件夾?

這裏是我的代碼,你可以編輯爲例:

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JProgressBar; 
import javax.swing.JButton; 

public class Minenow { 

    private JFrame frmMinenow; 

    /** 
    * Launch the application. 
    */ 
    public static void NewScreen() { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Minenow window = new Minenow(); 
        window.frmMinenow.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public Minenow() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frmMinenow = new JFrame(); 
     frmMinenow.setTitle("Minenow"); 
     frmMinenow.setBounds(100, 100, 793, 503); 
     frmMinenow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frmMinenow.getContentPane().setLayout(null); 

     JButton btnNewButton = new JButton(".minecraft"); 
     btnNewButton.setBounds(58, 50, 161, 80); 
     frmMinenow.getContentPane().add(btnNewButton); 
    } 
`} 
+0

你有問題要問?在這篇文章中沒有真正的問題。 – Michael

回答

0

通過使用JFileChooser中,你可以輕鬆地打開一個FileDialog的(如果這是你的意思)。看看here,還應該有一種方法來指定哪個fokder需要打開。

2

我想你是能夠點擊一個按鈕,它就會打開一個應用程序像\ Program.exe文件和另一個按鈕打開一個目錄,如%APPDATA%.minecraft

可以使用Desktop類打開外部應用程序。

閱讀Swing教程How to Integrate With the Desktop Class的部分以獲取更多信息和工作示例。

如果您想將文本文件讀入應用程序,那麼您可以將文件讀入文本區域。上面的教程鏈接也有關於How to Use Text Areas的部分。您可以使用JTextArea的read(...)方法來讀取加載數據。

相關問題