所以这现在有效,我修复了变量调用错误。但我得到:
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);
}
}
}
}
解决这个问题非常重要,在此先感谢您。