[JAVA] GridBagLayout alignment probleem

Pagina: 1
Acties:

  • Canard
  • Registratie: Oktober 1999
  • Laatst online: 22-08 13:18
Ik heb een Frame die een BorderLayout gebruikt. Daarin zitten 3 Panels op NORTH, CENTER en SOUTH...
Het Panel die in CENTER zit heb ik weer opgedeeld met een GridBagLayout en hierop wil ik wat TextFields en Labels zetten. Dit lukt wel maar ik krijg deze componenten niet op de juiste positie. Alle componenten worden namelijk verticaal gecentreerd en ik wil ze in de top van het panel hebben...

Wat doe ik hier fout?

code:
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
GridBagLayout myGBL = new GridBagLayout();
GridBagConstraints myGBLConstraints = new GridBagConstraints();

mainFrame.setLayout(new BorderLayout());
Panel myPanel1 = new Panel(myGBL);
Panel myPanel2 = new Panel(myGBL);
Panel myPanel3 = new Panel(myGBL);
myGBLConstraints.fill = GridBagConstraints.NONE;
myGBLConstraints.weightx = 1.0;
myGBLConstraints.anchor = GridBagConstraints.NORTHWEST;

Label myLabel1 = new Label("Label in NORTH-panel");
myGBLConstraints.gridx = 0;
myGBLConstraints.gridy = 0;
myGBL.setConstraints(myLabel1,myGBLConstraints);
myPanel1.add(myLabel1,myGBLConstraints);

TextField myTextField1 = new TextField("TextField1 in CENTERED-panel");
myGBLConstraints.gridx = 0;
myGBLConstraints.gridy = 0;
myGBL.setConstraints(myTextField1,myGBLConstraints);
myPanel2.add(myTextField1,myGBLConstraints);

TextField myTextField2 = new TextField("TextField2 in CENTERED-panel");
myGBLConstraints.gridx = 0;
myGBLConstraints.gridy = 1;
myGBL.setConstraints(myTextField2,myGBLConstraints);
myPanel2.add(myTextField2,myGBLConstraints);

Button myButton1 = new Button("Button in SOUTH-panel");
myGBLConstraints.gridx = 0;
myGBLConstraints.gridy = 0;
myGBL.setConstraints(myButton1,myGBLConstraints);
myPanel3.add(myButton1,myGBLConstraints);

myPanel1.setBackground(Color.BLUE);
mainFrame.add(myPanel1,BorderLayout.NORTH);

myPanel2.setBackground(Color.RED);
mainFrame.add(myPanel2,BorderLayout.CENTER);

myPanel3.setBackground(Color.YELLOW);
mainFrame.add(myPanel3,BorderLayout.SOUTH);

mainFrame.setSize(300, 300);
mainFrame.setTitle("Test");
mainFrame.setVisible(true);

  • windancer
  • Registratie: Maart 2000
  • Laatst online: 18-08 22:36
Een GridBagLayout plaatst de elementen volgens een bepaald gewicht, voor de x-richting zet je dit gewicht ook :

code:
1
myGBLConstraints.weightx = 1.0;


voor de y-richting zet je die gewichten niet en worden de elementen dus gelijkelijk in die richting verdeeld.

  • Canard
  • Registratie: Oktober 1999
  • Laatst online: 22-08 13:18
ok, bedankt.. dat werkt...

maar nu is het dus zo dat het 1e TextField bovenaan komt en het 2e TextField kom in het midden van het panel... die wil ik meteen onder het 1e TextField hebben...

  • windancer
  • Registratie: Maart 2000
  • Laatst online: 18-08 22:36
Ik geloof niet dat dit op een standaard ondersteund wordt. Wat je wel kunt proberen is een leeg label onder je TextFields te plaatsen met een hoger gewicht dan je TextFields.