【问题标题】:Buttons not appearing, null pointer exception按钮未出现,空指针异常
【发布时间】:2015-05-05 13:29:46
【问题描述】:

我正在学习使用 java.swing 库。我正在尝试创建一个非常简单的计算器的布局。我添加了 addNumbers 方法。我正在尝试在计算器中显示按钮,并且我使用了 for loops.buttons 没有出现我收到 nullpointerexception。

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

public class Calculator extends JFrame{

    /**
     * @param args
     */
    //dEFINE WIDTH AND HEIGHT
    private static final int WIDTH = 400;
    private static final int HEIGHT = 600;

    //Values for buttons having numbers
    private JButton[] numButton;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Calculator myCalculator = new Calculator();

    }

    public Calculator(){
        setTitle("Simple Calculator");
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container Pane = getContentPane();
        Pane.setLayout(new GridLayout(3,3));

        //Add numbers to screen now
        addNumbers(Pane);



    }

    //Function to add numbers on screen
    public void addNumbers(Container P){
        for(int i = 1; i <= 9; i++){
            numButton[i] = new JButton(String.valueOf(i));
            P.add(numButton[i]);
        }
    }

}

【问题讨论】:

标签: java arrays swing


【解决方案1】:

你需要初始化你的数组:

private JButton[] numButton = new JButton[10];

这里的 10 允许您的数组中有 10 个空格。

【讨论】:

  • 我将 private JButton[] numButton 更改为 private JButton[] numButton = new JButton[10],但我仍然得到随机按钮,有时是 3 个,有时是 4 个
  • 随机按钮是什么意思?您应该更新您的问题。
  • 进行您建议的更改后,我开始有时会收到 5 个按钮,有时会收到 4 个按钮。我一直想要 9 个按钮。此问题仅针对您的回答
  • 对不起,我的错误,确实出现了 9 个按钮,但有些按钮正在延迟,或者有时我必须点击窗口最大化,然后将它们恢复到正常大小,然后按钮出现
  • 在您的 addNumbers 方法之后。调用包();然后调用 setVisible();为简单起见,您应该始终先添加按钮。
【解决方案2】:

上述解决方案有效并删除了 NullPointer,因为现在将内存分配给 numButton 数组.... 这是一个工作代码片段,其中还包含(BODMAS)标志...... 唯一的区别是我已将所有关键标签放入 ArrayList 并且 for 循环现在在这个 ArrayList 上工作以获取它的标签 计算器... ArrayList 可能不是一个好的集合..也许枚举可能更好但是 这是为了展示如何使其更具动态性并添加按钮标签 或者新按钮会更容易...... 另请注意,您可以根据键标签的大小使 GridSize 动态化 array..gridSize = this.buttons.size() / 4....

【讨论】:

    【解决方案3】:
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class Calculator extends JFrame{
    
        private static final int WIDTH = 400;
        private static final int HEIGHT = 600;
    
    
        List<String> buttons = new ArrayList<String>();
    
    
    
    
        JButton[] numButton = null;
    
    
        public static void main(String[] args) {
            Calculator myCalculator = new Calculator();
        }
    
        public Calculator(){
            setTitle("Simple Calculator");
            setSize(WIDTH,HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            Container Pane = getContentPane();
    
    
            this.buttons.add("1");
            this.buttons.add("2");
            this.buttons.add("3");
            this.buttons.add("4");
            this.buttons.add("5");
            this.buttons.add("6");
            this.buttons.add("7");
            this.buttons.add("8");
            this.buttons.add("9");
            this.buttons.add("0");
            this.buttons.add("+");
            this.buttons.add("=");
            this.buttons.add("%");
            this.buttons.add("#");
            this.buttons.add("*");
            this.buttons.add("/");
    
            int gridSize = this.buttons.size() / 4;
    
            Pane.setLayout(new GridLayout(gridSize,gridSize));
    
    
            this.numButton = new JButton[this.buttons.size()];
    
            //Add numbers to screen now
            addNumbers(Pane);
           }
    
        //Function to add numbers on screen
        public void addNumbers(Container P){
            for(int i = 0; i < this.buttons.size(); i++){
                numButton[i] = new JButton(this.buttons.get(i).toString());
                P.add(numButton[i]);
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 1970-01-01
      • 2015-07-25
      • 2013-08-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      相关资源
      最近更新 更多