内部类事件处理程序无法访问顶级类字段。为什么?

时间:2011-04-27 16:34:26

标签: java events swing

这是一个非常微不足道的问题。但是Eclipse IDE告诉我它可以解析button1到任何变量。不知道为什么会这样。我正确使用e.getSource()吗? 这是代码:

package SimpleCRUD;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class CRUDFrame extends JFrame {
    public CRUDFrame(){
        super("AppCRUD");
        setLayout(new GridLayout(0,1,4,15));
        //setTitle("Hello");
        JLabel label1;
        JButton button1, button2, button3, button4;
        label1 = new JLabel(" Telephone Book" , JLabel.CENTER);
        label1.setFont (new Font("fallan", 1, 25));
        label1.setToolTipText("What?");
        button1 = new JButton(" Show Contacts ");
        button2 = new JButton(" Add Contact ");
        button3 = new JButton(" Update Number in a Contact ");
        button4 = new JButton(" Delete a Contact ");

        add(label1);
        add(button1);   
        add(button2);  
        add(button3); 
        add(button4);
        ButtonListener onClick = new ButtonListener();
        button1.addActionListener(onClick);
        button2.addActionListener(onClick);
        button3.addActionListener(onClick);
        button4.addActionListener(onClick);

    }


    class ButtonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            //JOptionPane.showMessageDialog(CRUDFrame.this, e.getActionCommand(), "Event fired", JOptionPane.PLAIN_MESSAGE);
            if(e.getSource() == button1)
            System.out.println("HI!");  
        }
    }

}

2 个答案:

答案 0 :(得分:3)

您在构造函数中声明button1而不在类体中声明。

因此它仅在构造函数执行期间可用。

简单修复:

package SimpleCRUD;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class CRUDFrame extends JFrame {

// now it's not in constructor
JButton button1, button2, button3, button4;

public CRUDFrame(){
    super("AppCRUD");
    setLayout(new GridLayout(0,1,4,15));
    //setTitle("Hello");
    JLabel label1;

    label1 = new JLabel(" Telephone Book" , JLabel.CENTER);
    label1.setFont (new Font("fallan", 1, 25));
    label1.setToolTipText("What?");
    button1 = new JButton(" Show Contacts ");
    button2 = new JButton(" Add Contact ");
    button3 = new JButton(" Update Number in a Contact ");
    button4 = new JButton(" Delete a Contact ");

    add(label1);
    add(button1);   
    add(button2);  
    add(button3); 
    add(button4);
    ButtonListener onClick = new ButtonListener();
    button1.addActionListener(onClick);
    button2.addActionListener(onClick);
    button3.addActionListener(onClick);
    button4.addActionListener(onClick);

}


class ButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //JOptionPane.showMessageDialog(CRUDFrame.this, e.getActionCommand(), "Event fired", JOptionPane.PLAIN_MESSAGE);
        if(e.getSource() == button1)
        System.out.println("HI!");  
    }
}

}

答案 1 :(得分:1)

你没有任何课程领域! button1是构造函数的本地。解决方案:在类中声明它以及其他方法或内部类所需的任何其他变量