现在的位置: 首页 > 综合 > 正文

转载一个java仿dos的(截获方式)

2013年10月28日 ⁄ 综合 ⁄ 共 2214字 ⁄ 字号 评论关闭

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dos;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class java仿dos截获方式 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 java仿dos截获方式() {
        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 java仿dos截获方式();
    }
}

抱歉!评论已关闭.