java中
JPanel如何添加cmd.exe

解决方案 »

  1.   

    事件触发机制Runtime.getRuntime().exec(......);
      

  2.   


    String cmd = "cmd命令";
    Process p = Runtime.getRuntime().exec(cmd);
    //这个也是从这个论坛学习到的,呵呵!
      

  3.   

    各位理解错了,我的意思是
    String cmd = "cmd命令";
    Process p = Runtime.getRuntime().exec(cmd);
    它会跳出一个界面,我想把这个界面放到jPanel中
    怎么放啊
      

  4.   

    我只是想在JPanel中直接调用cmd.exe 
    并且把它放在JPanel中
      

  5.   

    事件触发机制Runtime.getRuntime().exec(......);
      

  6.   

    辛辛苦苦帮你找到了这个网站,自己仔细看下,将里边的内容变通一下应该能实现你要的效果
    http://blog.csdn.net/wh_xiexing/archive/2007/12/05/1919009.aspx
      

  7.   

    只可以模拟吗,不可以直接把它放到JPanel中吗
      

  8.   

    你的意思是,在JPanel面板中可以直接调出控制台吗?——————————————————————
    如果是以上意思
    添加个按钮,事件触发时如下代码执行:Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler file://C:\\WINDOWS\\system32\\cmd.exe");测试成功。
      

  9.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;import javax.swing.*;public class MyDOS extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = -5418344602348249043L;
    private JPanel pup = new JPanel();
    private JPanel pdown = new JPanel();
    private JTextField txtCommand = new JTextField(45);
    private JTextArea txtContent = new JTextArea();
    private JButton btnExec = new JButton("Execute"); public MyDOS() {
    // 指定框架的布局管理器
    setLayout(new BorderLayout());
    // 设置文本框,文本域字体
    txtCommand.setFont(new Font("", Font.BOLD, 13));
    txtContent.setFont(new Font("", Font.BOLD, 13));
    // 指定面板的布局
    pup.setLayout(new BorderLayout());
    pdown.setLayout(new FlowLayout()); // 将文本域添加导面板中
    pup.add(txtContent);
    // 为文本域添加滚动条
    pup.add(new JScrollPane(txtContent));
    // 将文本框,按钮添加到面板中
    pdown.add(txtCommand);
    pdown.add(btnExec); // 添加面板到框架中
    this.add(pup, BorderLayout.CENTER);
    this.add(pdown, BorderLayout.SOUTH); // 设置事件 // 添加按钮事件
    btnExec.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String s;
    // 获取文本框中的命令
    String command = txtCommand.getText().trim();
    Process process;
    try {
    process = Runtime.getRuntime().exec("cmd /c " + command);
    // 截获被调用程序的DOS运行窗口的标准输出
    BufferedReader br = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
    while ((s = br.readLine()) != null)
    txtContent.append(s + "\r\n"); process.waitFor();
    txtCommand.setText("");
    } catch (IOException e1) {
    e1.printStackTrace();
    } catch (InterruptedException e2) {
    e2.printStackTrace();
    }
    }
    }); // 添加键盘Enter事件
    txtCommand.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) {
    // 当按下回车时
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    String s;
    // 获取文本框中的命令
    String command = txtCommand.getText().trim();
    Process process;
    try {
    process = Runtime.getRuntime()
    .exec("cmd /c " + command);
    // 截获被调用程序的DOS运行窗口的标准输出
    BufferedReader br = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
    while ((s = br.readLine()) != null)
    txtContent.append(s + "\r\n"); process.waitFor();
    txtCommand.setText("");
    } catch (IOException e1) {
    e1.printStackTrace();
    } catch (InterruptedException e2) {
    e2.printStackTrace();
    }
    }
    } public void keyReleased(KeyEvent e) {
    } public void keyTyped(KeyEvent e) {
    }
    });
    }
    public static void main(String[] args) {
    MyDOS frame = new MyDOS();
    frame.setTitle("MyDOS");
    frame.setSize(666, 444);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  10.   

    我还要把控制台 放到JPanel面板上
      

  11.   

    http://blog.csdn.net/wh_xiexing/archive/2007/12/05/1919009.aspx
    这里的实现是读取cmd进程里边的输入输出啊,只是外部表现不同而已,应该能满足你的要求吧
      

  12.   

    这个我看过了,有没有能运行的demo 啊