又是Helolo。我正在尝试执行此操作:单击“搜索”按钮时,提示promt窗口打开。我尝试使用ProcessBuilder,没有出现错误,但是没有用。您能帮我吗?
package sys.tool;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//Создаем поля
public class MainWindow extends JFrame{
private JLabel lcn = new JLabel("Enter computer name:");
private JTextField icn = new JTextField("", 5);
private JButton search = new JButton("Search");
private JLabel lun = new JLabel("Enter user name:");
private JTextField iun = new JTextField("", 5);
private JLabel empty = new JLabel("");
public MainWindow (){
super("SysAdminTool");
this.setBounds(100, 100, 700 , 90);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
Container container = this.getContentPane();
container.setLayout(new GridLayout(3, 2 , 1, 1));
container.add(lcn);
container.add(icn);
container.add(lun);
container.add(iun);
container.add(empty);
search.addActionListener(new SearchEventListener());
container.add(search);
}
class SearchEventListener implements ActionListener{
public void actionPerformed (ActionEvent e){
Runtime rt = new Runtime();
rt.exec(new String[]{"cmd.exe", "/C","start"}); \\Here a make an event for button, to open cmd when I click the button.
}
}
}
每个Java应用程序都有一个Runtime类的实例,该实例允许该应用程序与应用程序运行所在的环境进行交互。当前运行时可以从getRuntime方法获取。
应用程序无法创建自己的此类的实例。
所以代替new Runtime()
,请尝试Runtime.getRuntime()