简体   繁体   English

Java GUI变量问题

[英]Java GUI Variables Problems

I seem to have set up something wrong in the action listener for the Create Button handler. 我似乎在动作侦听器中为“创建按钮”处理程序设置了一些错误。 When I tried to get the value of the text field nameTF I get a null pointer error. 当我尝试获取文本字段nameTF的值时,出现空指针错误。 I tried to imitate your code for the calculator, and the Exit Button Handler works well. 我试图为计算器模仿您的代码,并且退出按钮处理程序运行良好。 I know that pressing the Create button invokes the handler but the statement 我知道按下创建按钮会调用处理程序,但是语句

inName = nameTF.getText(); inName = nameTF.getText();

gives the error message. 给出错误信息。

The complete text of the listener is 侦听器的完整文本是

private class CreateButtonHandler 
    implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        String inName, inType; //local variables
        int inAge;
        Dog arf;
        inName = nameTF.getText();
        inType = typeTF.getText();
        inAge = Integer.parseInt( ageTF.getText() );
        //System.out.println("Inside CreateButtonHandler");
        System.out.println(inName);
        arf = new Dog(inName, inType, inAge);
        System.out.println(arf.toString () );

    }

}

and the whole program is below. 整个程序在下面。 Any help/explanation/suggestion would be very welcome. 任何帮助/解释/建议都将受到欢迎。

import javax.swing.*; // import statement for the GUI components
import java.awt.*; //import statement for container class
import java.awt.event.*;



public class DogGUI //creation of DogGUI clas
{
private JLabel nameL, typeL, ageL, outtputL;
private JTextField nameTF, typeTF, ageTF;
private JButton createB, exitB;
CreateButtonHandler cbHandler;
ExitButtonHandler ebHandler; 

    public void driver () //creates everything
    {
        //create the window
        JFrame DogInfo = new JFrame ("Dog GUI");
        DogInfo.setSize(400,300); //set the pixels for GUI
        DogInfo.setVisible(true); // set visibility to true
        DogInfo.setDefaultCloseOperation(DogInfo.EXIT_ON_CLOSE); // when closed JFrame will disappear

//layout
Container DogFields = DogInfo.getContentPane();
DogFields.setLayout(new GridLayout(5,2));

// setting labels for GUI
nameL = new JLabel ("Enter name of Dog:  ",
                    SwingConstants.RIGHT);
typeL = new JLabel ("Enter the type of the Dog:  ",
                    SwingConstants.RIGHT);
ageL = new JLabel ("Enter the age of the Dog:   ",
                    SwingConstants.RIGHT);
outtputL = new JLabel ("Dog Information:  ",
                    SwingConstants.RIGHT);

//Buttons

JButton createB, exitB; //creating button for creation of Dog and button to exit
createB = new JButton("Create Dog");
exitB = new JButton("Exit");

//text fields

JTextField nameTF, typeTF, ageTF, outtputTF; 
nameTF = new JTextField(10);
typeTF = new JTextField(15);
ageTF = new JTextField(5);
outtputTF = new JTextField(25);

outtputTF.setEditable(false); //this TF is to display this output

//Lables and Textfields 
DogInfo.add(nameL);
DogInfo.add(nameTF);

DogInfo.add(typeL);
DogInfo.add(typeTF);

DogInfo.add(ageL);
DogInfo.add(ageTF);

DogInfo.add(outtputL);
DogInfo.add(outtputTF);

//Buttons
DogInfo.add(createB);
DogInfo.add(exitB);


//Instantiate Listeners
cbHandler = new CreateButtonHandler();
ebHandler = new ExitButtonHandler();

//Register Listeners
createB.addActionListener(cbHandler);
exitB.addActionListener(ebHandler);

DogInfo.setVisible(true);
}
//run the program from main, instantiate class, invoke driver() method

public static void main(String[] args)
{
    DogGUI d = new DogGUI();
    d.driver();
}

// class to actually handle Dog creation

private class CreateButtonHandler 
    implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        String inName, inType; //local variables
        int inAge;
        Dog arf;
        inName = nameTF.getText();
        inType = typeTF.getText();
        inAge = Integer.parseInt( ageTF.getText() );
        //System.out.println("Inside CreateButtonHandler");
        System.out.println(inName);
        arf = new Dog(inName, inType, inAge);
        System.out.println(arf.toString () );

    }

}


private class ExitButtonHandler 
    implements ActionListener
{
    public void actionPerformed(ActionEvent e)
        {
            System.out.println("inside exit button");
            System.exit(0);
        } // end method actionPerformed
}


}

You have a local variable in the driver() method called nameTF that is hiding that field (same for some other variables). 您在driver()方法中有一个名为nameTF的局部变量,该局部变量将隐藏该字段(其他变量相同)。

JTextField nameTF, typeTF, ageTF, outtputTF; 
nameTF = new JTextField(10);

Remove the declaration of those fields since they are already declared as instance fields. 删除那些字段的声明,因为它们已经被声明为实例字段。

private JTextField nameTF, typeTF, ageTF;

(probably not outtputTF , depending on what you want to do) (可能不是outtputTF ,具体取决于您要执行的操作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM