2011-02-07 37 views
0
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().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(); 
} 
} 

これはテストプログラムです。 addActionListener()メソッドを使用してButtonsクラスにPainterクラスを登録しましたが、なぜそれでもactionperformedメソッドを探すことができないのですか?その後、ペインタクラス自体にボタンを作成すると、それは機能しました。それはなぜそうですか?ボタンがリスナークラスを見つけることができません

答えて

1

少なくとも2つのペインタオブジェクトがあります。表示されているのは、ActionListenerオブジェクトとして使用されているものではありません。

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(); 
    } 
} 

考慮のBorderLayoutのを取る例を:たとえば、あなたはとても(変更は\ !!コメントが付されている)のようなあなたのButtonクラスに表示されている画家オブジェクトへの参照を何を渡した場合contentPaneは次のようになります。

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); 
    } 
} 
+0

実際には2つの画家インスタンスがあります。しかし、あなたが言ったようにしたら、ボタンが頻繁に表示されます。それはボタンとパネルが別々にウィンドウに追加されているからでしょうか? – Gopal

+0

コードにレイアウトの問題があります。 ButtonのsetBoundsメソッドを削除し、適切なレイアウトマネージャを使用することは、おそらく多くの助けになります。あなたはトップレベルのウィンドウcontentPaneがデフォルトでBorderLayoutを使用していることを理解する必要があるので、これに関係なくコンポーネントを追加すると問題に遭遇します。 –

+0

たとえば、BorderLayoutを考慮したレイアウトマネージャの変更については、上のコード補遺を参照してください –