在制作计算器应用程序时遇到 Java swing 问题

问题描述 投票:0回答:1

我一直在断断续续地创建一个计算器,并且正在使用 Bro Code 创建的教程。我能够毫无问题地运行它多次,但几周后回来后,它对我不再起作用。我没有更改一行代码,也没有添加任何新内容。它曾经工作得很好,但现在抛出了 java.lang.NullPointerException (Calculator calc = new Calculator();)

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

public class Calculator implements ActionListener
{
    JFrame frame;
    JTextField tf; 
    JButton[] numButtons = new JButton[10];
    JButton[] funcButtons = new JButton[7];
    JButton addButton, subButton, mulButton, divButton;
    JButton decButton, eqButton, clrButton;
    JPanel panel;

    Font myFont = new Font("Courier Bold", Font.BOLD, 30);
    
    Calculator()
    {
        frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 550);
        frame.setLayout(null);

        tf.setBounds(50, 25, 300, 50);
        tf.setFont(myFont);
        tf.setEditable(false);
        //Buttons
        //Creating our function buttons
        addButton = new JButton("+");
        subButton = new JButton("-");
        mulButton = new JButton("x");
        divButton = new JButton("/");
        decButton = new JButton(".");
        clrButton = new JButton("clr");
        eqButton = new JButton("=");

        //Adding function buttons to funcButtons array
        funcButtons[0] = addButton;
        funcButtons[1] = subButton;
        funcButtons[2] = mulButton;
        funcButtons[3] = divButton;
        funcButtons[4] = decButton;
        funcButtons[5] = clrButton;
        funcButtons[6] = eqButton;

        //Adding action listeners and stuff to the func buttons
        for (int i = 0; i < funcButtons.length; i++)
        {
            funcButtons[i].addActionListener(this);
            funcButtons[i].setFont(myFont);
            funcButtons[i].setFocusable(false);
        }

        for (int i = 0; i < numButtons.length; i++)
        {
            numButtons[i] = new JButton(String.valueOf(i));
            numButtons[i].addActionListener(this);
            numButtons[i].setFocusable(false);
        }

        clrButton.setBounds(127, 430, 145, 50);

        panel = new JPanel();
        panel.setBounds(50, 100, 300, 300);
        panel.setLayout(new GridLayout(4,4,10,10));
        //panel.setBackground(Color.gray);
        //Adding Buttons to panel
        panel.add(numButtons[1]);
        panel.add(numButtons[2]);
        panel.add(numButtons[3]);
        panel.add(addButton);
        panel.add(numButtons[4]);
        panel.add(numButtons[5]);
        panel.add(numButtons[6]);
        panel.add(subButton);
        panel.add(numButtons[7]);
        panel.add(numButtons[8]);
        panel.add(numButtons[9]);
        panel.add(mulButton);
        panel.add(decButton);
        panel.add(numButtons[0]);
        panel.add(eqButton);
        panel.add(divButton);

        frame.add(panel);
        frame.add(clrButton);
        frame.add(tf);
        frame.setVisible(true);
    }
    public static void main(String args[]){
        Calculator calc = new Calculator();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        for (int i = 0; i < 10; i++)
        {
            if (e.getSource() == numButtons[i])
            {
                tf.setText(tf.getText().concat(String.valueOf(i)));
            }
        }
        throw new UnsupportedOperationException("Unimplemented method 'actionPerformed'");
    }
}

抱歉,这是我第一次遇到堆栈溢出,所以我不确定是否要发布整个程序,但我想提供尽可能多的上下文。

我只是想弄清楚为什么我的代码不再运行

java swing user-interface actionlistener
1个回答
0
投票

我发现罪魁祸首就在第8行。 见下文 JTextField tf;

您应该将其修改为 JTextField tf = new JTextField();

© www.soinside.com 2019 - 2024. All rights reserved.