Java 按钮可以';找不到侦听器类

Java 按钮可以';找不到侦听器类,java,swing,event-handling,listener,Java,Swing,Event Handling,Listener,这是一个测试程序。我使用addActionListener()方法将Painter类注册到Buttons类,那么为什么它仍然不能查找actionperformed方法呢?然后,当我在painter类中创建一个按钮时,它工作了。为什么会这样?您至少有两个画师对象。显示的对象不是用作ActionListener对象的对象。例如,如果您像这样向Button类传递对正在显示的Painter对象的引用(更改由\!!注释表示),该怎么办 考虑contentPane的BorderLayout的示例如下: im

这是一个测试程序。我使用
addActionListener()
方法将
Painter
类注册到Buttons类,那么为什么它仍然不能查找
actionperformed
方法呢?然后,当我在painter类中创建一个按钮时,它工作了。为什么会这样?

您至少有两个画师对象。显示的对象不是用作ActionListener对象的对象。例如,如果您像这样向Button类传递对正在显示的Painter对象的引用(更改由\!!注释表示),该怎么办

考虑contentPane的BorderLayout的示例如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Painter extends JPanel implements ActionListener {
    private int x = 30, y = 30;

    // remove the blocked comment to make it run
    /*
     * public Painter(){ Buttons b = new Buttons(new String("Click to paint"));
     * b.addActionListener(this); add(b); }
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(44, 55, 66));
        g.fillRect(200, 200, x, y);
    }

    public void actionPerformed(ActionEvent e) {
        x = 600;
        y = 600;
        repaint();
    }
}

class Buttons extends JButton {
    public Buttons(String Tag, Painter p) {  \\!!
        super(Tag);
        setBounds(20, 20, 150, 50);
        // !! Painter p = new Painter();// comment it out
        addActionListener(p);// comment it out
    }
}

class Window extends JFrame {
    public Window() {
        Painter p = new Painter();// !!
        Buttons b = new Buttons("Click Me", p);// comment it out //!!
        getContentPane().add(b);// comment it out
        getContentPane().add(p);
        getContentPane().setBackground(Color.WHITE);
        setSize(700, 700);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

public class PaintOnEvent {
    public static void main(String[] args) {
        Window w = new Window();
    }
}

你说得对,实际上有两个画家的例子。但是,当我按照你说的那样做时,按钮经常被显示出来。是因为按钮和面板分别添加到窗口中吗?代码中存在一些布局问题。摆脱按钮的setBounds方法并使用适当的布局管理器可能会有很大帮助。您需要了解,顶级窗口内容窗格默认使用BorderLayout,因此,如果您添加组件时不考虑这一点,您将遇到麻烦。例如,请参阅上面关于考虑BorderLayout的布局管理器更改的代码附录
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Painter extends JPanel implements ActionListener {
    private int x = 30, y = 30;

    // remove the blocked comment to make it run
    /*
     * public Painter(){ Buttons b = new Buttons(new String("Click to paint"));
     * b.addActionListener(this); add(b); }
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(44, 55, 66));
        g.fillRect(200, 200, x, y);
    }

    public void actionPerformed(ActionEvent e) {
        x = 600;
        y = 600;
        repaint();
    }
}

class Buttons extends JButton {
    public Buttons(String Tag, Painter p) {  \\!!
        super(Tag);
        setBounds(20, 20, 150, 50);
        // !! Painter p = new Painter();// comment it out
        addActionListener(p);// comment it out
    }
}

class Window extends JFrame {
    public Window() {
        Painter p = new Painter();// !!
        Buttons b = new Buttons("Click Me", p);// comment it out //!!
        getContentPane().add(b);// comment it out
        getContentPane().add(p);
        getContentPane().setBackground(Color.WHITE);
        setSize(700, 700);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

public class PaintOnEvent {
    public static void main(String[] args) {
        Window w = new Window();
    }
}
class Window extends JFrame {
    public Window() {
        Painter p = new Painter();
        Buttons b = new Buttons("Click Me", p); // !!
        b.setPreferredSize(new Dimension(150, 50));

        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // !!
        btnPanel.setOpaque(false); // !!
        btnPanel.add((b)); // !!
        p.setOpaque(false); // !!

        getContentPane().add(btnPanel, BorderLayout.NORTH); // !!
        getContentPane().add(p, BorderLayout.CENTER); // !!
        getContentPane().setBackground(Color.WHITE);
        ((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // !!
        // !! setSize(700, 700);
        setPreferredSize(new Dimension(700, 700)); // !!
        pack(); // !!
        setLocationRelativeTo(null); // !!
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}