2013-05-26 77 views
0

該代碼工作正常。當我離開它時,代碼將描述所有內容。唯一的問題是我不想點擊同一個地點!我可以找不到解決辦法。有什麼建議嗎?Single Click with mouseListener on JPanel [] []表

for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < cols; j++) { 
      innerCells[i][j] = new JPanel(); 
      innerCells[i][j].setLayout(new BorderLayout()); 
      innerCells[i][j].setBorder(BorderFactory.createLineBorder(lineColor)); 
      innerCells[i][j].setBackground(backgroundColor); 
      innerCells[i][j].addMouseListener(new MouseListener() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        JPanel k = (JPanel) e.getSource(); 
        JLabel l = new JLabel("", JLabel.CENTER); 
        int x = getRows(); 
        int y = getCols(); 
        for (int r = 0; r < getRows(); r++) { 
         for (int c = 0; c < getCols(); c++) { 
          if (innerCells[r][c] == k) { 
           x = r; 
           y = c; 
          } 
         } 
        } 
        if (array[x][y] == 0) { 
         l.setBackground(k.getBackground()); 
         k.add(l); 
         k.setBackground(Color.white); 
         k.revalidate(); 
        } else { 
         l.setBackground(k.getBackground()); 
         k.add(l); 
         k.setBackground(Color.red); 
         k.revalidate(); 
        } 
        randomHits(); 
       } 
+0

你能否嘗試用更清晰的方式闡明你的問題? –

+0

我有一個JPanel [] []表二維。當我點擊一個JPanel時,它的backrounf顏色變成紅色或白色。事情是,當我點擊JPanel我不想再次點擊那裏,但與該代碼我貼了我不能這樣做!我希望我幫你理解我的代碼! –

+0

不要爲每個單元格創建一個新的MouseListener。創建一個MouseListener併爲每個單元格添加相同的偵聽器。 – camickr

回答

0

傢伙感謝幫助我通過設法找到解決辦法嘍!我有隻添加一行代碼,我的意思是隻有一個檢查!解決方案是

如果(k.getComponents()。長度== 0)

這意味着如果它點擊它不是0.謝謝大家的努力!

for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < cols; j++) { 
      innerCells[i][j] = new JPanel(); 
      innerCells[i][j].setLayout(new BorderLayout()); 
      innerCells[i][j].setBorder(BorderFactory.createLineBorder(lineColor)); 
      innerCells[i][j].setBackground(backgroundColor); 
      innerCells[i][j].addMouseListener(new MouseListener() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        JPanel k = (JPanel) e.getSource(); 
        JLabel l = new JLabel("", JLabel.CENTER); 
        int x = getRows(); 
        int y = getCols(); 
        for (int r = 0; r < getRows(); r++) { 
         for (int c = 0; c < getCols(); c++) { 
          if (innerCells[r][c] == k) { 
           x = r; 
           y = c; 
          } 
         } 
        } 
        if (k.getComponents().length == 0) { 
         if (array[x][y] == 0) { 
          l.setBackground(k.getBackground()); 
          k.add(l); 
          k.setBackground(Color.white); 
          k.revalidate(); 
         } else { 
          l.setBackground(k.getBackground()); 
          k.add(l); 
          k.setBackground(Color.red); 
          playerhit++; 
          GameScreen.FinalWinner(); 
          k.revalidate(); 
         } 
         randomHits(); 

        } 
       } 
3

如果面板從未需要應對的另一個鼠標點擊,只需dereigster相關的鼠標監聽...

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class NoMoreClicks { 

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

    public NoMoreClicks() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      add(new JLabel("Single Clicked Pane...")); 
      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        e.getComponent().removeMouseListener(this); 
        JOptionPane.showMessageDialog(e.getComponent(), "Was Clicked"); 
       } 
      }); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 

} 
+0

我嘗試了一個解決方案,用一個布爾型[] []表初始化表中的表false,當我們點擊一​​個JPanel時,這個布爾值變成true。事情是我可以再次擊中同一個點! 無論如何感謝您的興趣! –

+0

所以一旦你點擊一個面板,它再也不能被點擊了嗎?爲什麼不移除鼠標監聽器? – MadProgrammer

+0

我想實現,但我不能。我想要一個JPanel點擊它不能再次點擊! –