我有一个问题,从不同的类访问变量。我有一个三
classes.first是登录JFrame,sencod JFrame可见登录成功后完全。在此
Jframe有一个Jbutton,名称显示“关于用户的信息”
JButton btn = new JButton("inormation about user");
然后按此按钮可以看到新窗口,此Jframe包含所有userInformation
我正在尝试解决这个问题,但我失败了。
我在此类中访问LoginForm
usernametxt
类变量到另一个类(Userform
)
我打印usernametxt
另一个类UserForm
JLabel及其suceefull的详细信息。
但是这个(ProfileView1
)类变量在UserForm
类中是不可能的。所以帮助我;
这是我的代码:
public class LoginForm extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(vali(cuser, password))
{
UserForm class2 = new UserForm(LoginForm.this);
LoginForm.this.setVisible(false);
usernametxt.setText(null);
passwordtxt.setText(null);
}
}
}
此方法用于访问另一个类的JTextField
信息,这是我正在使用的方法;
public String getJTextField()
{
return usernametxt.getText();
}
这是我的第二堂课:
public class UserForm extends JFrame implements ActionListener
{
JLabel albl;
private LoginForm log;
JButton probtn;
public UserForm(LoginForm log)
{
this. log = log;
albl = new JLabel("");
albl.setText(log.getJTextField());
albl.setBounds(800, 10, 300, 30);
add(albl);
probtn = new JButton("inormation about user");
probtn.setBounds(400, 370, 300, 170);
add(probtn);
}
}
这是我的第三堂课:
public class ProfileView1 extends JFrame implements ActionListener
{
JLabel firstlbl,lastlbl,userlbl,passlbl,genlbl,doblbl,typelbl,conlbl,ci1lbl,stlbl,moblbl,pinlbl,emlbl,qlbl,anslbl;
JLabel flbl,llbl,ulbl,plbl,glbl,dlbl,tlbl,clbl,citlbl,slbl,mlbl,pilbl,elbl,qulbl,anlbl;
public ProfileView1()
{
setVisible(true);
setBounds(360,30,700,600);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLayout(null);
Font f = new Font("Arial",Font.PLAIN,13);
firstlbl = new JLabel("First Name :");
firstlbl.setFont(f);
lastlbl = new JLabel("Last Name :");
lastlbl.setFont(f);
userlbl = new JLabel("Username :");
userlbl.setFont(f);
passlbl = new JLabel("Password :");
passlbl.setFont(f);
genlbl = new JLabel("Gender :");
genlbl.setFont(f);
doblbl = new JLabel("Date of Birth :");
doblbl.setFont(f);
typelbl = new JLabel("User Type :");
typelbl.setFont(f);
conlbl = new JLabel("Country :");
conlbl.setFont(f);
ci1lbl = new JLabel("City :");
ci1lbl.setFont(f);
stlbl = new JLabel("State :");
stlbl.setFont(f);
moblbl = new JLabel("Mobile no :");
moblbl.setFont(f);
pinlbl = new JLabel("Pin no :");
pinlbl.setFont(f);
emlbl = new JLabel("E-mail :");
emlbl.setFont(f);
qlbl = new JLabel("Security Question :");
qlbl.setFont(f);
anslbl = new JLabel("Answer :");
anslbl.setFont(f);
flbl = new JLabel("");
flbl.setFont(f);
llbl = new JLabel("");
llbl.setFont(f);
ulbl = new JLabel("");
ulbl.setFont(f);
plbl = new JLabel("");
plbl.setFont(f);
glbl = new JLabel("" );
glbl.setFont(f);
dlbl = new JLabel("");
dlbl.setFont(f);
tlbl = new JLabel("");
tlbl.setFont(f);
clbl = new JLabel("");
clbl.setFont(f);
citlbl = new JLabel("");
citlbl.setFont(f);
slbl = new JLabel("");
slbl.setFont(f);
mlbl = new JLabel("");
mlbl.setFont(f);
pilbl = new JLabel("");
pilbl.setFont(f);
elbl = new JLabel("");
elbl.setFont(f);
qulbl = new JLabel("");
qulbl.setFont(f);
anlbl = new JLabel("");
anlbl.setFont(f);
firstlbl.setBounds(10,100,100,20);
lastlbl.setBounds(10,130,100,20);
userlbl.setBounds(10,160,100,20);
passlbl.setBounds(10,190,100,20);
genlbl.setBounds(10,220,100,20);
doblbl.setBounds(10,250,100,20);
typelbl.setBounds(10,280,100,20);
conlbl.setBounds(10,310,100,20);
ci1lbl.setBounds(230,310,100,20);
stlbl.setBounds(430,310,100,20);
moblbl.setBounds(10,340,100,20);
pinlbl.setBounds(230,340,100,20);
emlbl.setBounds(10,370,100,20);
qlbl.setBounds(10,400,170,20);
anslbl.setBounds(10,430,100,20);
flbl.setBounds(90,100,100,20);
llbl.setBounds(90,130,100,20);
ulbl.setBounds(90,160,100,20);
plbl.setBounds(90,190,100,20);
glbl.setBounds(90,220,100,20);
dlbl.setBounds(90,250,100,20);
tlbl.setBounds(90,280,100,20);
clbl.setBounds(90,310,100,20);
citlbl.setBounds(310,310,100,20);
slbl.setBounds(510,310,100,20);
mlbl.setBounds(90,340,100,20);
pilbl.setBounds(310,340,100,20);
elbl.setBounds(90,370,100,20);
qulbl.setBounds(90,400,100,20);
anlbl.setBounds(90,430,100,20);
add(firstlbl);
add(lastlbl);
add(userlbl);
add(passlbl);
add(genlbl);
add(doblbl);
add(typelbl);
add(conlbl);
add(ci1lbl);
add(stlbl);
add(moblbl);
add(pinlbl);
add(emlbl);
add(anslbl);
add(qlbl);
add(flbl);
add(llbl);
add(ulbl);
add(plbl);
add(glbl);
add(dlbl);
add(tlbl);
add(clbl);
add(citlbl);
add(slbl);
add(mlbl);
add(pilbl);
add(elbl);
add(qulbl);
add(anlbl);
}
}
在此代码中使用Jlabels
。所有空白Jlabels
显示Database
上显示的用户信息
我在另一个类(UserForm
)
此代码:
public class UserForm extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(probtn))
{
new ProfileView1();
}
}
}
此probtn
按下,然后另一个JFrame
打开(ProfileView1
)。此JFrame
已打开
我不知道如何访问此类viraible(所有显示用户信息JLabels
)
我尝试这种类型:
public class UserForm extends JFrame implements ActionListener
{
private LoginForm log;
private ProfileView1 pv;
public UserForm(LoginForm log, ProfileView1 pv)
{
this.log = log;
this.pv = pv;
}
}
但错误显示另一个类(LoginForm
):
这是显示错误的错误代码:
------> UserForm class2 = new UserForm(LoginForm.this,ProfileView.this);
LoginForm.this.setVisible(false);
setVisible(false);
它不起作用,实际上这个类(ProfileView1
)打开并在对此按钮执行操作后显示所有用户详细信息,probtn
上的此按钮名称为UserForm
。
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(probtn))
{
new ProfileView1();
}
}
我希望显示所有userdetails只有一个动作(按钮出现在UserForm
类)
请帮帮我...............拜托。
我知道你理解我的问题请帮助meeeeeeeeeeeeeeeeeeeeeee ............
答案 0 :(得分:0)
如果我正确理解您的问题,您希望类UserForm能够访问类ProfileView1中的字段。
首先,您需要UserForm中一个引用您的ProfileView1实例的字段。
public class UserForm extends JFrame implements ActionListener
{
private ProfileView1 proView1; //*** add this line
JLabel albl;
创建ProfileView1实例时,需要设置此字段
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(probtn))
{
this.proView1 = new ProfileView1(); //*** this line is changed
}
}
然后在UserForm中,您现在可以引用此ProfileView1实例中的非私有字段。例如
String firstName = proView1.firstlbl.getText();
String gender = proView1.genlbl.getText();
祝你好运!