有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法查找符号错误,空指针

现在没有编译错误,值不会显示,只有消息“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,然后数字出现在PlayerRegistration_JPanel中。 此外,还有一个ProccessedJPanel,我通过ActionListener调用所有按钮, 也是一个finalJpanel,我在一个帧中有我的主帧。我不知道问题出在哪里,因为我的JTextField是全局的(尽管我们在Java中没有全局变量这样的东西)


共 (5) 个答案

  1. # 1 楼答案

    你的JTextField useraccountnumberJTF不是全局的。它是Login_JPanel类的成员变量。这意味着Login_JPanel类的每个实例都有一个这样的文本字段。你应该如何知道你指的是哪个领域

    如果想要访问该字段,请在PlayerRegistration_JPanel的构造函数中传递Login_JPanel的实例,并要求该实例提供其字段

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

  2. # 2 楼答案

    你的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 ) );
          }
    }
    
  3. # 3 楼答案

    为什么你认为你可以在第二节课上直接使用useraccountnumberJTF

    您正在混合两个类的变量。如果不引用第一个类,就不能在第二个类中直接使用在一个类中定义的变量。在第二个类中没有useraccountnumberJTF,所以它在该类中给出了错误。你能做的就是找到一种方法把这个变量值传递给第二类

  4. # 4 楼答案

    您必须声明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);
    }
    
  5. # 5 楼答案

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