如何让JFrame关闭外部窗口?

时间:2017-10-25 02:23:58

标签: java eclipse user-interface jbutton

我想知道是否有人知道如何让JFrame关闭外部窗口。例如.bat文件。我的GUI窗口看起来像这样, (https://cdn.discordapp.com/attachments/339245512647770112/372569903070183425/unknown.png) 我想在其中说“关闭”关闭我用“运行”按钮打开的文件。

public void AddPrice(int turnTimeValue, decimal price)
{
/* --> */ _unitPrices[turnTimeValue] = price;
}

是否有可能使它能够使用相同或类似的代码关闭文件?如果您有任何关于此的信息,那么我很乐意听到。

感谢您的时间和考虑。 -Brand0n

1 个答案:

答案 0 :(得分:2)

您可以使用ProcessBuilder启动您的程序。然后可以使用返回的进程终止程序。

这是一个完整的工作代码

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

import javax.swing.*;

public class TestFrame {

public static void testWithoutUI(String s[]) {
    ProgramRunner programRunner = new ProgramRunner("notepad.exe");
    programRunner.start();

    System.out.println("waiting");
    try {
        TimeUnit.SECONDS.sleep(10);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    programRunner.getProcess().destroyForcibly();

    System.out.println("done");

}

public static void main(String s[]) {

    JFrame frame = new JFrame("JFrame Example");

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());

    JLabel label = new JLabel("Self-Bot");


    ProgramRunner programRunner = new ProgramRunner("notepad.exe");//C:\\Users\\User\\Desktop\\Discord-Selfbot-master\\self-bot.bat



    JButton btnStart = new JButton("Run");
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnStart.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            programRunner.start();
        }
    });

    JButton buttonClose = new JButton();
    buttonClose.setText("close");

    buttonClose.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            programRunner.getProcess().destroyForcibly();
        }
    });

    panel.add(label);
    panel.add(btnStart);
    panel.add(buttonClose);

    frame.add(panel);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}



class ProgramRunner extends Thread{
private String pathToFile = null;
private Process process = null;

public ProgramRunner(String pathToFile) {
    this.pathToFile = pathToFile;
}

@Override
public void run() {
    try {
        startProgram();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void startProgram() throws IOException {
    process = new ProcessBuilder(pathToFile).start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
}

public Process getProcess() {
    return process;
  }
}