Points that are not read correctly MouseListener

I am having a problem where I cannot correctly access the instance data of an instance of an instance.

I create a multidimensional array of GridPanels and instantiate each using Point. At first creation, everything works as expected.

pic1 http://img.skitch.com/20100218-fciwr7t73ci2gajafmfxa2yf9q.jpg

However, when I click on the GridPanel, the Listener class always gets the Point from the last created GridPanel ((3, 3) in this case.)

When I pass an int instead of a Point, an int is displayed for the GridPanel that was clicked as you expected.

Does anyone know what is going on here?

thank

import javax.swing.JFrame;

/**
 * Driver class.
 */
public class Test {
    /**
     * The main method.
     * @param args Command line arguments.
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame("TEST");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TestPanel panel = new TestPanel();
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }
}

import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JPanel;

/**
 * Creates a 4 by 4 grid of GridPanels.
 */
public class TestPanel extends JPanel {
    static final int ROW_SIZE = 4;
    static final int COL_SIZE = 4;
    private GridPanel[][] g = new GridPanel[ROW_SIZE][COL_SIZE];

    public TestPanel() {
        Point coords = new Point();

        setLayout(new GridLayout(ROW_SIZE, COL_SIZE));

        for (int i = 0; i < ROW_SIZE; i++) {
            for (int j = 0; j < COL_SIZE; j++) {
                coords.setLocation(i, j);
                g[i][j] = new GridPanel(coords);
                add(g[i][j]);
            }
        }
    }
}

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Contains the MouseListener.
 */
public class GridPanel extends JPanel {

    private JLabel label;
    private Point p;

    public GridPanel(Point p) {
        this.p = p;
        label = new JLabel("" + p);

        add(label);

        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(200, 50));

        addMouseListener(new SelectListener());
    }

    private class SelectListener extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            label.setText("" + p);
        }
    }
}
+3
source share
4 answers

, , coords. . , , , .

label = new JLabel("" + p);

, p. p , .

, -

this.p = p;

this.p = new Point(p); // Create a defensive copy.

, . ,

Point p = new Point(3, 4);
Point p2 = p;
p.x = 7;
System.out.println(p2.x);

7, , . = .

(, , !)

+2

, . , . - .

:

public TestPanel() {

    setLayout(new GridLayout(ROW_SIZE, COL_SIZE));

    for (int i = 0; i < ROW_SIZE; i++) {
        for (int j = 0; j < COL_SIZE; j++) {

            g[i][j] = new GridPanel(new Point(i, j));

            add(g[i][j]);
        }
    }

}
+1

i change setPreferredSize ( (w, h)); . . gridpanel .... (1200 800) (1170,920), JLabel .

0

frame.pack(); frame.setSize(W, H);

setPreferredSize ( (x, y)); setBorder (BorderFactory.createLineBorder(Color.red)); JLabel x = w/col_size; y = h/row_size;

, Test.java, ;

0
source

Source: https://habr.com/ru/post/1733413/


All Articles