我想知道是否有人知道如何使JFrame关闭外部窗口。例如.bat文件。我的GUI窗口看起来像这样,
https://cdn.discordapp.com/attachments/339245512647770112/372569903070183425/unknown.png
我想在显示“关闭”的位置关闭通过“运行”按钮打开的文件。

        btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnNewButton.setBounds(375, 190, 120, 40);
    frame.getContentPane().add(btnNewButton);

    JButton btnStart = new JButton("Run");
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnStart.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                Desktop.getDesktop().open(new File("C:\\Users\\User\\Desktop\\Discord-Selfbot-master\\self-bot.bat"));
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    });


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

感谢您的时间和考虑。
-品牌0

最佳答案

您可以使用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;
  }
}

10-07 19:27
查看更多