gpt4 book ai didi

Java 单选按钮变量错误

转载 作者:行者123 更新时间:2023-11-29 03:35:52 25 4
gpt4 key购买 nike

所以这现在可以工作了,我修复了变量调用错误。但我得到:

Exception in thread "main" java.lang.NullPointerException
at Radio.buildPanel(Radio.java:56)
at Radio.<init>(Radio.java:33)
at Radio.main(Radio.java:74)

我的 GUI 弹出但是是空白的,现在是什么情况?我现在无法弄清楚问题是什么。

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




public class Radio extends JFrame
{

private JPanel Panel;
private JPanel buttonPanel;
private JTextField base;
private JTextField width;
private JRadioButton squareArea;
private JRadioButton parallelogramArea;
private JLabel messageLabel;
private JTextField text;
private final int WINDOW_WIDTH = 550;
private final int WINDOW_HEIGHT = 550;
private ButtonGroup radioButtonGroup;
private JRadioButton radioButton1;
private JRadioButton radioButton2;
double pTotal;
double sTotal;

public Radio()
{
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setTitle("Area Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
buildPanel();
add(Panel);

}

public void init()
{
setLayout(new BorderLayout());
add(buttonPanel, BorderLayout.SOUTH);
}
private void buildPanel()
{
JLabel messageLabel1 = new JLabel("Please enter the base: ");
JTextField base = new JTextField(10);
JLabel messageLabel2 = new JLabel("Please enter the width: ");
JTextField width = new JTextField(10);
JRadioButton squareArea = new JRadioButton("Choice 1", true);
JRadioButton parallelogramArea = new JRadioButton("Choice 2");
ButtonGroup group = new ButtonGroup();
JButton calcButton = new JButton("Calculate");
calcButton.setBackground(Color.BLUE);
calcButton.setForeground(Color.PINK);
calcButton.addActionListener(new CalcButtonListener());
Panel.add(messageLabel1);
Panel.add(base);
Panel.add(messageLabel2);
Panel.add(width);
group.add(squareArea);
group.add(parallelogramArea);
buttonPanel.add(squareArea);
buttonPanel.add(parallelogramArea);
Panel.add(calcButton);
Panel.add(buttonPanel);
}




public static void main (String[] args)
{

Radio radio = new Radio();
}

private class CalcButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{


if (parallelogramArea.isSelected());
{
pTotal = Double.parseDouble(base.getText()) * Double.parseDouble(width.getText());
JOptionPane.showMessageDialog(null, "The Area is: " + pTotal);
}


if (squareArea.isSelected())
{

sTotal = Double.parseDouble(base.getText()) * Double.parseDouble(width.getText());
JOptionPane.showMessageDialog(null, "The Area is: " + sTotal);
}
}
}
}

解决这个问题非常重要,在此先感谢您。

最佳答案

第一个错误意味着当您调用 buildPanel(String, String) 时,您需要使用 2 个字符串参数传递给该函数。目前您没有传递参数。第二个错误意味着您尝试使用的变量未定义,因为范围问题意味着当您尝试在方法中使用它们时它们未定义。

关于Java 单选按钮变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15601612/

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