当我编译它时,在第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);
    }
}

最佳答案

这可能是可行的(采用您在此处发布的形式),@ 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();
            }
        });
    }
}

关于java - Java事件处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11383933/

10-09 03:18