2011-09-07 9 views
6

taşıma Olay whitch Jpanel uzanır:MouseListener'da tıklatılan JPanel bileşenini belirleyin. Bir sınıf olması

public class ButtonPanel extends JPanel { 

    private label; 

    public ButtonPanel() { 
     label=new JLabel("waiting for click"); 
     add(label); 
    } 

    public void setButtonText() { 
     label.setText("just clicked"); 
    } 

} 

Ben JFrame eklenir bu sınıfın birkaç kopyası olabilir. MouseAdapter sınıfının bir instanse'si oluşturmak ve daha sonra bunları JFrame'imdeki tüm ButtonPanel bileşenlerine bir fare dinleyicisi olarak eklemek istiyorum. Ben meen:

ButtonPanel butt1 = new ButtonPanel(); 
ButtonPanel butt2 = new ButtonPanel(); 
ButtonPanel butt3 = new ButtonPanel(); 
//... here goes code which add ButtonPanels to JFrame 

MouseAdapterMod mam = new MouseAdapterMod(); 
butt1.addMouseListener(mam); 
butt2.addMouseListener(mam); 
butt3.addMouseListener(mam); 

MouseAdapterMod sınıf I diğerinden ayrı ve kendi paketinde bulmak istiyoruz. Gelmeli şuna benzer:

public class MouseAdapterMod extends MouseAdapter { 

    public void mouseClicked(MouseEvent e) { 
     //here goes the code of calling setButtonText method of ButtonPanel component on which the event had occurred 
    } 
} 

Yani sorun ben ButtonPanel olay üretmek ve() metodu bu bileşen setButtonText tekabül çağrı belirlemek yapmak mouseClicked yönteminin nasıl uygulanacağını bilmiyorum olmasıdır. Bunu yapmayı bilen var mı?

Bunu, ButtonPanel sınıfında olay işleme işlevselliği dahil ederek başarabileceğimi biliyorum, ancak bu benim için uygun bir yol değil, çünkü yukarıda anlattığım gibi sınıf yapısını korumak istiyorum ve yalnızca bir MouseAdapterMod sınıfı örneğine sahip olmak istiyorum Tüm ButtonPanels'ları ele almak.

+0

Kudos. Paylaşılan bir dinleyici yazarken kurtarmaya bir kez daha –

cevap

14

MouseEvent#getSource yöntem nesne tıklandığında hangi döndürür: Eğer mouseClicked basın çalışmak ve için çünkü mousePressed veya mouseReleased yerine mouseClicked için dinleme kapalı genellikle daha iyi konum, yorum not olarak

public class MouseAdapterMod extends MouseAdapter { 

    // usually better off with mousePressed rather than clicked 
    public void mousePressed(MouseEvent e) { 
     ButtonPanel btnPanel = (ButtonPanel)e.getSource(); 
     btnPanel.setButtonText(); 
    } 
} 

serbest bırakma aynı noktadan olmalı ve eğer fare hafif bir miktar kaydırıyorsa, tıklama kayıt olmayacaktır.

Benim test programı: En son paragraf için

import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.*; 

import javax.swing.*; 

public class MainForButtonPanel extends JPanel { 
    public MainForButtonPanel() { 
     setLayout(new GridLayout(4, 4)); 

     MouseAdapter myMA = new MouseAdapterMod(); 

     for (int i = 0; i < 4; i++) { 
     for (int j = 0; j < 4; j++) { 
      ButtonPanel btnPanel = new ButtonPanel(); 
      btnPanel.addMouseListener(myMA); 
      add(btnPanel); 
     } 
     } 

    } 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("MainForButtonPanel"); 
     frame.getContentPane().add(new MainForButtonPanel()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 

class ButtonPanel extends JPanel { 

    private static final int TIMER_DELAY = 2000; 
    private static final String JUST_CLICKED = "just clicked"; 
    private static final String WAITING_FOR_CLICK = "waiting for click"; 
    private static final Color CLICKED_COLOR = Color.pink; 
    private JLabel label; 

    public ButtonPanel() { 
     label = new JLabel(WAITING_FOR_CLICK); 
     add(label); 
    } 

    public void setButtonText() { 
     label.setText(JUST_CLICKED); 
     setBackground(CLICKED_COLOR); 

     new Timer(TIMER_DELAY, new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
      label.setText(WAITING_FOR_CLICK); 
      setBackground(null); 
      ((Timer)ae.getSource()).stop(); 
     } 
     }).start(); 
    } 

} 

class MouseAdapterMod extends MouseAdapter { 

    // usually better off with mousePressed rather than clicked 
    public void mousePressed(MouseEvent e) { 
     ButtonPanel btnPanel = (ButtonPanel)e.getSource(); 
     btnPanel.setButtonText(); 
    } 
} 
+1

+1, 'getSource()' yazın. – camickr

+0

+1 güzel örnek :-) – kleopatra

+0

Mükemmel örnek. Ben sorduğumdan daha fazlasını elde ediyorum. Ancak "Timer" örneğinin "setButtonText()" yönteminde ne olduğundan emin olmadığından emin olun. Üzgünüm .. java'da acemi;) .. Ama her durumda, Çok teşekkür ederim! – Gubert