java dos如何实现行对齐_转载一个java仿dos的(截获方式)

/*

* 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截获方式();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值