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

2 回答 2

3

如果面板从不需要响应另一个鼠标单击,只需取消关联的鼠标侦听器...

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

    }

}
于 2013-05-26T10:42:21.680 回答
0

伙计们感谢我自己设法找到解决方案的帮助!我只需要添加一行代码,我的意思是只检查一次!解决方案是

if (k.getComponents().length == 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();

                    }
                }
于 2013-05-26T18:37:37.380 回答