无法在任何地方找到符号错误

时间:2014-06-27 23:18:43

标签: java swing button user-interface compiler-errors

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

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

任何帮助将不胜感激。

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(" ");
            }

        }
    }

}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

我同意rgettman.Here只是代码示例:

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

    private  JLabel display = new JLabel();


    public NumericKeypadPanel2() 
    {

        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);

        // .....
    }

}