0

我正在尝试从另一个类访问一个类的全局变量,玻璃在执行操作期间给我空值而不是输入值`

package com.audit;

    public class panel extends JPanel {
     String shost;
     String suser;
     String spass;
     int sport;
     public int getsport() {
           return this.sport;
        }
     public String getshost() {
           return this.shost;
        }
     public String getsuser() {
           return this.suser;
        }
     public String getspass() {
           return this.spass;
        }
    public panel(){
        Dimension size = getPreferredSize();
        size.width = 680;
        size.height = 600;
        setPreferredSize(size);
        setBorder(BorderFactory.createTitledBorder("Linux Audit"));
        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();


        JLabel labelhost = new JLabel("Host    ");
        JLabel labeluser = new JLabel("User name    ");
        JLabel labelpass = new JLabel("Password    ");
        JLabel labelport = new JLabel("Port    ");
        final JTextField host = new JTextField(15);
        final JTextField user = new JTextField(15);
        final JTextField pass=(JTextField)new JPasswordField(15);
        final JTextField port = new JTextField(15);
        final JButton start = new JButton("Start Audit");
        //layout design
        gc.anchor = GridBagConstraints.LINE_END;
        gc.weightx = 0.5;
        gc.weighty = 0.5;
        gc.gridx=0;
        gc.gridy=0;
        add(labelhost,gc);
        gc.gridx=0;
        gc.gridy=1;
        add(labeluser,gc);
        gc.gridx=0;
        gc.gridy=2;
        add(labelpass,gc);
        gc.gridx=0;
        gc.gridy=3;
        add(labelport,gc);
        gc.anchor = GridBagConstraints.LINE_START;
        gc.gridx=1;
        gc.gridy=0;
        add(host,gc);
        gc.gridx=1;
        gc.gridy=1;
        add(user,gc);
        gc.gridx=1;
        gc.gridy=2;
        add(pass,gc);
        gc.gridx=1;
        gc.gridy=3;
        add(port,gc);
        gc.anchor = GridBagConstraints.FIRST_LINE_START;
        gc.weighty=10;
        gc.gridx=1;
        gc.gridy=4;
        add(start,gc);
        //startaudit action

    start.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {

                String shost = host.getText();
                String suser = user.getText();
                String spass = pass.getText();
                String sportb = port.getText();
                int sport = Integer.parseInt(sportb);

                sshConnection s = new sshConnection();
                s.Connection();
                /*System.out.println(shost);
                System.out.println(suser);
                System.out.println(spass);
                System.out.println(sport);*/

            }


        });


    }

}

在上面我试图获取全局变量的值

 String shost;
 String suser;
 String spass;
 int sport; 

在另一个班级

///another class

public class sshConnection {

    public void Connection(){
         String sshhost = new panel().getshost();
         String sshuser = new panel().getsuser();
         int sshport = new panel().getsport();
         String sshpass = new panel().getspass();
         System.out.println(sshhost);
         System.out.println(sshuser);
         System.out.println(sshport);
         System.out.println(sshpass);
    }
}

我在执行时得到空值而不是插入值。请解决问题,我知道它非常基本,但我是初学者。所以请帮助谢谢

4

2 回答 2

0

您正在创建其字段为的panel多个实例。sshConnection#Connectionnull

panel需要使用现有实例中的值。我建议只是将它们作为参数传递

public void performConnection(String sshHost, String sshUser, int sshPort, String sshPass){
 ...
}

Java 命名约定表明类以大写字母开头,例如SshConnection,方法以小写字母开头,例如performConnection. 阅读命名约定

于 2013-09-13T23:31:38.617 回答
0

panel类中shostsuser, 等,在您单击 Start Audit 按钮之前不会设置为任何内容。但是Connection构造了4个 panel对象。这些与panel您单击按钮的对象不同。我认为你想要做的是改变你的 Connection 方法,以便它接受一个panel参数:

public void Connection(panel p){
    String sshhost = p.getshost();
    String sshuser = p.getsuser();
    ...

并将s.Connection监听器内部的调用更改为

s.Connection (panel.this);

所以它Connection知道panel它应该与什么一起工作。

或者,更好的设计可能是Connection将三个字符串和整数作为参数,而根本不关心getshost()等。

于 2013-09-13T23:33:59.423 回答