Java swing、范围外变量和按钮的使用

Java swing、范围外变量和按钮的使用,java,swing,jbutton,calculator,Java,Swing,Jbutton,Calculator,我正在尝试制作一个actionlistener,对于我创建的按钮,每个按钮都显示一个文本字段,就像一个计算器在输入一个数字时一样 然而,在actionPerformed方法下,它看起来像button1,而且它们似乎都超出了范围,我现在不能声明按钮静态,对吗 我需要克服超出范围的问题有人吗 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java

我正在尝试制作一个actionlistener,对于我创建的按钮,每个按钮都显示一个文本字段,就像一个计算器在输入一个数字时一样

然而,在
actionPerformed
方法下,它看起来像
button1
,而且它们似乎都超出了范围,我现在不能声明按钮静态,对吗

我需要克服超出范围的问题有人吗

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;

import layout.TableLayout;

public class example extends JFrame implements ActionListener
{

public static void main (String args[])
 {
     new example();
 }

 public example ()
 {
     super("The Power of Preferred Sizes");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     Container pane = getContentPane();




     double size[][] = {{75, 75, 75, 75, 75}, // Columns
             {75, 75, 75, 75, 75,75}}; // Rows

   pane.setLayout(new TableLayout(size));

     TableLayout layout = new TableLayout(size);
     pane.setLayout (layout);

     // Create all controls
     String label[] = {"1", "2", "3", "4", "5","6","7", "8","9","0","PLS","RES"};
     JButton button[] = new JButton[label.length];


     JTextField upperText    = new JTextField();
     JTextField bottomText    = new JTextField();

    // textfieldName.setSize(10, 10);
     pane.add(upperText,    "0,  0, 4, 0");
     pane.add(bottomText,   "0,  5, 4, 0");




     JButton button1 = new JButton(label[0]);
     JButton button2 = new JButton(label[1]);
     JButton button3 = new JButton(label[2]);
     JButton button4 = new JButton(label[3]);
     JButton button5 = new JButton(label[4]);
     JButton button6 = new JButton(label[5]);
     JButton button7 = new JButton(label[6]);
     JButton button8 = new JButton(label[7]);
     JButton button9 = new JButton(label[8]);
     JButton button10 = new JButton(label[9]);
     JButton button11= new JButton(label[10]);
     JButton button12 = new JButton(label[11]);





     pane.add(button1, "1,1");//1
     pane.add(button2, "2,1");//2
     pane.add(button3, "3,1");//3
     pane.add(button4, "1,2");//
     pane.add(button5, "2,2");
     pane.add(button6, "3,2");
     pane.add(button7, "1,3");
     pane.add(button8, "2,3");
     pane.add(button9, "3,3");
     pane.add(button10, "1,4");
     pane.add(button11, "2,4");
     pane.add(button12, "3,4");

     button1.addActionListener(this);
     button2.addActionListener(this);
     button3.addActionListener(this);
     button4.addActionListener(this);
     button5.addActionListener(this);
     button6.addActionListener(this);
     button7.addActionListener(this);
     button8.addActionListener(this);
     button9.addActionListener(this);
     button10.addActionListener(this);
     button11.addActionListener(this);
     button12.addActionListener(this);



    pack();
    setResizable(false);
     show();

 }
 public void actionPerformed(ActionEvent e) { 
      //code that reacts to the action... 
     Object source = e.getSource();


     if(source == button1){
         upperText.append("1");

     }

    }
}

您可以将它们声明为类字段,以便在整个类中可以访问它们:

public class example extends JFrame implements ActionListener {
    JButton button1;
    JButton button2;
    JButton button3;
    JButton button4;
    JButton button5;
    JButton button6;
    JButton button7;
    JButton button8;
    JButton button9;
    JButton button10:
    JButton button11;
    JButton button12;

    // Constructor
    // Methods
}

也应该考虑使用数组来保存12个按钮。