Button can't find the Listener class
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
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();
}
}
import javax.swing.JButton;
public class Buttons extends JButton{
public Buttons(String Tag){
super(Tag);
setBounds(20, 20, 150, 50);
Painter p = new Painter();//comment it out
addActionListener(p);//comment it out
}
}
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame{
public Window(){
Buttons b = new Buttons("Click Me");//comment it out
Painter p = new Painter();
getContentPane().开发者_StackOverflow中文版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();
}
}
This is a testing program. I registered the Painter
class to the Buttons class using addActionListener()
method, then why doesn't it still can't look for the actionperformed
method? Then when I created a button in the painter class itself, it worked. Why is it so?
You have at least two Painter objects. The one being displayed is not the one being used as an ActionListener object. For instance, what if you pass a reference to the Painter object that is being displayed to your Button class like so (changes are denoted by \!! comments):
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();
}
}
An example that takes into consideration the BorderLayout of the contentPane is as follows:
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);
}
}
精彩评论