Java Swing - 添加带有约束的小部件时出现 NullPointerException

标签 java swing

我刚刚开始学习Swing ,我遇到了NullPointerException当我尝试使用约束将小部件添加到我的内容时。

错误跟踪:

NullPointerException
at chapter.pkg6.ImprovedForceCalculator.<init>(ImprovedForceCalculator.java:56)
at chapter.pkg6.ImprovedForceCalculator.main(ImprovedForceCalculator.java:36)

这是我的代码:

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Font;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class ImprovedForceCalculator {
    private JFrame frame;
    private Container content;
    private GridBagConstraints constraints;

    private JLabel massLabel;
    private JLabel accelerationLabel;

    private JTextField massInput;
    private JTextField accelerationInput;

    private JButton calculateForce;

    private double mass;
    private double acceleration;
    private double force;

    public static void main(String[] args)
    {
        new ImprovedForceCalculator();
    }

    public ImprovedForceCalculator()
    {
        frame = new JFrame();
        frame.setTitle("Improved Force Calulcator");
        frame.setResizable(false);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        constraints = new GridBagConstraints();

        content = frame.getContentPane();
        content.setLayout(new GridBagLayout());

        massLabel = new JLabel("Enter the mass of the object (m): ");
        massLabel.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        content.add(massLabel);
        constraints.gridx = 0;
        constraints.gridy = 0;
        content.add(massInput, constraints);

        massInput = new JTextField();
        massInput.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        constraints.gridx = 1;
        constraints.gridy = 0;
        content.add(massInput, constraints);

        accelerationLabel = new JLabel("Enter the acceleration of the object (a): ");
        accelerationLabel.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = 0;
        constraints.gridy = 1;
        content.add(accelerationLabel, constraints);

        accelerationInput = new JTextField();
        accelerationInput.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        constraints.gridx = 1;
        constraints.gridy = 1;
        content.add(accelerationInput, constraints);

        calculateForce = new JButton("Calculate Force");
        calculateForce.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        calculateForce.addActionListener (new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    mass = Double.parseDouble(massInput.getText());
                    acceleration = Double.parseDouble(accelerationInput.getText());
                    force = mass * acceleration;

                    System.out.println("Force: " + force + " newtons");
                }
                catch (NumberFormatException ex)
                {
                    JOptionPane.showMessageDialog(frame, "Please enter a valid mass and/or acceleration");
                }
            }
        });
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 2;
        content.add(calculateForce, constraints);

        frame.pack();
        frame.setVisible(true);        
    }
}

最佳答案

您遇到空指针异常,因为您在初始化之前尝试使用massInput。请参阅此处:

  content.add(massInput, constraints);

    massInput = new JTextField();

我认为您的意思是将第一行改为 content.add(massLabel,constraints) 。 在将 MassInput 添加到内容之前对其进行初始化。

关于Java Swing - 添加带有约束的小部件时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31305145/

相关文章:

java - 免费画线 java2d 应用程序连接每个新绘图的线

java - Java 中的 ActionPerformed 方法

java - 创建可由另一个类调用的 JTable 渲染方法

java - 如何配置 Gradle Java 平台以偏好更高的依赖版本?

java - 如何仅设置具有流式布局的面板的首选宽度?

java - 如何将 IntelliJ Evaluate Expression 工具与 lambda 和外部作用域一起使用?

java - 字节数组是二维字节数组吗?

java - JButton 无法在 actionListener 内解析

java - Apache bval 依赖于 apache geronimo

java - 使用本地类型推断的交叉类型的有用应用