Question

The code is working fine.When i left click it does everything the code discribes.The only problem is that i don't want to click an the same spot!I can't find a solution to that.Any suggestions?

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();
                }
Was it helpful?

Solution 2

Guys thanks for help by i manage to find the solution myself!I had to add only one line of code,i mean only one checking!The solution is

if (k.getComponents().length == 0)

that means if its clicked its not 0.Thank you all for trying!

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

                    }
                }

OTHER TIPS

If the panels never need to respond to another mouse click, simply dereigster the associated mouse listener...

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

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top