gpt4 book ai didi

java - 实现 JFrame 时线程 "main"java.lang.NullPointerException 中出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:31 27 4
gpt4 key购买 nike

我的任务是创建一个java程序,实现关于BMI转换器的GUI,该转换器可以获取高度和体重。目前尚未完成,但我收到以下错误:

Exception in thread "main" java.lang.NullPointerException
at BMIGUI.<init>(BMIGUI.java:60)
at BMITest.main(BMITest.java:7)

在和我的一个 friend 一起调试它时,他建议我的错误是我没有在 BMIGUI 代码中初始化我的变量“frame”。但再看一遍,我觉得这似乎是正确的..

请帮忙。谢谢

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class BMIGUI extends JFrame
{
private final int WIDTH = 275;
private final int HEIGHT = 100;

DecimalFormat fmt = new DecimalFormat ("0.00");

private JFrame frame;
private JPanel panel;
private JLabel heightLabel, weightLabel, BMILabel, resultLabel;
private JTextField height, weight;
private JButton calculate;


public BMIGUI()
{
super("BMI Converter");

heightLabel = new JLabel ("Your height in meters:");
weightLabel = new JLabel ("Your weight in kilograms: ");

//create a "this is your BMI" label
BMILabel = new JLabel ("Your BMI is ") ;
//create a result label to hold the BMI value
resultLabel = new JLabel ("") ;

//create a JTextField to hold the person's height in inches
height = new JTextField (5);
//create a JTextField to hold the person's weight in pounds
weight = new JTextField (5) ;

//create a button to press to calculate BMI
calculate = new JButton ("calculate BMI");
//create a BMIListener and make it listen for the button to be pressed
calculate.addActionListener ( new BMIListener () ) ;

//set up the JPanel to go on the JFrame
panel = new JPanel();
panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
panel.setBackground (Color.yellow);

//add the height label and height textfield to the panel
panel.add (heightLabel) ;
panel.add (height) ;
//add the weight label and weight textfield to the panel
panel.add (weightLabel) ;
panel.add (weight) ;
//add the button to the panel
panel.add (calculate) ;
//add the BMI label to the panel
panel.add (BMILabel) ;
//add the label that holds the result to the panel
panel.add (resultLabel) ;

//add the panel to the frame
frame.getContentPane().add (panel);
}


public void display()
{
frame.pack();
frame.setVisible(true);
}


private class BMIListener implements ActionListener
{

public void actionPerformed (ActionEvent event)
{
String heightText, weightText;
double heightVal, weightVal, bmi;

//get the text from the height and weight textfields
heightText = height.getText();
weightText = weight.getText();

//Use Integer.parseInt to convert the text to integer values
heightVal = Double.parseDouble (heightText) ;
weightVal = Double.parseDouble (weightText) ;

//Calculate the bmi = 703 * weight in pounds / (height in inches)^2
bmi = weightVal / (heightVal*heightVal) ;

//Put result in result label. Use Double.toString to convert double to string.
resultLabel.setText( fmt.format(bmi) ) ;

}
}
}

主类

 import javax.swing.JFrame;

public class BMITest extends JFrame
{
public static void main(String args[])
{
BMIGUI frame = new BMIGUI();

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
frame.setSize(350,100);
frame.setVisible(true);
}
}

最佳答案

尽管您正在引用 BMIGUI 类上下文中声明的 frame,但您尚未初始化它。如:

frame.getContentPane().add (panel);

还有其他地方。由于 BMIGUI 本身就是您的框架窗口,实际上您根本不需要在 BIMGUI 上下文中声明另一个框架实例,只需将 frame 引用替换为 this 即可引用当前引用实例。

这始终被认为是良好的做法:

  1. 始终覆盖 getPreferredSize(Dimension) 而不是 setPreferredSize()
  2. 调用 pack() 而不是使用 setSize(Dimension) :pack() 方法调整框架大小,以便其所有内容都等于或高于其首选大小。

请,follow java's usual coding convention 。类的名字后面要大写,但不能全部字母都大写,例如BIMGUI应该是BimGui。我们使用全部大写字母来表示常量字段:public static final int DATAMARKER

关于java - 实现 JFrame 时线程 "main"java.lang.NullPointerException 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20445222/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com