2012-04-01 30 views
5

我正在使用Java中的Netbeans IDE。如何在單擊jPanel(Java)時調用函數?

我有一個JPanel的一種形式。 每個JPanel都有一個gridLayout 3x3,並且在每個位置都有一個表示數字[0,1,2,3,4,5,6,7,8]的圖像(該圖像使用自定義類創建,而不僅僅是擬合在實驗室中的圖像)。

我希望能夠在面板交換兩幅圖像,當用戶點擊他們(首次點擊:不採取行動,第二次點擊:開關安裝在JPanel組件中的兩個圖像)。

我已經創建了一個功能exchangeComponents和用測試碼(如:

exchangeComponents (0,8,jPanel1) 

它交換正確地)(位於位置1(第一行,第一列中的圖像,並在情況2第3行,第3列) 。

的功能的creted如下:

public void exchangeComponents(int component1,int component2,JPanel jpanel){ 
    try{ 
    Component aux1 = jpanel.getComponent(component1); 
    Point aux1Loc = aux1.getLocation(); 
    Component aux2 = jpanel.getComponent(component2); 
    Point aux2Loc = aux2.getLocation(); 
    aux1.setLocation(aux2Loc); 
    aux2.setLocation(aux1Loc); 
    } 
    catch (java.lang.ArrayIndexOutOfBoundsException ex){ /* error! bad input to the function*/ 
     System.exit(1); 
    } 
} 

我想我neeed爲具有調用函數exchangeComponents()的事件時,用戶C舔jPanel1上的圖像之一,但我應該怎麼做?以及如何檢查用戶選擇了哪些組件(圖像)? 我只知道,當我創建一個按鈕,如果在創建上有一個點擊(從IDE)像

private void button1ActionPerformed(java.awt.event.ActionEvent evt) { 
// some code.. 
} 

一個事件,我填寫代碼被執行。

非常感謝您提供任何提示。

回答

4

您需要將相同的鼠標監聽器添加到你的JLabel或任何容器,你必須爲你的圖片,如:

img1.addMouseListener(this); 
img2.addMouseListener(this); 

等。,然後檢測你點擊了哪個的JLabel與MouseEvent.getSource();,這樣

boolean hasclicked1=false; 
JLabel click1label=null; 

public void mouseClicked(MouseEvent me){ 
    if(!hasclicked1){ //clicked first pic 
    hasclicked1 = true; 
    click1label = (JLabel) me.getSource(); 
    } else { //clicked second pic 
    hasclicked1 = false; 
    exchangeComponents(click1label, (JLabel) me.getSource(), /*your jpanel here*/); 
    } 
    //now change exchangeComponents so it uses JLabels as parameters 
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){ 
    try{ 
    Component aux1 = component1; 
    Point aux1Loc = aux1.getLocation(); 
    Component aux2 = component2; 
    Point aux2Loc = aux2.getLocation(); 
    aux1.setLocation(aux2Loc); 
    aux2.setLocation(aux1Loc); 
    } catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/ 
    System.exit(1); 
    } 
} 

如果你不使用的JLabel的圖像,雖然,在任何你正在使用的代碼替換的JLabel ...

編輯:對不起,我不認爲我做了這個不清楚,但你的類與方法exchangeComponents必須實現MouseListener。然後,在mouseClicked事件中輸入我給它的代碼。確保在課堂中包含變量hasclicked1click1label。讓你上課像這樣

public class ComponentExchanger implements MouseListener { 
boolean hasclicked1=false; 
JLabel click1label=null; 
JPanel mainPanel; 
public ComponentExchanger(){ 
    //create JFrame, JPanel, etc. 
    JFrame f=new JFrame(); 
    //etc. 
    mainPanel=new JPanel(); 
    f.add(mainPanel); 
    //set layout of panel, etc. 
    for(int i=0;i<9;i++){ 
     JLabel l=new JLabel(/*label image here*/); 
     Point loc=new Point(/*coordinates here*/); 
     l.setLocation(loc); 
     mainPanel.add(l); 
     /*more code*/ 
     f.setVisible(true); 
    } 
} 

public static void main(String args[]){ 
    new ComponentExchanger(); 
} 


public void mouseClicked(MouseEvent me){ 
    if(!hasclicked1){ //clicked first pic 
    hasclicked1 = true; 
    click1label = (JLabel) me.getSource(); 
    } else { //clicked second pic 
    hasclicked1 = false; 
    exchangeComponents(click1label, (JLabel) me.getSource(), mainPanel); 
    } 
    //now change exchangeComponents so it uses JLabels as parameters 
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){ 
    try{ 
    Component aux1 = component1; 
    Point aux1Loc = aux1.getLocation(); 
    Component aux2 = component2; 
    Point aux2Loc = aux2.getLocation(); 
    aux1.setLocation(aux2Loc); 
    aux2.setLocation(aux1Loc); 
    } catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/ 
    System.exit(1); 
    } 
} 

//Also, you will need to include the other mouselistener implemented methods, just 
//leave them empty 
} 
+0

謝謝。這很清楚..但與img1.addMouseListener(this);我通過表單本身,它說「錯誤:預期的MouseListener」。所以呢? – dragonmnl 2012-04-02 13:13:24

+0

好吧,我做了它(做了一個類getComponent實現MouseListener)並使用img1.addMouseListener(新的ComponentListener())。現在的問題是..我爲每個組件添加了一個偵聽器,但是當我點擊任何組件時(圖片),事件根本沒有被觸發 – dragonmnl 2012-04-02 14:15:08

+0

對不起,我認爲我沒有說清楚,但是你的班級需要實現MouseListener,然後您需要將偵聽器添加到正在切換的每個組件。 @dragonml – DankMemes 2012-04-03 20:49:52

1

首先,是技術是方法沒有的功能。 有幾種方法可以做到這一點。你可以繼續使用actionListener,但你可能需要按鈕或其他東西。 或者您可以使用MouseListener,並檢測面板某個區域上的點擊。 對於切換算法,可能是2個圖像的數組。每次點擊增加1個變量。當係數爲2時,它復位回0

clicks++; //every time the mouse is clicked; clicks starts out at 0 
if(clicks == 2){ 
clicks = 0; //at the end of the listener method 
} 

在第一點擊點擊圖像進入第一陣列插槽,因爲用戶點擊一次。

clickImage = imageArray[clicks]; 

在第二次單擊時,由於檢測到2次單擊,其他單擊的圖像將進入第二個陣列插槽。在這種情況下,您的exchangeComponents方法將在偵聽器方法的末尾執行,參數爲imageArray [1],imageArray [2],。

您可以應用此整數或什麼,只是保存在數組中的價值和使用遞增和重置變量。

相關問題