2

现在没有编译错误,值不会显示,只有消息“数字:”我已经在这里,除了之前显示“数字: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要求用户输入一个数字,到此为止 a String,然后用户应该点击playerregistrationJB,并且数字出现在 中PlayerRegistration_JPanel。此外,还有一个ProccessedJPanel我用 调用所有按钮的地方ActionListener,还有一个finalJpanel我在一个框架中拥有我的主要按钮的地方。我不知道问题出在哪里,因为 myJTextField是全局的(尽管我们在 Java 中没有像 GLOBAL VARIABLE 这样的东西)。

4

5 回答 5

4

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

于 2012-04-27T05:35:27.973 回答
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 ) );
      }
}
于 2012-04-27T05:52:44.043 回答
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);
}
于 2012-04-27T05:43:31.273 回答
2

JTextField useraccountnumberJTF的不是全球性的。它是您的Login_JPanel类的成员变量。Login_JPanel这意味着您的类的每个实例都有一个这样的文本字段。你怎么PlayerRegistration_JPanel知道你指的是哪个领域?

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

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

于 2012-04-27T05:37:07.440 回答
2

为什么你认为你可以直接useraccountnumberJTF在二等舱使用?

您正在混合 2 个类的变量。您不能在不参考第一类的情况下直接在第二类中使用一个类中定义的变量。您没有useraccountnumberJTF二等舱,因此它会在该课程中出错。您可以做的是找到一种方法将此变量值传递给第二类。

于 2012-04-27T05:37:22.117 回答