JFrame,试图使按钮运行另一个类

时间:2016-08-19 07:44:37

标签: java jframe jpanel

我正在尝试做的几乎是制作一个自定义安装程序,我有按钮,工作正常,但我想在我点击按钮时运行另一个名为CopyDir.java的类,以便复制必要的文件到正确的目录。问题是,我对如何做到这一点感到有点难过。

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您想要的是以Thread

的方式启动课程

要将您的课程CopyDir作为主题启动,请将其设为Thread,将main方法签名更改为:

public void run() {
  //Your code
}

此外,要将参数传递给您的线程,请在CopyDir类中添加一个构造函数,该构造函数接受您拥有的参数并将它们存储为属性,以便能够从您的方法中获取它。

然后,从事件监听器启动线程:

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

此代码将创建一个在CopyDir方法

中开始运行run()的线程