gpt4 book ai didi

java - 单击 JButton 时如何更改 JLabel?

转载 作者:行者123 更新时间:2023-12-02 10:46:26 25 4
gpt4 key购买 nike

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

public class JCalculator implements ActionListener {

private JButton[] buttons;
private JLabel display;
private String[] button_Shapes;
//private String s0, s1, s2;

//Create new form Calculator
public JCalculator(){

//Create new JFrame container
JFrame jfrm = new JFrame("Calculator");

//Set the initial size for frame
jfrm.setSize(500,500);

//Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set the calculator to center on the screen
jfrm.setLocationRelativeTo(null);

//Set the icon
ImageIcon icon = new ImageIcon("C:/Users/zetsu/Desktop/JCalculator.png");
jfrm.setIconImage(icon.getImage());

//Create label
display = new JLabel("0", JLabel.RIGHT);

//Put border around display label
display.setBorder(BorderFactory.createLineBorder(Color.black));

//Create a grid layout
GridLayout layout = new GridLayout(4,4);

//Create a panel and set layout
JPanel bottom_Panel = new JPanel();
bottom_Panel.setLayout(layout);

//Create an array of buttons
buttons = new JButton[16];

//Put buttons in an array
String[] button_Shapes = {"7", "8", "9", "/", "4", "5", "6", "x", "1",
"2", "3", "-", "0", "C", "=", "+"};

for(int i = 0; i < button_Shapes.length; i++){
//make new button name
JButton btn = new JButton("" + button_Shapes[i]);
buttons[i] = btn;
//add action listener for each button
btn.addActionListener(this);
//add each button to panel
bottom_Panel.add(btn);
}

//Set [=] button to default
jfrm.getRootPane().setDefaultButton(buttons[14]);

//Set Mnemonic to Alt+[C]
buttons[13].setMnemonic(KeyEvent.VK_C);

//Add label to content pane
jfrm.add(display, BorderLayout.NORTH);

//Add panel to content pane
jfrm.add(bottom_Panel, BorderLayout.CENTER);

jfrm.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent e) {
//try left operand, operation, right operand
Object event = e.getSource();
if(event == buttons[0]){
display.setText(""+button_Shapes[0]);
}
}

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new JCalculator();
}
});
}
}

程序说明:

Implement a simple integer calculator with the following features:

  1. Center the calculator on the default screen at startup.
  2. The display must have a border.
  3. All the operations are in the form: (operand1 operator1 operand2 =) operator2 operand3 =
  4. The [C] button will clear the calculator, eg. initial state. Must support Alt+[C]
  5. Ctrl+[C] Ctrl+button combination will display “(c) your-name” Only the [C] button can be used to restore the current display.
  6. The [=] button is the default button.
  7. The result can be negative.
  8. Only allow up to 8 digits.
  9. Display error such as “Overflow”, “Div by 0”, …, and the [C] button will clear the error and reset the calculator.
  10. Use JCalculator.png as the program icon.

我正在尝试编写一个程序来创建一个简单计算器的 swing gui。我希望点击的数字显示在标签上。但是,我不断收到NullPointerException每当我点击数字时都会出错。我认为actionPerformed部分是错误的,但我不太确定。

最佳答案

您正在隐藏 button_Shapes 变量。

您声明了一个名为 button_Shapes 的实例字段,但在构造函数中,您创建了另一个名为 button_Shapes 的变量,该变量仅具有构造函数的上下文(它只能在构造函数中使用)构造函数的上下文)

public class JCalculator implements ActionListener {

private String[] button_Shapes;
//Create new form Calculator
public JCalculator(){
//...
//Put buttons in an array
String[] button_Shapes = {"7", "8", "9", "/", "4", "5", "6", "x", "1",
"2", "3", "-", "0", "C", "=", "+"};
//...

删除第二个声明

private String[] button_Shapes;
//Create new form Calculator
public JCalculator(){
//...
//Put buttons in an array
button_Shapes = {"7", "8", "9", "/", "4", "5", "6", "x", "1",
"2", "3", "-", "0", "C", "=", "+"};

您可能还想通读Language Basics/Variables了解更多详情

关于java - 单击 JButton 时如何更改 JLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52527762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com