我正在处理管道输出流,以便在GUI中运行控制台命令;虽然我有点迷路。我正在尝试使用动作监听器重新执行主流程。但是我不能引用静态类。因此,基本上我想使用DispatchCommand JButtons ActionListener重新运行PipeShellCommand方法。我不确定要重构什么。谢谢,我知道我听起来像个新手,但这主要是针对Collage Club项目的。
import java.lang.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
public class PipeShellCommand implements Runnable
{
private PipedOutputStream pipeout;
private PipedOutputStream pipeerr;
String command = "cmd /K ";
public PipeShellCommand(String[] cmd) {
pipeout = null;
pipeerr = null;
}
@Override
public void run ()
{
exec ();
}
public void exec ()
{
// Class to redirect the process input stream to a piped output stream
class OutputMonitor implements Runnable
{
InputStream is;
PipedOutputStream pout;
public OutputMonitor ( InputStream i, PipedOutputStream p )
{
is = i;
pout = p;
}
@Override
public void run ()
{
try
{
int inputChar;
for ( ;; )
{
inputChar = is.read();
if ( inputChar == -1 )
{ break; }
if ( pout == null )
{
System.out.write ( inputChar );
}
else
{
pout.write ( inputChar );
}
}
if ( pout != null )
{
pout.flush ();
pout.close ();
}
else
{
System.out.flush();
}
}
catch ( Exception e ) { e.printStackTrace (); }
}
}
try
{
Runtime r = Runtime.getRuntime ();
JOptionPane.showConfirmDialog(null, "(uses quotes for system Commands eg: \"java -jar BuildTools.jar --Latest \" )");
String cmdargs = JOptionPane.showInputDialog("Type in Command:", "");
Process p = r.exec (command + cmdargs);
OutputMonitor out = new OutputMonitor ( p.getInputStream (), pipeout );
OutputMonitor err = new OutputMonitor ( p.getErrorStream (), pipeerr );
Thread t1 = new Thread ( out );
Thread t2 = new Thread ( err );
t1.start ();
t2.start ();
}
catch ( Exception e ) { e.printStackTrace (); }
}
public PipedInputStream getInputStream () throws IOException
{
pipeout = new PipedOutputStream ();
return new PipedInputStream ( pipeout );
}
public PipedInputStream getErrorStream () throws IOException
{
pipeerr = new PipedOutputStream ();
return new PipedInputStream ( pipeerr );
}
public void execInThread ()
{
Thread t = new Thread ( this );
t.start ();
}
public static JPanel getContentPane ( JTextArea ta )
{
JPanel p = new JPanel ( new BorderLayout () );
p.add ( new JScrollPane ( ta ), BorderLayout.CENTER );
p.setPreferredSize ( new Dimension ( 640,480 ) );
return p;
}
public static void main ( String[] arg )
{
class GuiUpdate implements Runnable
{
private PipedInputStream pin;
private PipedInputStream perr;
private JTextArea outputArea;
GuiUpdate ( JTextArea textArea, PipedInputStream in )
{
pin = in;
outputArea = textArea;
}
@Override
public void run ()
{
// Class to run on the event dispatch thread to update the GUI
class UpdateSwing implements Runnable
{
String update;
JTextArea swingTextArea;
public UpdateSwing ( JTextArea a, String s )
{
update = s;
swingTextArea = a;
}
public void run ()
{
outputArea.append ( update + "\n" );
}
}
try
{
// Read file before displaying
BufferedReader r = new BufferedReader ( new InputStreamReader ( pin ) );
String line;
for ( ;; )
{
line = r.readLine ();
if ( line == null ) { break; }
SwingUtilities.invokeLater ( new UpdateSwing ( outputArea, line ) );
Thread.yield ();
}
}
catch ( Exception e ) { e.printStackTrace (); }
}
}
// Create and invoke GUI
JFrame f = new JFrame ( "Minecraft GUI" );
f.setDefaultCloseOperation ( JFrame.ICONIFIED );
JTextArea textOutput = new JTextArea ();
textOutput.setBackground(Color.BLACK);
textOutput.setForeground(Color.red);
f.getContentPane().add ( getContentPane ( textOutput ) );
JPanel bottom = new JPanel ( new FlowLayout () );
JButton Exit = new JButton ( " Exit " );
Exit.addActionListener ( new ActionListener ( )
{
public void actionPerformed ( ActionEvent e )
{
System.exit ( 0 );
}
}
);
JButton DispatchCommand = new JButton ( " Command " );
DispatchCommand.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Need To Re-Run Post Process Here
}
});
bottom.add ( Exit );
bottom.add ( DispatchCommand );
f.add ( bottom, BorderLayout.SOUTH );
f.pack();
f.show ();
try
{
// Create command and setup the pipes
PipeShellCommand pipe = new PipeShellCommand ( arg );
PipedInputStream stdout_pipe = pipe.getInputStream ();
PipedInputStream stderr_pipe = pipe.getErrorStream ();
pipe.execInThread ( );
// Results
Thread t1 = new Thread ( new GuiUpdate ( textOutput, stdout_pipe ) );
Thread t2 = new Thread ( new GuiUpdate ( textOutput, stderr_pipe ) );
t1.start ();
t2.start ();
}
catch ( Exception e ) { e.printStackTrace (); }
}}