gpt4 book ai didi

java - 到处都找不到符号错误

转载 作者:行者123 更新时间:2023-11-30 09:06:26 25 4
gpt4 key购买 nike

这应该是一个由三部分组成的面板,顶部面板是一个标签,当按下中间面板中的按钮时,该标签会更新为一个数字。当一个标签被清除按下底部的清除按钮。

问题是几乎每次从中间面板提到每个按钮都会导致“找不到符号”错误...

如有任何帮助,我们将不胜感激。

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

public class NumericKeypadPanel2 extends JPanel
{


//----------------------------------------------------------------------


public NumericKeypadPanel2()
{
JButton b1 = new JButton ("1");
JButton b2 = new JButton ("2");
JButton b3 = new JButton ("3");
JButton b4 = new JButton ("4");
JButton b5 = new JButton ("5");
JButton b6 = new JButton ("6");
JButton b7 = new JButton ("7");
JButton b8 = new JButton ("8");
JButton b9 = new JButton ("9");
JButton b10 = new JButton ("*");
JButton b11 = new JButton ("0");
JButton b12 = new JButton ("#");
JButton clear = new JButton ("Clear");

NumListener listener = new NumListener();

b1.addActionListener (listener);
b2.addActionListener (listener);
b3.addActionListener (listener);
b4.addActionListener (listener);
b5.addActionListener (listener);
b6.addActionListener (listener);
b7.addActionListener (listener);
b8.addActionListener (listener);
b9.addActionListener (listener);
b10.addActionListener (listener);
b11.addActionListener (listener);
b12.addActionListener (listener);
clear.addActionListener (listener);

String num = " ";

JPanel panel_main = new JPanel();
panel_main.setLayout(new BorderLayout());

//top
JLabel display = new JLabel(num);
display.setBorder (BorderFactory.createLineBorder (Color.black, 3));
panel_main.add(display, BorderLayout.NORTH);

//mid
JPanel keypad = new JPanel();
keypad.setLayout(new GridLayout (4, 3));
keypad.setBorder (BorderFactory.createLineBorder (Color.black, 3));
//adding the buttons
keypad.add (b1);
keypad.add (b2);
keypad.add (b3);
keypad.add (b4);
keypad.add (b5);
keypad.add (b6);
keypad.add (b7);
keypad.add (b8);
keypad.add (b9);
keypad.add (b10);
keypad.add (b11);
keypad.add (b12);
//adding the panel
panel_main.add(keypad, BorderLayout.CENTER);

//btm
panel_main.add(clear, BorderLayout.CENTER);

}

private class NumListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource() == b1)
{
display.setText(display.getText()+"1");
}
else if(e.getSource() == b2)
{
display.setText(display.getText()+"2");
}
else if(e.getSource() == b3)
{
display.setText(display.getText()+"3");
}
else if(e.getSource() == b4)
{
display.setText(display.getText()+"4");
}
else if(e.getSource() == b5)
{
display.setText(display.getText()+"5");
}
else if(e.getSource() == b6)
{
display.setText(display.getText()+"6");
}
else if(e.getSource() == b7)
{
display.setText(display.getText()+"7");
}
else if(e.getSource() == b8)
{
display.setText(display.getText()+"8");
}
else if(e.getSource() == b9)
{
display.setText(display.getText()+"9");
}
else if(e.getSource() == b10)
{
display.setText(display.getText()+"*");
}
else if(e.getSource() == b11)
{
display.setText(display.getText()+"0");
}
else if(e.getSource() == b12)
{
display.setText(display.getText()+"#");
}
else if(e.getSource() == clear)
{
display.setText(" ");
}

}
}

}

最佳答案

所有的 JButtonsJLabels 都在构造函数中声明为局部变量,因此无法从构造函数外部访问它们。

要让它们对其他方法可用,请在您的类中将这些变量声明为实例变量(在任何方法或构造函数之外)。然后它们将出现在整个类(class)的范围内。

关于java - 到处都找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24462043/

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