java在dos中如何测试_[求助]怎么在java中模拟dos命令行

[CODE]/*

* Test.java

*

* Created on 2006年12月21日, 下午3:53

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package testCMD;

/**

*

* @author lbf

*/

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Test extends JFrame implements KeyListener,ActionListener{

private JTextArea content;

private JButton start,exit;

private PrintWriter pw;

private BufferedReader br;

private Process p;

/** Creates a new instance of Test */

public Test() {

initWindow();

}

private void initWindow(){

content=new JTextArea();

start=new JButton("开始");

exit=new JButton("退出");

content.setFont(new Font("宋体",Font.BOLD,15));

JPanel center=new JPanel(new BorderLayout());

JPanel bottom=new JPanel();

center.add(new JScrollPane(content),BorderLayout.CENTER);

bottom.add(start);

bottom.add(exit);

content.addKeyListener(this);

start.addActionListener(this);

exit.addActionListener(this);

Container c=this.getContentPane();

c.add(center,BorderLayout.CENTER);

c.add(bottom,BorderLayout.SOUTH);

this.setSize(500,500);

this.setLocationRelativeTo(null);

this.setVisible(true);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we){

if(p!=null){

p.destroy();

}

System.exit(0);

}

});

}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

String s=getInput();

pw.println(s+"\r");

pw.flush();

}

}

private String getInput(){

String input=content.getText();

StringBuffer sb=new StringBuffer(input);

sb.replace(sb.length()-1,sb.length()," ");

int index=sb.lastIndexOf("\n");

String sub=input.substring(index);

return sub;

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==start){

start.setEnabled(false);

initCMD();

} else if(e.getSource()==exit){

if(p!=null){

p.destroy();

}

System.exit(0);

}

}

private void initCMD(){

try{

p=Runtime.getRuntime().exec("cmd.exe");

br=new BufferedReader(new InputStreamReader(p.getInputStream()));

pw=new PrintWriter(new OutputStreamWriter(p.getOutputStream()));

new Thread(new Runnable(){

public void run(){

try{

String output="";

while((output=br.readLine())!=null){

content.append(output+"\n");

content.setCaretPosition(content.getText().length());

}

System.exit(0);

} catch(Exception exe){

exe.printStackTrace();

}

}

}).start();

} catch(Exception exe){

exe.printStackTrace();

}

}

public static void main(String[] args)throws Exception {

new Test();

}

}[/CODE]

你看看是不是你想实现的功能

这个只是简单的调用了CMD的功能

----------------解决方案--------------------------------------------------------

执行的时候,光标不能乱动

都是计算好了的,你直接输入命令就可以了

你要输入命令的时候,光标一定要在最后面,默认它自动会跳到最后面去

----------------解决方案--------------------------------------------------------

不错,强,辛苦了!

----------------解决方案--------------------------------------------------------

如果对他有帮助并且他能所有进步,辛苦也值得

----------------解决方案--------------------------------------------------------

给你加个精得了,我还没给别人加过精呢

----------------解决方案--------------------------------------------------------

加个精也好,以后找起来方便

我以前写的例子都找不到了

----------------解决方案--------------------------------------------------------

加精了,真是受益良多

----------------解决方案--------------------------------------------------------

受益非浅!

3Q ~~~

----------------解决方案--------------------------------------------------------

千里,太厉害了。学习一下。

----------------解决方案--------------------------------------------------------

昨天下午5点左右,公寓突然停电,晕了.

谢谢千里冰封大哥了,我好好看看!

----------------解决方案--------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值