public void setJPanel1()
{
pl1 = new JPanel(new FlowLayout());
pl1.setBackground(Color.pink);
JButton jb1 = new JButton("start");
JButton jb2 = new JButton("stop");
pl1.add(jb1);
pl1.add(jb2);

MyAL al = new MyAL();
jb1.addActionListener(al);
jb2.addActionListener(al);
}

public void setJPanel2(){
pl2 = new JPanel(new FlowLayout());
pl2.setBackground(Color.cyan);
JLabel jl = new JLabel("****");
pl1.add(jl);
}
// 2个按钮的监听
class MyAL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
                    这里的jl变量接受不到上面的 有什么变法接受么?
jl.setText(e.getActionCommand());

} }
内部类怎么接受 外部类方法中的变量?

解决方案 »

  1.   

    呵呵,主窗体实现 ActionListener 接口,然后写public void actionPerformed(ActionEvent e)方法就可以得到了啊
      

  2.   

    主窗体实现了 现在问题是public void actionPerformed(ActionEvent e)中的方法需要引用上面标签变量 可能不能引用有什么变法可以引用这个变量
      

  3.   

    jl 现在是方法setJPanel2内的局部变量,把它声明为类成员变量就可以了。
      

  4.   

    给楼主一个超级简单的计算器示例,主要是想让楼主了解 Action 部分应该写成内部类。// Calculator 界面类
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;public class Calculator
    {
    JFrame myFrame = new JFrame("计算器"); 
    Container contentPane = myFrame.getContentPane();

    JTextArea txt1 = new JTextArea();
    JTextArea txt2 = new JTextArea();
    JTextArea txt3 = new JTextArea();
    JComboBox cmbOperation = new JComboBox();
    JButton btnOK = new JButton("确定");

    String[] operation = {"+", "-", "*", "/"};

    public Calculator()
    {
    contentPane.setLayout(new GridLayout(5, 1));
    txt1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    txt2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    txt3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    btnOK.addActionListener(btnOKAction);

    cmbOperation.addItem("+");
    cmbOperation.addItem("-");
    cmbOperation.addItem("*");
    cmbOperation.addItem("/");
    contentPane.add(txt1);
    contentPane.add(cmbOperation);
    contentPane.add(txt2);
    contentPane.add(txt3);
    contentPane.add(btnOK);

    myFrame.setSize(200, 150);
        myFrame.setVisible(true);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    Action btnOKAction = new AbstractAction()
            {
    public void actionPerformed(ActionEvent e)
                    {
                        calculateResult();
                    }
            };
        
        private void calculateResult()
        {
         double value1 = Double.valueOf((String)txt1.getText());
         double value2 = Double.valueOf((String)txt2.getText());
         String operation = cmbOperation.getSelectedItem().toString();
         double result = Compute.getResult(operation, value1, value2);
        
         txt3.setText(String.valueOf(result));
        }
    }// Compute 计算器的计算逻辑类
    public class Compute
    {
        public static double getResult(String operation, double value1, double value2)
        {
            double result = 0;        if (operation.equals("+"))
            {
                result = value1 + value2;
            }
            else if (operation.equals("-")) 
            {
                result = value1 - value2;
            }
            else if (operation.equals("*")) 
            {
                result = value1 * value2;
            }
            else
            {
                result = value1 / value2;
            }        return result;
        }
    }// 测试类
    public class Main
    {
        public static void main(String[] args)
        {
            new Calculator();
        }
    }
    请注意 Calculator 类中的 btnOKAction ,其实我这样写只是个人觉得代码比较容易理解
      

  5.   

    主窗体实现 ActionListener 接口,然后写public void actionPerformed(ActionEvent e)方法
      

  6.   

    主窗体实现 ActionListener 接口,然后写public void actionPerformed(ActionEvent e)方法