按钮不出现,空指针异常



我正在学习使用java.swing库。我正在尝试创建非常简单的计算器的布局。我添加了addnumbers方法。我正在尝试在计算器中显示按钮,并且我已经用于循环。但是,我没有出现我的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]);
        }
    }
}

您需要初始化数组:

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

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

上面的解决方案有效并删除了nullpointer,因为现在将内存分配给Numbutton数组。这是一个工作代码剪辑,还包含了(BODMAS)标志...唯一的区别是我将所有钥匙标签都放入arrayList中现在的for循环在此阵列列表上使用以获取其标签计算器...ArrayList可能不是一个好的收藏。也许枚举可能更好,但是这是为了显示如何使其更具动态并添加按钮标签或新按钮会更容易...另请注意,您可以根据密钥标签的大小使网格化动态化array..gridSize = this.buttons.size()/4 ....

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

最新更新