Java GUI变量问题

时间:2013-05-22 03:00:13

标签: java swing user-interface actionlistener jtextfield

我似乎在Create Button处理程序的动作侦听器中设置了错误。当我试图获取文本字段nameTF的值时,我得到一个空指针错误。我试图模仿你的计算器代码,退出按钮处理程序运行良好。我知道按下Create按钮会调用处理程序,但会调用语句

inName = nameTF.getText();

给出错误信息。

听众的完整文本是

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

    }

}

整个程序如下。任何帮助/解释/建议都会非常受欢迎。

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
}


}

1 个答案:

答案 0 :(得分:3)

您在名为driver()的{​​{1}}方法中有一个隐藏该字段的局部变量(对于其他一些变量也是如此)。

nameTF

删除这些字段的声明,因为它们已经被声明为实例字段。

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

(可能不是private JTextField nameTF, typeTF, ageTF; ,取决于你想做什么)