我正在尝试从另一个类访问一个类的全局变量,在执行操作期间,玻璃给我空值而不是输入值。
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);
}
}
我在执行时获取空值而不是插入值。请解决问题,我知道它非常基本但我是beginer.so plz帮助 感谢
答案 0 :(得分:0)
您正在panel
中创建sshConnection#Connection
的多个实例,其{,1}}字段为null
。
需要使用现有panel
实例的值。我建议简单地将它们作为参数传递
public void performConnection(String sshHost, String sshUser, int sshPort, String sshPass){
...
}
Java命名约定显示类以大写字母开头,例如SshConnection
方法以 lowecase 字母开头,例如performConnection
。阅读Naming Conventions
答案 1 :(得分:0)
在panel
班级中,shost
,suser
等,在您点击“开始审核”按钮之前,不会设置为任何内容。但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()
等等。