Java 单击JButton时如何更改JLabel?
程序说明: 实现具有以下功能的简单整数计算器: 启动时,将计算器置于默认屏幕的中心。 显示器必须有边框。 所有操作均采用以下形式: 操作数1运算符1操作数2=运算符2操作数3= [C]按钮将清除计算器,例如初始状态。必须支持Alt+[C] Ctrl+[C]Ctrl+按钮组合将显示“C您的姓名” 只有[C]按钮可用于恢复当前显示。 [=]按钮是默认按钮。 结果可能是负面的。 最多只允许8位数字。 显示错误,如“溢出”、“0除以除法”、…,并且[C]按钮将清除错误并重置计算器。 使用JCalculator.png作为程序图标。 我正试图编写一个程序来创建一个简单计算器的swing gui。我希望点击的号码显示在标签上。但是,每当我单击数字时,我总是收到NullPointerException错误。我认为动作表演部分是错误的,但我不太确定 您正在隐藏button_Shapes变量 您声明了一个名为button_Shapes的实例字段,但在构造函数中,您创建了另一个名为button_Shapes的变量,该变量仅具有构造函数的上下文,它只能在构造函数的上下文中使用Java 单击JButton时如何更改JLabel?,java,swing,user-interface,nullpointerexception,Java,Swing,User Interface,Nullpointerexception,程序说明: 实现具有以下功能的简单整数计算器: 启动时,将计算器置于默认屏幕的中心。 显示器必须有边框。 所有操作均采用以下形式: 操作数1运算符1操作数2=运算符2操作数3= [C]按钮将清除计算器,例如初始状态。必须支持Alt+[C] Ctrl+[C]Ctrl+按钮组合将显示“C您的姓名” 只有[C]按钮可用于恢复当前显示。 [=]按钮是默认按钮。 结果可能是负面的。 最多只允许8位数字。 显示错误,如“溢出”、“0除以除法”、…,并且[C]按钮将清除错误并重置计算器。 使用JCalcula
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();
}
});
}
}
删除第二个声明
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", "=", "+"};