Je veux insérer un gridlayout dans un panel. le gridlayout est apparus mais avec une taille très petite comme un point. ce gridLayout content 4 panel.

ci-joint le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
 
public class Move extends JPanel {
 
 
 
 
 
 
    /**
     * Creates new form Move
     * 
     * 
     * 
     */
    public Move() {
       // setLayout(new GridLayout(2,2));
       // setBorder(line);
 
        initComponents();
        ComponentMove listener = new ComponentMove(this);
        //JPanel Pan = new JPanel();
       // setLayout(new java.awt.GridLayout(2, 2));
 
        for(int i=0; i<4; i++) {
            add(createComponent(i+1));
        }
 
        addMouseListener(listener);
        addMouseMotionListener(listener);
 
    }
     private JComponent createComponent(int i) {
        JLabel component=new JLabel(); // ici on peut faire n'importe quel JComponent, JLabel, par exemple
        component.setLocation((int)(Math.random()*100), (int)(Math.random()*100)); // position aléatoire
        component.setSize(125, 125); // taille aléatoire
        component.setIcon(new javax.swing.ImageIcon("C:\\Users\\olfa\\Documents\\NetBeansProjects\\JavaApplication2\\img\\img"+i+".jpg"));
       // component.setEnabled(false); // les composants ne doivent pas intercepter la souris
        return component;
    }
 
     private static class ComponentMove extends MouseAdapter {
 
        private boolean move;
        private int relx;
        private JComponent component;
        private int rely;
        private Container container;
 
        public ComponentMove(Container container) {
            this.container=container;
        }
 
        @Override
        public void mousePressed(MouseEvent e) {
            if ( move ) {
                move=false; // arrêt du mouvement
                component.setBorder(null); // on  supprime la bordure noire
                component=null;
            }
            else {
                component = getComponent(e.getX(),e.getY()); // on mémorise le composant en déplacement
                if ( component!=null ) {
                    container.setComponentZOrder(component,0); // place le composant le plus haut possible
                    relx = e.getX()-component.getX(); // on mémorise la position relative
                    rely = e.getY()-component.getY(); // on mémorise la position relative
                    move=true; // démarrage du mouvement
                    component.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // on indique le composant sélectionné par une bordure noire
                }
            }
        }
 
        private JComponent getComponent(int x, int y) {
            // on recherche le premier composant qui correspond aux coordonnées de la souris
            for(Component component : container.getComponents()) {
                if ( component instanceof JComponent && component.getBounds().contains(x, y) ) {
                    return (JComponent)component;
                }
            }
            return null;
        }
 
        @Override
        public void mouseMoved(MouseEvent e) {
            if ( move ) {
                // si on déplace
                component.setLocation(e.getX()-relx, e.getY()-rely);
            }
        }
 
    }
 
    public static void main(String[] args) {
 
          Border line = BorderFactory.createLineBorder(Color.BLACK);
 
        JFrame frame = new JFrame("exemple");
        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(2,2));
        pan.setBorder(line);
        pan.setSize(8000, 8011);
 
 
        JLabel lab1 = new JLabel();
        lab1.setBorder(line);
        lab1.setSize(124, 125);
        pan.add(lab1);
 
 
        JLabel lab2 = new JLabel();
        lab2.setBorder(line);
        lab2.setSize(124, 125);
        pan.add(lab2);
 
        JLabel lab3 = new JLabel();
        lab3.setBorder(line);
        lab3.setSize(124, 125);
        pan.add(lab3);
 
        JLabel lab4 = new JLabel();
        lab4.setBorder(line);
        lab4.setSize(124, 125);
        pan.add(lab4);
 
        frame.setSize(800,600);
        JLabel image = new JLabel( new ImageIcon("C:\\Users\\olfa\\Documents\\NetBeansProjects\\JavaApplication2\\img\\chat.jpg"));
        frame.setLayout(new FlowLayout());
        frame.add(new Move(),FlowLayout.LEFT);
        frame.add(image);
        frame.add(pan,FlowLayout.CENTER);
 
 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
      // frame.setContentPane(new Move().setBorder(BorderFactory.createLineBorder(Color.yellow)));
 
 
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
 
    }