0

我有一个关于 JTextField 的问题,每当我想从我的学生类中获取文本字段值并将其传递给我的另一个类 myAction 时,我总是会得到一个 NullPointerException 我想要发生的是在我单击按钮或 Jbutton 之后文本字段中的所有数据都将保存到数组中,并且我的按钮已经有了一个动作监听器并创建了一个 myAction 类。

这是我的代码:学生类(主类)

/*

package Student;
import java.util.Scanner;
import javax.swing.*;
import java.awt.event.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student{
    private static Scanner input = new Scanner(System.in);
    public String[] name = {"Lara", "Jerome", "Ferdie", "Jeffrey", "Eduard", "King"};
    public int[] grade = new int[6];
    JFrame frame;
    JLabel label1, label2, label3, label4, label5, label6;
    JPanel panel;
    public JTextField text1, text2, text3, text4, text5, text6;
    JButton button;


    public static void main(String[] args)
    {
        Student myStudent = new Student();
        myStudent.Layout();
        //myStudent.
    }

    public void GetValue()
    {
        grade[0] = Integer.parseInt(text1.getText());
        grade[1] = Integer.parseInt(text2.getText());
        grade[2] = Integer.parseInt(text3.getText());
        grade[3] = Integer.parseInt(text4.getText());
        grade[4] = Integer.parseInt(text5.getText());
        grade[5] = Integer.parseInt(text6.getText());

        for(int i = 0; i < grade.length; i++)
        {
            System.out.println(grade[i]);
        }
    }

    public void Layout()
    {
        //Design Layout
        panel = new JPanel();
        panel.setLayout(null);
        frame = new JFrame("Student Grades");
        button = new JButton("Get Grade");
        button.setBounds(50, 260, 200, 30);
        label1 = new JLabel("Your Grade: " + name[0]);
        label1.setBounds(10, 10, 150, 30);
        text1 = new JTextField(5);
        text1.setBounds(140, 15, 150, 20);
        label2 = new JLabel("Your Grade: " + name[1]);
        label2.setBounds(10, 50, 150, 30);
        text2 = new JTextField(5);
        text2.setBounds(140, 55, 150, 20);
        label3 = new JLabel("Your Grade: " + name[2]);
        label3.setBounds(10, 90, 150, 30);
        text3 = new JTextField(5);
        text3.setBounds(140, 95, 150, 20);
        label4 = new JLabel("Your Grade: " + name[3]);
        label4.setBounds(10, 130, 150, 30);
        text4 = new JTextField(5);
        text4.setBounds(140, 135, 150, 20);
        label5 = new JLabel("Your Grade: " + name[4]);
        label5.setBounds(10, 170, 150, 30);
        text5 = new JTextField(5);
        text5.setBounds(140, 175, 150, 20);
        label6 = new JLabel("Your Grade: " + name[5]);
        label6.setBounds(10,210, 150, 30);
        text6 = new JTextField(5);
        text6.setBounds(140, 215, 150, 20);
        frame.setSize(350, 350);
        frame.setVisible(true);
        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);
        panel.add(label3);
        panel.add(text3);
        panel.add(label4);
        panel.add(text4);
        panel.add(label5);
        panel.add(text5);
        panel.add(label6);
        panel.add(text6);
        panel.add(button);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyEvent myAction = new MyEvent();
        button.addActionListener(myAction);
    }

}

and my class myAction(or actionlistener) class

class MyEvent implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
      if(e.getActionCommand().equals("Get Grade"))
      {
          Student EventStudent = new Student();
          EventStudent.GetValue();

      }
    }   
}

*/ 这是我所有的代码,希望你能帮助我。

4

1 回答 1

2

您的代码中的问题是您在 main 中创建了一个 Student 对象。因此,您的文本字段与该对象相关联。现在,在您的事件侦听器中,您正在创建另一个不属于您需要的任何组件的 student 对象。您必须在 main 方法中从对象中检索数据。像下面一样。希望它会有所帮助。

import java.util.Scanner;
import javax.swing.*;
import java.awt.event.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student{
    static Student myStudent ;
    private static Scanner input = new Scanner(System.in);
    public String[] name = {"Lara", "Jerome", "Ferdie", "Jeffrey", "Eduard", "King"};
    public int[] grade = new int[6];
    JFrame frame;
    JLabel label1, label2, label3, label4, label5, label6;
    JPanel panel;
    public JTextField text1, text2, text3, text4, text5, text6;
    JButton button;


    public static void main(String[] args)
    {
         myStudent = new Student();
         myStudent.Layout();
        //myStudent.
    }

    public void GetValue()
    {
        grade[0] = Integer.parseInt(text1.getText());
        grade[1] = Integer.parseInt(text2.getText());
        grade[2] = Integer.parseInt(text3.getText());
        grade[3] = Integer.parseInt(text4.getText());
        grade[4] = Integer.parseInt(text5.getText());
        grade[5] = Integer.parseInt(text6.getText());

        for(int i = 0; i < grade.length; i++)
        {
            System.out.println(grade[i]);
        }
    }

    public void Layout()
    {
        //Design Layout
        panel = new JPanel();
        panel.setLayout(null);
        frame = new JFrame("Student Grades");
        button = new JButton("Get Grade");
        button.setBounds(50, 260, 200, 30);
        label1 = new JLabel("Your Grade: " + name[0]);
        label1.setBounds(10, 10, 150, 30);
        text1 = new JTextField(5);
        text1.setBounds(140, 15, 150, 20);
        label2 = new JLabel("Your Grade: " + name[1]);
        label2.setBounds(10, 50, 150, 30);
        text2 = new JTextField(5);
        text2.setBounds(140, 55, 150, 20);
        label3 = new JLabel("Your Grade: " + name[2]);
        label3.setBounds(10, 90, 150, 30);
        text3 = new JTextField(5);
        text3.setBounds(140, 95, 150, 20);
        label4 = new JLabel("Your Grade: " + name[3]);
        label4.setBounds(10, 130, 150, 30);
        text4 = new JTextField(5);
        text4.setBounds(140, 135, 150, 20);
        label5 = new JLabel("Your Grade: " + name[4]);
        label5.setBounds(10, 170, 150, 30);
        text5 = new JTextField(5);
        text5.setBounds(140, 175, 150, 20);
        label6 = new JLabel("Your Grade: " + name[5]);
        label6.setBounds(10,210, 150, 30);
        text6 = new JTextField(5);
        text6.setBounds(140, 215, 150, 20);
        frame.setSize(350, 350);
        frame.setVisible(true);
        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);
        panel.add(label3);
        panel.add(text3);
        panel.add(label4);
        panel.add(text4);
        panel.add(label5);
        panel.add(text5);
        panel.add(label6);
        panel.add(text6);
        panel.add(button);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyEvent myAction = new MyEvent(myStudent);
        button.addActionListener(myAction);
    }

}
class MyEvent implements ActionListener
{
    Student myStudent;
    public MyEvent(Student myStudent) {
        this.myStudent=myStudent;
    }

    public void actionPerformed(ActionEvent e) 
    {
      if(e.getActionCommand().equals("Get Grade"))
      {

         myStudent.GetValue();

      }
    }   
}
于 2012-08-02T06:52:48.637 回答