2016-08-19 61 views
0

我試圖做的是做一個自定義安裝程序,我有按鈕和工作正常,但我想運行另一個類時調用CopyDir.java當我點擊按鈕它將必要的文件複製到正確的目錄。問題是,我是如何做到這一點難倒的。JFrame,試圖讓按鈕運行另一個類

public class Frame extends JFrame implements ActionListener { 
JPanel pane = new JPanel(); 
JButton PC = new JButton("Install Mod (PC)"); 
JButton Steam = new JButton("Install Mod (Steam)"); 
JLabel Text = new JLabel("Welcome to the BTD 5 Mod Installer"); 
JLabel Text2 = new JLabel("Click on the button that matches your version of BTD 5"); 
JLabel Text3 = new JLabel("To install it for the version that you are using"); 
JLabel Text4 = new JLabel("© Nixxx60/Nanikos"); 
Frame() { 
    super("BTD 5 Mod Installer"); 
    setBounds(100, 100, 400, 150); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container con = this.getContentPane(); 
    con.add(pane); 
    PC.setMnemonic('P'); 
    PC.addActionListener(this); 
    pane.add(PC); 
    PC.requestFocus(); 
    con.add(pane); 
    Steam.setMnemonic('P'); 
    Steam.addActionListener(this); 
    pane.add(Steam); 
    Steam.requestFocus(); 
    setVisible(true); 
    pane.add(Text); 
    pane.add(Text2); 
    pane.add(Text3); 
    pane.add(Text4); 
} 

public void actionPerformed(ActionEvent event) { 
    Object source = event.getSource(); 
    if (source == PC) { 
     JOptionPane.showMessageDialog(null, "Mod has been installed on PC/Cracked Edition!", "BTD 5 Installer", 
      JOptionPane.PLAIN_MESSAGE); 
     setVisible(true); 
    } 
    if (source == Steam) { 
     JOptionPane.showMessageDialog(null, "Mod has been installed for Steam Edition!", "BTD 5 Installer", 
      JOptionPane.PLAIN_MESSAGE); 
     setVisible(true); 
    } 
} 
public static void main(String args[]) { 
    new Frame(); 
} 
} 

此外,這裏是「CopyDir.java」類的代碼。

package Nanikos.BTD5.Main; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 

public class CopyDir { 

    public static void main(String args[]) throws Exception 
    { 
     copyFiles(new File("C:\\Users\\User\\Desktop\\BTD5 Mod Installer\\Mod Files\\PC Assets"),new File("C:\\Program Files (x86)\\Steam\\steamapps\\common\\BloonsTD5")); 
     System.out.println("Files Copied"); 
    } 

    public static void copyFiles(File src,File des) throws Exception 
    { 
     if(src.isDirectory()) 
     { 
      if(!des.exists()) des.mkdir(); 
      String [] filePaths=src.list(); 
      for(String filePath: filePaths) 
      { 
       File srcFile =new File(src, filePath); 
       File desFile =new File(des, filePath); 
       copyFiles(srcFile,desFile); 
      } 
     } 
     else 
     { 
      FileInputStream from =null; 
      FileOutputStream to =null; 

      from = new FileInputStream(src); 
      to = new FileOutputStream(des); 
      byte [] buffer=new byte[4096]; 
      int byteReads; 

      while((byteReads=from.read(buffer))!=-1) 
      { 
       to.write(buffer,0,byteReads); 
      } 

      from.close(); 
      to.close(); 
     } 
    } 
} 
+0

嘿尼古拉斯,問題是我不明白。爲了響應按鈕點擊,您必須像編輯一樣編寫「ActionListener」。如果你想調用另一個類,調用構造函數,然後調用方法。 '新的CopyDir()。doSomething()'。如果你想開始一個長時間運行的任務,請參閱關於[Swing中的線程]的文檔(https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – Dangermouse

+0

另外:'if(source = = PC){...'應該是'if(source.equals(PC)){...'。你可能想要使用if(...){...} else if(...){...}'-construct。 – pzaenger

回答

0

你想要的是推出類爲Thread

要啓動您的CopyDir類作爲一個線程,使其實現Thread,你main方法簽名改成這樣:

public void run() { 
    //Your code 
} 

此外,爲了將參數傳遞給線程,請在您的CopyDir類中添加一個構造函數,該構造函數接受您擁有的參數並將它們存儲爲屬性,以便能夠從您的方法中獲取它。

然後,從你的事件偵聽器啓動線程:

CopyDir myCopyThread = new CopyDir(inputPath,outputPath); 
myCopyThread.start(); 

此代碼將創建開始在run()方法運行CopyDir線程

+0

工作完美,只是移動'CopyDir myCopyThread = new CopyDir();我的所有需要​​做的就是製作完全像CopyDir的另一個類,然後調用它CopyDir1,然後做同樣的事情,但把另一個放在'if (source == Steam){}'因爲CopyDir1將用於其他目錄文件。 –

+0

好的,還有一個問題。 C:\\用戶\\用戶\\桌面\\ BTD5 Mod安裝程序\\ Mod文件\\ PC資產我怎麼能得到它來檢測用戶,因爲不是每個人都安裝這個用戶名爲用戶。我知道有公共靜態void main(){ \t String username = System.getProperty(「user.name」); \t System.out.println(「username =」+ username); \t} \t}但我想它採取user.name並將其插入到C:\\ Users \ ********* \ –

+0

嘗試查看[this](http:// stackoverflow .COM /問題/ 9677692 /讓 - 我的文檔 - 路徑 - 在-JAVA) – Dijdkay