java.awt.eventqueue_java – 线程“AWT-EventQueue-0”中的异常?

我正在使用

java和

javax.swing编写一个简单的计算器程序

基本上,当您按下按钮时,程序应该获取该按钮的功能(数字或操作)并将其显示在textArea中.计算器本身的整个逻辑并不重要.此外,还有一个清除菜单项,清除textArea中的所有文本.

但是,每按一次按钮,我都会收到以下错误:

线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException

at calculator.CalculatorGUI.actionPerformed(CalculatorGUI.java:106)

当我按下清除菜单项时,我也遇到类似的错误,当我想要更改文本区域中的文本时,似乎java不喜欢它.

这是我的代码:(第106行是在actionPerfomed方法中,为了方便起见,我已将其标记为)

public class CalculatorGUI implements ActionListener

{

// The calculator for actually calculating!

// private RPNCalculator calculator;

// The main frame for the GUI

private JFrame frame;

// The menubar for the GUI

private JMenuBar menuBar;

// The textbox

private JTextArea textArea;

private JScrollPane scrollArea;

// Areas for numbers and operators

private JPanel numKeysPane;

private JPanel opKeysPane;

private String input;

final String[] numbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };

final String[] operations = { "+", "-", "*", "/" };

/**

* Constructor

*/

public CalculatorGUI() {

// Initialize the calculator

calculator = new RPNCalculator();

}

/**

* Initialize and display the calculator

*/

public void showCalculator() {

String buttonValue;

JButton button;

JMenu menu;

JMenuItem menuItem;

// Create the main GUI components

frame = new JFrame("RPN Calculator");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

numKeysPane = new JPanel(new GridLayout(4, 3));

opKeysPane = new JPanel(new GridLayout(2, 2));

initializeMenu();

initializeNumberPad();

initializeOps();

JTextArea textArea = new JTextArea();

textArea.setEditable(false);

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

JScrollPane scrollArea = new JScrollPane(textArea);

// Create the components to go into the frame and

// stick them in a container named contents

frame.getContentPane().add(numKeysPane, BorderLayout.CENTER);

frame.getContentPane().add(scrollArea, BorderLayout.NORTH);

frame.getContentPane().add(opKeysPane, BorderLayout.LINE_END);

// Finish setting up the frame, and show it.

frame.pack();

frame.setVisible(true);

}

/*

* (non-Javadoc)

*

* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)

*/

public void actionPerformed(ActionEvent e) {

String s = (String)e.getActionCommand();

// calculator.performCommand(s);

textArea.append(s + " "); // <

}

/**

* Initialize the number pad for the calculator

*/

private void initializeNumberPad() {

JButton button;

for (int i = 0; i < numbers.length; i++) {

button = new JButton(numbers[i]);

button.addActionListener(this);

numKeysPane.add(button);

}

}

private void initializeOps(){

JButton button;

for (int i = 0; i < operations.length; i++){

button = new JButton(operations[i]);

button.addActionListener(this);

opKeysPane.add(button);

}

}

/**

* Initialize the menu for the GUI

*/

private void initializeMenu() {

JMenu menu;

JMenuItem menuItem;

JMenuItem menuItem2;

// Create a menu with one item, Quit

menu = new JMenu("Calculator");

menuItem = new JMenuItem("Quit");

// When quit is selected, destroy the application

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// A trace message so you can see this

// is invoked.

System.err.println("Close window");

frame.setVisible(false);

frame.dispose();

}

});

menuItem2 = new JMenuItem("Clear");

menuItem2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e){

textArea.setText("");

}

});

menu.add(menuItem2);

menu.add(menuItem);

// Create the menu bar

menuBar = new JMenuBar();

menuBar.add(menu);

frame.setJMenuBar(menuBar);

}

/**

* Helper method for displaying an error as a pop-up

* @param message The message to display

*/

private static void errorPopup(String message) {

JOptionPane.showMessageDialog(null, message);

}

}

任何帮助将不胜感激!谢谢!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值