[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的功能
可惜不是你,陪我到最后