当前位置: 代码迷 >> Java相关 >> 求助:仿DOS程序~万分感谢大哥哥大姐姐!
  详细解决方案

求助:仿DOS程序~万分感谢大哥哥大姐姐!

热度:132   发布时间:2006-12-30 11:05:12.0
求助:仿DOS程序~万分感谢大哥哥大姐姐!
1 编写一个CUI(dos字符界面)的Application。要求如下:
(1)运行程序后,仿照dos窗口模式,显示一个提示符(如:张三:\>),等待用户输入;
(2)如果用户输入常规dos命令,则显示执行结果。
常规命令包括:显示当前目录文件(dir 或者 dir /s)、切换目录(cd 目录)、删除文件(del 文件名)、查看文本文件(type 文件名)
(3)程序接受用户的输入后,要分辨出输入了何种命令,然后利用Java中的功能,实现与dos系统中相同的执行结果。
(4)如果输入了“exit”命令,则程序退出。否则,就等待用户输入并执行。
(5)对上述dos命令不熟悉者,请先在DOS窗口里学习一下。
2、请提交完整的程序以及程序说明文档(doc);
3、文档中包括设计思路、所有用到的类名称、关键代码、设计中遇到的问题以及最终解决方法或途径;
大哥哥大姐姐们我现在由于时间有限还有就是自身水平有限,无法完成上述设计,希望高手们能帮我一下~小女子万分感谢好心人~谢谢!
搜索更多相关的解决方案: 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]

以前写的一个
----------------解决方案--------------------------------------------------------
  相关解决方案