简体   繁体   English

在类的jlabel中设置文本

[英]set text in jlabel from class

How to set JLabel text to output from runShellScript(unixCommand) 如何设置JLabel文本以从runShellScript(unixCommand)输出

String unixCommand = "echo testing"; 
runShellScript(unixCommand);
JLabel labelel = new JLabel("");
labelel.setText(runShellScript(unixCommand)); <----still error

this all my code 这是我所有的代码

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);
    }

}

What does runShellScript(unixCommand); runShellScript(unixCommand);是什么? return? 返回?

Check this 检查一下

public void setText(String text)

It only takes a String. 它只需要一个字符串。 It would also be helpful if you can tell the error you are facing. 如果您可以告诉您所面临的错误,这也将很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM