gpt4 book ai didi

java - 如何通过在 GUI 中单击 JButton 来启动 .exe 文件?

转载 作者:搜寻专家 更新时间:2023-11-01 01:57:37 25 4
gpt4 key购买 nike

我创建了一个带有 3 个 Jbutton 的 JFrame。我想要按钮启动位于同一文件夹中的不同 .exe 文件。这可能吗?如果是,我应该为 actionListener 写什么?另一方面,不是启动 exe 文件,而是可以使用 JButton 启动主类吗?如果是,我应该为 actionListener 写什么?

注意:.exe 文件是从 java 主程序创建的。

JButton button1 = new JButton("Program1"); //The JButton name.
frame.add(button1); //Add the button to the JFrame.
button1.addActionListener().... // how to launch the .exe file

提前致谢

最佳答案

Runtime.getRuntime().exec( ... );

或使用 ProcessBuilder 类。

您应该能够在网络上找到使用这些类的示例。

编辑:例如,要在 Windows 中启动记事本 exe,您可以这样做:

Process process = Runtime.getRuntime().exec( "cmd.exe /C start notepad" );

如果你想执行一个类,那么你需要像从命令行调用它一样调用 JVM。

关于java - 如何通过在 GUI 中单击 JButton 来启动 .exe 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4772289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com