Java事件处理

时间:2012-07-08 14:36:33

标签: java swing user-interface

当我编译它时,在第33行显示错误:找不到符号。 我打电话给jbtNew.addActionListener(listener),为什么它无法在

中找到jbtNew
(e.getSource() == jbtNew) in line 33.

来自代码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnonymousListenerDemo extends JFrame {

    public AnonymousListenerDemo() {
        // Create four buttons
        JButton jbtNew = new JButton("New");
        JButton jbtOpen = new JButton("Open");
        JButton jbtSave = new JButton("Save");
        JButton jbtPrint = new JButton("Print");
        // Create a panel to hold buttons
        JPanel panel = new JPanel();
        panel.add(jbtNew);
        panel.add(jbtOpen);
        panel.add(jbtSave);
        panel.add(jbtPrint);

        add(panel);
        // Create and register anonymous inner-class listener
        AnonymousListenerDemo.ButtonListener listener = new AnonymousListenerDemo.ButtonListener();
        jbtNew.addActionListener(listener);


    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtNew) //Here it show the problem
            {
                System.out.println("Process New");
            }
        }
    }

    /** Main method */
    public static void main(String[] args) {
        JFrame frame = new AnonymousListenerDemo();
        frame.setTitle("AnonymousListenerDemo");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:3)

这是本地变量 它不存在于构造函数之外。

你需要在课堂上创建一个字段。

答案 1 :(得分:1)

这可能是有效的(以您在此处发布的形式)和@SLaks提到+1,并进行了一些重大更改

如果将所有方法放入分隔的类中以使用put / getClientProperty()

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnonymousListenerDemo {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame("AnonymousListenerDemo");
    // Create four buttons
    private JButton jbtNew = new JButton("New");
    private JButton jbtOpen = new JButton("Open");
    private JButton jbtSave = new JButton("Save");
    private JButton jbtPrint = new JButton("Print");

    public AnonymousListenerDemo() {
        JPanel panel = new JPanel();// Create a panel to hold buttons
        panel.add(jbtNew);
        panel.add(jbtOpen);
        panel.add(jbtSave);
        panel.add(jbtPrint);
        // Create and register anonymous inner-class listener
        jbtNew.addActionListener(new ButtonListener());
        frame.add(panel);
        //frame.setTitle("AnonymousListenerDemo");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtNew) {
                System.out.println("Process New");
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AnonymousListenerDemo();
            }
        });
    }
}