【问题标题】:Java Radio Button Variable ErrorsJava 单选按钮变量错误
【发布时间】:2013-03-14 03:43:49
【问题描述】:

所以现在可以了,我修复了变量调用错误。但我明白了:

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);
            }
        }
    }
}

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

【问题讨论】:

    标签: java swing variables button radio


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    在 Eclipse 中,转到 Window - Show view 菜单,然后选择打开名为“Problems”的视图。此视图应始终打开。它包含所有编译错误和警告。虽然存在编译错误(标记为红色),但您甚至不应该尝试运行您的程序。

    似乎尝试使用未定义的变量,并且您正在调用一个方法buildPanel(String, String),将两个字符串作为参数而不传递任何参数。在问题视图中双击一个编译错误,eclipse会将光标放在问题所在的行。

    在学习如何编译程序和解决这些基本错误之前,您是如何使用 Swing 的?这就像在学习驾驶之前尝试驾驶法拉利一样。从基础开始。

    【讨论】:

      【解决方案3】:

      您对范围有疑问。例如,您在buildPanel() 方法的作用域内定义parallelogramArea,并试图从另一个作用域(内部类CalcButtonListener 的作用域)内访问它。

      另外,您调用 buildPanel() 时没有两个字符串参数。

      【讨论】:

        【解决方案4】:

        您正在调用此buildPanel(); 方法。

        这是错误的,因为这个方法接受两个参数作为参数。

        所以调用这个方法就像`buildPanel(str1,str2);

        private void buildPanel(String width1, String base1){
              .............
        }
        

        编辑

        声明所有你没有声明的变量。

        String base1 = null;
        

        【讨论】:

          猜你喜欢
          • 2015-03-18
          • 1970-01-01
          • 2020-03-18
          • 1970-01-01
          • 1970-01-01
          • 2017-09-20
          • 2019-03-20
          • 2013-02-12
          • 1970-01-01
          相关资源
          最近更新 更多