2014-01-11 87 views
-1

所以這是我的問題。我有一個8 * 8的面板網格,全白色。然後,當點擊它們中的一個時,它應該變成隨機顏色。我現在唯一的問題是,我不知道如何查看用戶是否在特定面板中單擊鼠標。這裏是我到目前爲止的代碼(我打算以後實行隨機元) `如何知道某人是否點擊了特定的面板?

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class GridOfPanels extends JPanel{ 
    int x, y; 
    public GridOfPanels(){ 
     JPanel content = new JPanel(new GridLayout(8,8)); 
     for(int i = 0; i < 64; i++){ 
      JPanel panel = new JPanel(); 
      panel.setBackground(Color.white); 
      content.add(panel); 
     } 
     this.add(content); 
    } 
    public GridOfPanels(Color backColor){ 
     setBackground(backColor); 
     addMouseListener(new PanelListener()); 
     x = 200; 
     y = 200; 
    } 
    private class PanelListener extends MouseAdapter{ 
     public void mousePressed(MouseEvent e){ 
      x = e.getX(); 
      y = e.getY(); 
      repaint(); 
     } 
    } 
    public static void main(String[] args){ 
     JFrame theGUI = new JFrame(); 
     theGUI.setTitle("Grid"); 
     theGUI.setVisible(true); 
     theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     theGUI.setSize(400,400); 
     Rectangle z = new Rectangle(x, y, 50, 50); 
    } 
} 

`

+0

考慮將JButton替換爲ActionListener。點擊或改變點燃時的BG顏色。 –

回答

1

您必須爲每個可點擊的對象添加一個偵聽器。下面是一個工作示例:

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

public class TestFrame extends JFrame{ 

    public TestFrame(int size){ 
     JPanel content = new JPanel(new GridLayout(size, size)); 
     JPanel[] panel = new JPanel[size * size]; 
     PanelListener listener = new PanelListener(); 

     for(int i = 0; i < panel.length; i++){ 
      panel[i] = new JPanel(); 
      panel[i].setBackground(Color.white); 
      panel[i].addMouseListener(listener); 
      content.add(panel[i]); 
     } 

     this.add(content); 
    } 

    // MouseListener offers the method mouseClicked(MouseEvent e) 
    private class PanelListener implements MouseListener { 

     @Override 
     public void mouseClicked(MouseEvent event) { 
        /* source is the object that got clicked 
        * 
        * If the source is actually a JPanel, 
        * then will the object be parsed to JPanel 
        * since we need the setBackground() method 
        */ 
      Object source = event.getSource(); 
      if(source instanceof JPanel){ 
       JPanel panelPressed = (JPanel) source; 
       panelPressed.setBackground(Color.blue); 
      } 
     } 

     @Override 
     public void mouseEntered(MouseEvent arg0) {} 

     @Override 
     public void mouseExited(MouseEvent arg0) {} 

     @Override 
     public void mousePressed(MouseEvent arg0) {} 

     @Override 
     public void mouseReleased(MouseEvent arg0) {} 

    } 

    public static void main(String[] args){ 
     TestFrame theGUI = new TestFrame(8); 
     theGUI.setTitle("Grid"); 
     theGUI.setVisible(true); 
     theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     theGUI.setSize(400,400); 

    } 
} 
+0

好的,非常感謝。只有一個問題,@Override的東西是什麼?另外,getSource的功能是什麼?謝謝! – user3185730

+1

@Override是[Java註釋](http://en.wikipedia.org/wiki/Java_annotation)。我的示例中的覆蓋註釋是可選的,它讓編譯器檢查我確實重寫了MouseListener類的mouseClicked()方法。大多數IDE都會自動添加這些註釋。 getSource()方法返回觸發事件的對象,在我的例子中是那個被鼠標點擊的JPanel。 – ChopChop

+0

好的,非常感謝! – user3185730

0

你要的MouseListener添加到面板之一。只有一個面板會對點擊事件做出反應。在監聽器中將源代碼轉換爲JPanel並更改顏色。

相關問題