【问题标题】:Radio Buttons won't display properly单选按钮无法正确显示
【发布时间】:2014-03-01 06:56:51
【问题描述】:

我只是在学习单选按钮,并认为我终于掌握了它,因为它编译得很好。但是当我尝试运行程序时,出现了这个错误:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at ActionFrame.makeComponents(ActionFrame.java:43)
at ActionFrame.<init>(ActionFrame.java:20)
at ActionFrame.main(ActionFrame.java:80)

我不知道我做错了什么,所以如果你能指出我正确的方向吗?或者向我解释为什么我做错了?

public class ActionFrame extends JFrame {
JLabel messageLabel;
JRadioButton rb1;
JRadioButton rb2;
JRadioButton rb3;
JRadioButton rb4;
String a = "Football";
String b = "Basketball";
String c = "Baseball";
String d = "Hockey";

public ActionFrame() {
  setTitle("Favorite Sports");
  setSize (400,200);
  setDefaultCloseOperation(
     JFrame.EXIT_ON_CLOSE );

  JPanel myStuff = makeComponents();
  add(myStuff);
  setVisible(true);
}

private JPanel makeComponents() {
  JPanel myPanel = new JPanel();

  messageLabel = new JLabel("Select your favorite sport: ");

  rb1 = new JRadioButton(a, true);
  rb1 = new JRadioButton(b);
  rb1 = new JRadioButton(c);
  rb1 = new JRadioButton(d);

  ButtonGroup group = new ButtonGroup();

  group.add(rb1);
  group.add(rb2);
  group.add(rb3);
  group.add(rb4);

  myPanel.add(rb1);
  myPanel.add(rb2);
  myPanel.add(rb3);
  myPanel.add(rb4);

  rb1.addActionListener( new BList() );
  rb2.addActionListener( new BList() );
  rb3.addActionListener( new BList() );
  rb4.addActionListener( new BList() );

  myPanel.add(messageLabel);

  return myPanel;
}

private class BList implements ActionListener {
  public void actionPerformed(ActionEvent e) {

     if(e.getSource() == rb1){
        System.out.println("Your favorite sport is " + a +".");
     }

     else if(e.getSource() == rb2){
        System.out.println("Your favorite sport is " + b +".");
     }

     else if(e.getSource() == rb3){
        System.out.println("Your favorite sport is " + c +".");
     }

     else if(e.getSource() == rb4){
        System.out.println("Your favorite sport is " + d +".");
     }

  }
}

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

【问题讨论】:

    标签: java radio-button jpanel


    【解决方案1】:

    不是:

      rb1 = new JRadioButton(a, true);
      rb1 = new JRadioButton(b);
      rb1 = new JRadioButton(c);
      rb1 = new JRadioButton(d);
    

    但是:

      rb1 = new JRadioButton(a, true);
      rb2 = new JRadioButton(b);
      rb3 = new JRadioButton(c);
      rb4 = new JRadioButton(d);
    

    哎呀。

    这里的关键不是你的错误,我们都会犯类似的错误。关键是要学习如何调试 NPE。当你得到一个时,检查抛出异常的行,找出哪个变量为空,然后回头尝试解决它。 是关键。

    【讨论】:

    • @user2877117:不,你不是白痴;你只是还没有学会调试 NPE。请阅读我的编辑。
    【解决方案2】:

    您已将所有单选按钮命名为 rb1,因此其他组件永远不会初始化

    rb1 = new JRadioButton(a, true);
    rb2 = new JRadioButton(b);
    rb3 = new JRadioButton(c);
    rb4 = new JRadioButton(d);
    

    【讨论】:

      【解决方案3】:

      改变

        rb1 = new JRadioButton(a, true);
        rb1 = new JRadioButton(b);
        rb1 = new JRadioButton(c);
        rb1 = new JRadioButton(d);
      

      进入

        rb1 = new JRadioButton(a, true);
        rb2 = new JRadioButton(b);
        rb3 = new JRadioButton(c);
        rb4 = new JRadioButton(d);
      

      原始代码(有改动)

      public class ActionFrame extends JFrame {
      JLabel messageLabel;
      JRadioButton rb1;
      JRadioButton rb2;
      JRadioButton rb3;
      JRadioButton rb4;
      String a = "Football";
      String b = "Basketball";
      String c = "Baseball";
      String d = "Hockey";
      
      public ActionFrame() {
        setTitle("Favorite Sports");
        setSize (400,200);
        setDefaultCloseOperation(
           JFrame.EXIT_ON_CLOSE );
      
        JPanel myStuff = makeComponents();
        add(myStuff);
        setVisible(true);
      }
      
      private JPanel makeComponents() {
        JPanel myPanel = new JPanel();
      
        messageLabel = new JLabel("Select your favorite sport: ");
      
      /// changes are here...
        rb1 = new JRadioButton(a, true);
        rb2 = new JRadioButton(b);
        rb3 = new JRadioButton(c);
        rb4 = new JRadioButton(d);
      
        ButtonGroup group = new ButtonGroup();
      
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);
      
        myPanel.add(rb1);
        myPanel.add(rb2);
        myPanel.add(rb3);
        myPanel.add(rb4);
      
        rb1.addActionListener( new BList() );
        rb2.addActionListener( new BList() );
        rb3.addActionListener( new BList() );
        rb4.addActionListener( new BList() );
      
        myPanel.add(messageLabel);
      
        return myPanel;
      }
      
      private class BList implements ActionListener {
        public void actionPerformed(ActionEvent e) {
      
           if(e.getSource() == rb1){
              System.out.println("Your favorite sport is " + a +".");
           }
      
           else if(e.getSource() == rb2){
              System.out.println("Your favorite sport is " + b +".");
           }
      
           else if(e.getSource() == rb3){
              System.out.println("Your favorite sport is " + c +".");
           }
      
           else if(e.getSource() == rb4){
              System.out.println("Your favorite sport is " + d +".");
           }
      
        }
      }
      
      public static void main(String[] args) {
        new ActionFrame();
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-06
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-04
        • 2015-04-16
        • 1970-01-01
        相关资源
        最近更新 更多