salut , je veux créer un JPanel qui soit resizable par la souris
J'ai donc créé mon PropreJPanel avec:
- un northPane qui simule le comportement de la barre d'une JInternalFrame
-un centerPane qui implemente MouseInputListener et qui va gerer le resinzing
le code marche
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 /* import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Cursor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JTextField; import javax.swing.event.MouseInputListener; import sun.awt.VerticalBagLayout; /** * @author komando */ public class ProprePanel extends JPanel{ private static int HGAP = 5; private static int VGAP = 0; private static int HEIGHT = 36; private static String closeStr = "x"; private static String expStr = "+"; private static String cloStr = "-"; private boolean isClosed; private String title = ""; //northPane contient les bouton - et x semblable a ceux d'une Frame private JPanel northPane = new JPanel(new BorderLayout(HGAP,VGAP)); //contient n'importe quel component private JPanel centerPane = new CenterPane(); private JPanel northEastPane = new JPanel(); private JPanel northCenterPane = new JPanel(); private JLabel mainLabel = new JLabel(); private JButton mainButton = new JButton(); private JButton closeButton = new JButton(); private JButton expandButton = new JButton(); //in order to interact with ActionListener JPanel myPane; /** * constructor */ public ProprePanel(String title){ super(new BorderLayout()); this.setBorder(BorderFactory.createBevelBorder(0)); this.title=title; this.init(); this.myPane=this; } /** * add panes to the main one * */ public void init(){ this.add(northPane, BorderLayout.NORTH); this.add(centerPane, BorderLayout.CENTER); this.setActions(); this.openNorthPane(); isClosed=false; // mainButton.setBorder(BorderFactory.createEtchedBorder()); // closeButton.setBorder(BorderFactory.createEtchedBorder()); // expandButton.setBorder(BorderFactory.createEtchedBorder()); } /** * close main Pane * */ public void closePane(){ centerPane.setVisible(false); closeNorthPane(); this.repaint(); isClosed=true; } /** * open main Pane * */ public void openPane(){ centerPane.setVisible(true); openNorthPane(); this.repaint(); isClosed=false; } /** * the closed northPane Behaviour */ private void closeNorthPane(){ northPane.removeAll(); northPane.add(northCenterPane,BorderLayout.CENTER); northPane.add(northEastPane, BorderLayout.EAST); //get to be changed mainLabel.setText("::"); mainButton.setText(title); expandButton.setText(expStr); closeButton.setText(closeStr); northCenterPane.add(mainLabel); northCenterPane.add(mainButton); northEastPane.add(expandButton); northEastPane.add(closeButton); } /** * the opened northPane behavior * */ private void openNorthPane(){ northPane.removeAll(); northPane.add(mainLabel,BorderLayout.CENTER); northPane.add(northEastPane, BorderLayout.EAST); mainLabel.setText(title); expandButton.setText(cloStr); closeButton.setText(closeStr); northEastPane.add(expandButton); northEastPane.add(closeButton); } private void setActions(){ mainButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { openPane(); } }); expandButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if (isClosed) openPane(); else closePane(); } }); closeButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { myPane.setVisible(false); } }); } public void addCenter(Component comp){ centerPane.add(comp); } public static void main(String[] args) { JFrame fr = new JFrame(); JPanel westPane = new JPanel(); JPanel centerPane = new JPanel(); JSplitPane mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); ProprePanel pn1 = new ProprePanel("magic1"); ProprePanel pn2 = new ProprePanel("magic2"); ProprePanel pn3 = new ProprePanel("magic3"); ProprePanel pn4 = new ProprePanel("magic4"); ProprePanel pn5 = new ProprePanel("magic5"); fr.setLayout(new BorderLayout()); fr.add(mainPane); mainPane.setLeftComponent(westPane); mainPane.setRightComponent(centerPane); VerticalBagLayout layout = new VerticalBagLayout(); westPane.setLayout(layout); westPane.add(pn1); westPane.add(pn2); westPane.add(pn3); westPane.add(pn4); westPane.add(pn5); pn1.addCenter(new JTextField("pn1")); pn2.addCenter(new JTextField("pn2")); pn3.addCenter(new JTextField("pn3")); pn4.addCenter(new JTextField("pn4")); pn5.addCenter(new JTextField("pn5")); fr.setVisible(true); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fr.pack(); } } class CenterPane extends JPanel implements MouseInputListener{ boolean isDrag=false; int HGAP = 0; int VGAP = 5; Cursor c; int lastY; int newY; Dimension dim; JPanel parent=null; public CenterPane(){ super(); setLayout(new BorderLayout(HGAP,VGAP)); //necessaire pour voir le changement du curseur //bref pour voir le centerPane car ses composant le cachent setBorder(BorderFactory.createEtchedBorder(0)); addMouseListener(this); addMouseMotionListener(this); dim=new Dimension(getWidth(),getHeight()); } public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { lastY=e.getY(); } public void mouseReleased(MouseEvent e) { isDrag=false; } public void mouseEntered(MouseEvent e) { //get to be moved c= Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR); setCursor(c); } public void mouseExited(MouseEvent e) { c=null; setCursor(c); } public void mouseDragged(MouseEvent e) { isDrag=true; newY = e.getY(); dim.height=newY; setPreferredSize(dim); parent = (JPanel) getParent(); parent.revalidate(); } public void mouseMoved(MouseEvent e) {} }
Le pb est que je veux que le comportement de resizin' soit possible juste au bord de centerPane et que le curseur de souris puisse changer pour indiquer le resizing
Aussi, je me dis que des composants comme les JFrame possedent déja ce comportement mais j'arrive pas a trouver les methodes qui l'assure
merci pour vos commentaires
Partager