2017-05-10 208 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(...)方法来读取加载数据。

相关问题