我是靠谱客的博主 爱笑白开水,最近开发中收集的这篇文章主要介绍java cmd 交互,求助,我想做一个jswing界面和cmd进行交互的程序,有个问题,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JButton;

import javax.swing.JScrollPane;

import javax.swing.JTable;

public class tc extends JFrame implements ActionListener{

private JPanel jContentPane = null;

private JTextField jTextField = null;

private JButton jButton = null;

private String str=null;

private JScrollPane jScrollPane = null;

private JTextArea jTextArea = null;

/**

* This is the default constructor

*/

public tc() {

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setSize(500, 300);

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

this.setResizable(false);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

start();

}

private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(null);

jContentPane.add(getJTextField(), null);

jContentPane.add(getJButton(), null);

jContentPane.add(getJScrollPane(), null);

}

return jContentPane;

}

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

jTextField.setBounds(new java.awt.Rectangle(83,215,296,29));

}

return jTextField;

}

private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButton.setBounds(new java.awt.Rectangle(413,215,52,27));

jButton.addActionListener(this);

}

return jButton;

}

private JScrollPane getJScrollPane() {

if (jScrollPane == null) {

jScrollPane = new JScrollPane();

jScrollPane.setBounds(new java.awt.Rectangle(40,17,392,157));

jScrollPane.setViewportView(getJTextArea());

}

return jScrollPane;

}

private JTextArea getJTextArea() {

if (jTextArea == null) {

jTextArea = new JTextArea();

}

return jTextArea;

}

public void start(){

try {

Process process = Runtime.getRuntime().exec("cmd");

PrintWriter writer = new PrintWriter(process.getOutputStream());

new CommandThread(writer,jTextArea,str).start();

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

String s = null;

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

jTextArea.append(s+"n");

}

Thread.currentThread().interrupt();

} catch (Exception e) {

e.printStackTrace();

}

}

static class CommandThread extends Thread{

PrintWriter writer;

JTextArea jta=null;

String s=null;

CommandThread(PrintWriter writer,JTextArea ta,String str){

this.writer = writer;

this.jta = ta;

this.s=str;

this.setDaemon(true);

}

@Override

public void run() {

String cmd = null;

if((cmd = s) != null){

writer.println(cmd);

writer.flush();

}

}

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

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

str=jTextField.getText();

jTextField.setText("");

if("".equals(str.trim())){

JOptionPane.showMessageDialog(null, "message null");

return;

}

jTextArea.append(str+"n");

}

}

}

最后

以上就是爱笑白开水为你收集整理的java cmd 交互,求助,我想做一个jswing界面和cmd进行交互的程序,有个问题的全部内容,希望文章能够帮你解决java cmd 交互,求助,我想做一个jswing界面和cmd进行交互的程序,有个问题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部