Java GUI 变量问题

标签 java swing user-interface actionlistener jtextfield

我似乎在“创建按钮”处理程序的操作监听器中设置了一些错误。当我尝试获取文本字段 nameTF 的值时,出现空指针错误。我尝试模仿您的计算器代码,退出按钮处理程序运行良好。我知道按“创建”按钮会调用处理程序,但语句

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
}


}

最佳答案

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

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

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

private JTextField nameTF, typeTF, ageTF;

(可能不是 outtputTF,具体取决于您想要执行的操作)

关于Java GUI 变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16682780/

相关文章:

Java - 如何使用盒子布局设置组件的高度和宽度

java - TableColumnModelListener。 JPanel revalidate() 方法破坏了 swing 元素的组合

java - 使用定时器暂停程序执行

android - Android上的相机预览UI覆盖?

user-interface - 使用系统调用的 Common Lisp GUI 编程

java - 负向后查找中的字范围或\w

java - 同步 Controller 与 View

java - 生命周期配置未涵盖的插件执行 : org. jetbrains.kotlin :kotlin-maven-plugin:1. 1.51:test-compile

java - 替换字符串中的字符,不使用 string Replace() 方法

css - css 中的 % 和 vw 有什么区别?