2014-02-15 49 views
-1

我有一個訪問來自不同類的變量的問題。我有一個三個 classes.first是登錄JFrame,sencod JFrame可見後登錄成功fully.in這 JFrame中有一個JButton的多數民衆贊成的名字是秀「有關用戶的信息」 JButton btn = new JButton("inormation about user"); 然後按下該按鈕的新窗口可見此JFrame中包含所有userInformation如何從一個類訪問一個類變量到另一個或不同的類和不同的類變量

我試圖解決這個問題,但我失敗。 i訪問LoginForm類變量是usernametxt轉換成另一個類(Userform)在這個類 iam印usernametxt另一個類的細節是UserForm JLabel和它的suceefull。 但是這個(ProfileView1)class varibel在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); 
     } 
    } 
在此代碼IAM

使用Jlabels。所有空白Jlabels顯示userinformation其是存在於Database 我有一個問題,用於訪問該類varible另一個類(UserForm

此代碼:

public class UserForm extends JFrame implements ActionListener 
{ 
     public void actionPerformed(ActionEvent ae) 
     { 
     if(ae.getSource().equals(probtn)) 
    { 
     new ProfileView1();  
     } 
     } 
    } 

probtn按下然後另一個JFrame打開(ProfileView1) 。這個JFrame是開放的 我不知道如何訪問這個類可以(所有顯示用戶信息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類) 請幫我...............請。

我知道你明白我的問題,請幫助meeeeeeeeeeeeeeeeeee ............

回答

0

如果我正確理解你的問題,你想要的類窗體,以能夠訪問在該領域class 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 
    } 
} 

然後在用戶窗體,您現在可以參考非私有領域的ProfileView1的這個實例。例如

String firstName = proView1.firstlbl.getText(); 
String gender = proView1.genlbl.getText(); 

祝你好運!

相關問題