2012-04-27 18 views
2

現在沒有編譯錯誤,該值不會顯示,只有消息「number:」 我已經在這裏,除了它之前顯示「number:NULL」之前的例外,但我想我到了那裏......謝謝大家。我一直在讀線程了大約一個星期,但我學到了更多今晚你建議無法找到符號錯誤,空指針

登錄類

public class Login_JPanel extends JPanel 
{ 
    JLabel welcomeMessage, 
    mainPicture; 

    JButton playerregistrationJB = new JButton ("Player Registration"); 

    JLabel userAccountNumberJLabel = new JLabel("<HTML><CENTER><FONT SIZE=6 
    COLOR=YELLOW>Please, enter your account number below and then click on player 
    registration to continue </COLOR></CENTER></HTML>"); 

    JTextField useraccountnumberJTF = new JTextField(); 

    public Login_JPanel() 
    { 
     //======================================================== 
     //SET UP USERNAME JLABEL AND TEXT FIELD 
     //======================================================== 
     add(userAccountNumberJLabel); 
     userAccountNumberJLabel.setBounds(322, 335, 300, 155); 

     add(useraccountnumberJTF); 
     useraccountnumberJTF.setBounds (330, 500, 90, 25); 

     playerregistrationJB.setBounds(350, 600, 325, 75); 
     playerregistrationJB.setFont(new Font("Broadway", Font.BOLD, 30)); 
     playerregistrationJB.setForeground(Color.red); 
     playerregistrationJB.setBackground(Color.YELLOW); 
     add(playerregistrationJB);   

     add(welcomeMessage = new JLabel("Welcome!!")); 
     welcomeMessage.setBounds(0,0,50,23); 

     add(mainPicture = new JLabel(new ImageIcon("henkidama.jpg"))); 
     mainPicture.setBounds(0,0,50,50); 

     setLayout(null); 
     setBounds(0,0,1000,800);  
    } 
} 

這是playerregistration面板

public class PlayerRegistration_JPanel extends JPanel 
{ 

    Login_JPanel loginJP = new Login_JPanel(); 

    JLabel welcomeMessage, 
    mainPicture; 

    JLabel useraccountnumberJL = new JLabel(); 

    JButton submitJB = new JButton ("Submit");  

    public PlayerRegistration_JPanel() 
    { 
     add(useraccountnumberJL); 
     useraccountnumberJL.setText("number: " + 
         loginJP.useraccountnumberJTF.getText());  
     useraccountnumberJL.setBounds(100, 75, 625, 200); 
     useraccountnumberJL.setFont(new Font("Broadway", Font.BOLD, 18)); 

     submitJB.setBounds(350, 600, 325, 75); 
     submitJB.setFont(new Font("Broadway", Font.BOLD, 30)); 
     submitJB.setForeground(Color.red); 
     submitJB.setBackground(Color.YELLOW); 
     add(submitJB); 

     add(welcomeMessage = new JLabel("Welcome to Building Another Panel!!")); 
     welcomeMessage.setBounds(0,0,50,23); 

     add(mainPicture = new JLabel(new ImageIcon("henkidama.jpg"))); 
     mainPicture.setBounds(0,0,50,50); 

     setLayout(null); 
     setBounds(0,0,1000,800);   
    } 
} 

有一個JLabel要求用戶輸入一個號碼,直到這一點一個String,那麼用戶應該點擊playerregistrationJB,號碼出現在PlayerRegistration_JPanel。 此外,還有一個ProccessedJPanel,我撥打我的所有按鈕ActionListener, 也finalJpanel我在哪裏我的主要在一個框架。我不知道問題在哪裏,因爲我的JTextField是全球性的(儘管我們沒有像Java中的GLOBAL VARIABLE這樣的東西)。

+0

請注意編輯並指出我,如果我錯了某處!此外,你剛剛在你的'PlayerRegistration_JPanel'中初始化你的'Login_JPanel'類的對象來訪問'useraccountnumberJTF.getText()',像'Login_JPanel loginPanel = new Login_JPanel(); loginPanel.useraccountnumberJTF.getText();'? – 2012-04-27 05:40:04

+0

只需在你的'Login_JPanel'類裏面聲明'JTextField'像這樣'public static JTextField useraccountnumberJTF = new JTextField();'現在在你的'PlayerRegistration_JPanel'類裏面就像這樣'Login_JPanel.useraccountnumberJTF.getText()'來訪問它。這將做:-) – 2012-04-27 13:08:56

回答

4

您沒有在類PlayerRegistration_JPanel(它只在​​類中聲明)中聲明useraccountnumberJTF,但您在此行中調用它。這是你的錯誤。

+0

我已經嘗試聲明它例如在我的playerregistration jpanel寫這個:JTextField useraccountnumberJTF; 但後來我在PlayerRegistration_JPanel的線程「main」java.lang.NullPointerException \t中得到空指針異常。 (PlayerRegistration_JPanel.java:38)在同一行 – Grecia 2012-04-27 05:55:04

+0

@Grecia當然,那麼你仍然需要初始化它。要獲得您想要的功能,您應該確保您的代碼可以訪問您的用戶界面中使用的特定字段,正如我在我的回答 – Robin 2012-04-27 06:09:27

2

您的JTextField useraccountnumberJTF不是全球性的。它是您的​​類的成員變量。這意味着您的​​類的每個實例都有一個這樣的文本字段。 PlayerRegistration_JPanel你應該怎麼知道你指的是哪個領域?

如果要訪問該字段,請在構造函數PlayerRegistration_JPanel中傳遞​​的實例,然後向該實例請求該字段。

這是一個相當基本的面向對象的概念。也許重新閱讀一些教程很好,如this one例如

2

爲什麼你認爲你可以直接在第二課中使用useraccountnumberJTF

您正在混合2個類別的變量。你不能在第二類中直接使用定義在一個類中的變量而不參考第一類。你沒有第二課的useraccountnumberJTF,所以它在該課上給出錯誤。你可以做的是找到一種方法將這個變量值傳遞給第二類。

2

您必須聲明useraccountnumberJTF

import java.awt.Color; 
import java.awt.Font; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Login_JPanel extends JPanel { 
    JLabel welcomeMessage, mainPicture; 

    JButton playerregistrationJB = new JButton("Player Registration"); 
    JLabel userAccountNumberJLabel = new JLabel("<HTML><CENTER><FONT SIZE=6 COLOR=YELLOW>Please, enter your account number below and then click on playerregistration to continue </COLOR></CENTER></HTML>"); 
    JTextField useraccountnumberJTF = new JTextField(); 

    public Login_JPanel() { 
     // ======================================================== 
     // SET UP USERNAME JLABEL AND TEXT FIELD 
     // ======================================================== 
     add(userAccountNumberJLabel); 
     userAccountNumberJLabel.setBounds(322, 335, 300, 155); 

     add(useraccountnumberJTF); 
     useraccountnumberJTF.setBounds(330, 500, 90, 25); 

     playerregistrationJB.setBounds(350, 600, 325, 75); 
     playerregistrationJB.setFont(new Font("Broadway", Font.BOLD, 30)); 
     playerregistrationJB.setForeground(Color.red); 
     playerregistrationJB.setBackground(Color.YELLOW); 
     add(playerregistrationJB); 

     add(welcomeMessage = new JLabel("Welcome!!")); 
     welcomeMessage.setBounds(0, 0, 50, 23); 

     add(mainPicture = new JLabel(new ImageIcon("henkidama.jpg"))); 
     mainPicture.setBounds(0, 0, 50, 50); 

     setLayout(null); 
     setBounds(0, 0, 1000, 800); 
    } 

    public String getUseraccountnumberJTFText() { 
     return useraccountnumberJTF.getText(); 
    } 
} 

PlayerRegistration_JPanel:

import java.awt.Color; 
import java.awt.Font; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class PlayerRegistration_JPanel extends JPanel 
{ 
    JLabel welcomeMessage, 
      mainPicture; 
    JLabel useraccountnumberJL = new JLabel(); 
    JButton submitJB = new JButton ("Submit");  

    public PlayerRegistration_JPanel(Login_JPanel panel) 
    { 
     add(useraccountnumberJL); 
     useraccountnumberJL.setText(panel.getUseraccountnumberJTFText()); 
     useraccountnumberJL.setBounds(100, 75, 625, 200); 
     useraccountnumberJL.setFont(new Font("Broadway", Font.BOLD, 18)); 

     submitJB.setBounds(350, 600, 325, 75); 
     submitJB.setFont(new Font("Broadway", Font.BOLD, 30)); 
     submitJB.setForeground(Color.red); 
     submitJB.setBackground(Color.YELLOW); 
     add(submitJB); 

     add(welcomeMessage = new JLabel("Welcome to Building Another Panel!!")); 
     welcomeMessage.setBounds(0,0,50,23); 

     add(mainPicture = new JLabel(new ImageIcon("henkidama.jpg"))); 
     mainPicture.setBounds(0,0,50,50); 

     setLayout(null); 
     setBounds(0,0,1000,800); 
    } 
} 

並請閱讀本:http://java.about.com/od/javasyntax/a/nameconventions.htm

編輯:

的類的聲明應該是這樣:

private Login_JPanel loginPanel; 

private void theMethodWhoDeclareLoginJPanel() { 
    loginPanel = new Login_JPanel(); 
} 

private void theMethodWhoDeclarePlayerRegistrationJPanel() { 
    new PlayerRegistration_JPanel(loginPanel); 
} 
+0

中所解釋的,我嘗試這樣做時,出現此錯誤。 PlayerRegistration_JPanel.java:37:錯誤:非靜態方法getUseraccountnumberJTFText()不能從靜態上下文中引用 – Grecia 2012-04-27 06:02:36

+0

soo ...'PlayerRegistration_JPanel'是靜態的嗎? – Neifen 2012-04-27 06:15:39

+0

no> _>據說這是這個程序的重點,它是一個非靜態方法的程序...:/ JGrasp這是一個痛苦的屁股 – Grecia 2012-04-27 06:27:07

3

您的useraccountnumberJTF在哪裏?你已經在另一個班上申報了。爲了訪問其他類屬性,您必須在您的類中創建其他類的oject,然後訪問其他類屬性。而且這兩個類都必須在同一個包中。

public class PlayerRegistration_JPanel extends JPanel 
{ 
    Login_JPanel login = new Login_JPanel(); 
    public PlayerRegistration_JPanel() 
    { 
      add(useraccountnumberJL); 
      useraccountnumberJL.setText(login.useraccountnumberJTF.getText()); 
      useraccountnumberJL.setBounds(100, 75, 625, 200); 
      useraccountnumberJL.setFont(new Font("Broadway", Font.BOLD, 18)); 
     } 
} 
+0

現在它不會給我任何錯誤,Jbutton的工作原因是因爲顯示了下一個PlayerRegistration_JPane,我可以看到圖片,但是沒有JLabel顯示數字......感謝您的幫助 – Grecia 2012-04-27 06:17:54

+1

您是否從textfield獲得任何數據?我的意思是從textfield和tyr中獲取文本以在控制檯上顯示它..這可能是很好的練習練習.. – 2012-04-27 06:26:37

+0

虐待嘗試,謝謝 – Grecia 2012-04-27 07:02:51