Java中GUI计算器的运行时错误

失败者

我正在使用FlowLayout,GridLayout和BorderLayout创建一个GUI计算器。我有以下代码。

import java.awt.*;
import javax.swing.*;
//Imports visual components for program
import java.awt.event.*;
//Imports functions for providing performing action on object

public class Calc extends JFrame implements ActionListener { 
    JPanel[] row = new JPanel[5];
//5 panels are created for 5 rows of buttons
 JButton[] button = new JButton[19];
String[] buttonString = {"1","2","3","+","4","5","6","-",
                     "7","8","9","*",".","/","C","rt","%",
                     "=", "0"};
double[] temporary = {0,0};
//Two memory locations for current number and upcoming number for signs such as *,/,%,+,-

boolean[] sign = new boolean[5];
//5 values for +,-,*,/,% are stored in array because they expect another number upon invocation 
JTextArea display = new JTextArea(1,10);
//Creates display with location specified 

    Calc(){
        //Constructor begins here
        setResizable(false);
        //Sets calculator size to be fixed at 380x250 
        //5x5 is created and selected for the calculator
        for(int a = 0; a < 5; a++)
            sign[a] = false;
        //Initialises the state of the signs for +,-,*,/,%

        JPanel displaypanel = new JPanel();
        JPanel first = new JPanel();
        JPanel last = new JPanel();
        //Create three panels for buttons to be placed in

        displaypanel.setLayout(new FlowLayout());
        displaypanel.add(display);
        //Display is added


        first.setLayout(new GridLayout(3,5));
        for(int a = 0; a<15; a++)
            first.add(button[a]);
        //"first" panel is added

        last.setLayout(new GridLayout(1,4));
        last.add(button[15]);
        last.add(button[16]);
        last.add(button[17]);
        last.add(button[18]);

        JFrame window = new JFrame("Task twelve");
        window.setLayout(new BorderLayout());
        window.add(displaypanel, BorderLayout.PAGE_START);
        window.add(first, BorderLayout.CENTER);
        window.add(last, BorderLayout.PAGE_END);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(400, 400);

        for(int a = 0; a < 19; a++){
            button[a] = new JButton();
            button[a].setText(buttonString[a]);
            button[a].addActionListener(this);
            //Assigns all the numbers and signs for the buttons
        }           
        for(int a = 0; a < 5; a++)
            row[a] = new JPanel();
        //Initialises JPanel for rows so they can be used

        //Assigns size for all buttons and display
        display.setEditable(false);         
    }
public void clear(){
    try{
        display.setText("");
        //Sets the display to be blank
        for(int a = 0; a < 5; a++)
            sign[a] = false;
            //Sets state of all signs to be false
        temporary[0] = 0;
        temporary[1] = 0;
        //Sets temporary values to be 0 as well
    } catch(NullPointerException e){
    }
}
public void root(){
    try{
        double temp = Math.sqrt(Double.parseDouble(display.getText()));
        //Creates variable that converts the value in display to a double and Sqroots the value
        display.setText(Double.toString(temp));
        //Converts value in temp to string and copies it to display
    } catch(NullPointerException e){
    }
}
public void getResult() {
    double result = 0;
    temporary[1] = Double.parseDouble(display.getText());
    String temp0 = Double.toString(temporary[0]);
    String temp1 = Double.toString(temporary[1]);
    try {
        if(temp0.contains("-")) {
            String[] temp2 = temp0.split("-", 2);
            temporary[0] = (Double.parseDouble(temp2[1]) * -1);
        }
        if(temp1.contains("-")) {
            String[] temp3 = temp1.split("-", 2);
            temporary[1] = (Double.parseDouble(temp3[1]) * -1);
        }
    } catch(ArrayIndexOutOfBoundsException e) {
    }
    try {
        if(sign[0] == true)
        //Addition sign
            result = temporary[0] + temporary[1];
         else if(sign[1] == true)
        //Subtraction sign
            result = temporary[0] - temporary[1];
        else if(sign[2] == true)
        //Multiplication sign
            result = temporary[0] * temporary[1];
        else if(sign[3] == true)
        //Division sign
            result = temporary[0] / temporary[1];
        else if(sign[4] == true)
        //Modulus sign
            result = temporary[0] % temporary[1];
        display.setText(Double.toString(result));

        for(int a = 0; a < 5; a++)
            sign[a] = false;
        //Sets state of all signs to be false after one of them has been invoked
    } catch(NumberFormatException e) {
    }
}
public void actionPerformed(ActionEvent ae){
    if(ae.getSource() == button[0])
        display.append("1");
    //When "1" is pressed, "1" is inserted to the display
    if(ae.getSource() == button[1])
        display.append("2");
    if(ae.getSource() == button[2])
        display.append("3");
    if(ae.getSource() == button[3]){
        //Addition sign is selected
        temporary[0] = Double.parseDouble(display.getText());
        sign[0] = true;
        display.setText("");
    }
    if(ae.getSource() == button[4])
        display.append("4");
    if(ae.getSource() == button[5])
        display.append("5");
    if(ae.getSource() == button[6])
        display.append("6");
    if(ae.getSource() == button[7]){
        //Subtraction sign is selected
        temporary[0] = Double.parseDouble(display.getText());
        sign[1] = true;
        display.setText("");
    }
    if(ae.getSource() == button[8])
        display.append("7");
    if(ae.getSource() == button[9])
        display.append("8");
    if(ae.getSource() == button[10])
        display.append("9");
    if(ae.getSource() == button[11]){
        //Multiplication sign is selected
        temporary[0] = Double.parseDouble(display.getText());
        sign[2] = true;
        display.setText("");
    }
    if(ae.getSource() == button[12])
        display.append(".");
    if(ae.getSource() == button[13]){
        //Division sign is selected
        temporary[0] = Double.parseDouble(display.getText());
        sign[3] = true;
        display.setText("");
    }
    if(ae.getSource() == button[14])
        clear();
    if(ae.getSource() == button[15])
        root();
    if(ae.getSource() == button[16]){
        //Modulus sign is selected
        temporary[0] = Double.parseDouble(display.getText());
        sign[4] = true;
        display.setText("");
    }
    if(ae.getSource() == button[17])
        getResult();
    if(ae.getSource() == button[18])
        display.append("0");        
}
public static void main(String[] args){
    Calc c = new Calc();
}
}

编译它不会导致任何错误。但是,运行类确实可以。

Exception in thread"main" java.lang.NullPointerException
at java.awt.Container.addlmpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Calc.<init>(Calc.java:43)
at Calc.main(Calc.java:198)

我不了解这些错误,因此不知道如何解决。有人可以帮忙吗?

Mr_and_Mrs_D

您正在循环中创建15个按钮,button[a] = new JButton(buttonString[a]);但是随后您要请求button [15,16,...](尚未创建的16nth,17nth ...按钮),并且为空。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章