如何设置JLabel
要输出的文本runShellScript(unixCommand)
String unixCommand = "echo testing";
runShellScript(unixCommand);
JLabel labelel = new JLabel("");
labelel.setText(runShellScript(unixCommand)); <----still error
这是我所有的代码
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
public class swcls extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void runShellScript(String unixCommand) throws IOException, InterruptedException {
String hostname = "192.168.3.101";
String username = "root";
String password = "password";
boolean isAuthenticated = false;
try
{
Connection conn = new Connection(hostname);
conn.connect();
isAuthenticated=conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
Session sess = conn.openSession();
//sess.execCommand("cd /;ls -l");
sess.execCommand(unixCommand);
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
InputStreamReader insrout=new InputStreamReader(stdout);
InputStreamReader insrerr=new InputStreamReader(stderr);
BufferedReader stdoutReader = new BufferedReader(insrout);
BufferedReader stderrReader = new BufferedReader(insrerr);
while (true)
{
String line = stdoutReader.readLine();
if (line == null)
{
break;
}
System.out.println(line);
}
while (true)
{
String line = stderrReader.readLine();
if (line == null)
{ break;}
System.out.println(line);
}
sess.close();
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
swcls frame = new swcls();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws InterruptedException
* @throws IOException
*/
public swcls() throws IOException, InterruptedException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
String unixCommand = "echo testing";
runShellScript(unixCommand);
JLabel labelel = new JLabel("");
labelel.setText(runShellScript(unixCommand));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(110)
.addComponent(labelel)
.addContainerGap(268, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(74)
.addComponent(labelel)
.addContainerGap(164, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
}
}