找不到符号错误,空指针

时间:2012-04-27 05:28:54

标签: java swing jlabel jtextfield symbols

现在没有编译错误,值不会显示,只有消息“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);    
    }   
}

这是播放器注册面板

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中出现的号码{1}}。 此外,还有一个ProccessedJPanel我可以通过ActionListener调用我的所有按钮, 也是一个finalJpanel,我的主要在一个框架中。我不知道问题出在哪里,因为我的JTextField是全局的(尽管我们没有像Java中的GLOBAL VARIABLE这样的东西)。

5 个答案:

答案 0 :(得分:4)

您没有在类useraccountnumberJTF中声明PlayerRegistration_JPanel(它仅在Login_JPanel类中声明),但您在此行中调用它。这是你的错误。

答案 1 :(得分:3)

useraccountnumberJTF在哪里。你已经在另一个类中声明了它。要访问其他类属性,必须在类中创建其他类的对象,然后访问其他类属性。这两个类都必须在同一个包中。

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 ) );
      }
}

答案 2 :(得分:2)

您的JTextField useraccountnumberJTF不是全球性的。它是Login_JPanel类的成员变量。这意味着您的Login_JPanel类的每个实例都有一个这样的文本字段。您应该如何PlayerRegistration_JPanel知道您所指的字段?

如果要访问该字段,请在Login_JPanel的构造函数中传递PlayerRegistration_JPanel的实例,并询问该实例的字段。

这是一个相当基本的OO概念。也许重新阅读一些教程是很好的,例如this one

答案 3 :(得分:2)

为什么您认为可以在第二课中直接使用useraccountnumberJTF

您正在混合2个类的变量。您不能直接在第二个类中使用在一个类中定义的变量而不引用第一个类。你在第二课中没有useraccountnumberJTF所以它在那个课程中给出错误。你能做的就是找到一种方法将这个变量值传递给第二类。

答案 4 :(得分: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);
}