给你个例子 import java.awt.*; import java.util.*; import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); } public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setFont(new Font("Helvetica", Font.PLAIN, 14)); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("Button1", gridbag, c); makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button7", gridbag, c); c.gridwidth = 1; //reset to the default c.gridheight = 2; c.weighty = 1.0; makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default c.gridwidth = GridBagConstraints.REMAINDER; //end row c.gridheight = 1; //reset to the default makebutton("Button9", gridbag, c); makebutton("Button10", gridbag, c); setSize(300, 100); } public static void main(String args[]) { Frame f = new Frame("GridBag Layout Example"); GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1); f.pack(); f.setSize(f.getPreferredSize()); f.show(); } }
qljsd(我本善良) 这个就是API上的例子,我昨天研究了一天还是如在云雾里,不明白:-(
从书上扒下来的 The GridBag Layout Manager GridBag is by far the most powerful layout manager. It can perform the work of the Flow, Grid, and Border layouts if appropriately programmed, and is capable of much more besides, often without the need for nesting multiple panels so often required with the other layout managers. The GridBag layout divides its container into an array of cells, but (unlike the cells of a Grid layout manager) different cell rows can have different heights, and different cell columns can have different widths. A component can occupy part or all of a region that is based on either a single cell or a rectangle made up of multiple cells. A GridBag layout manager requires a lot of information to know where to put a component. A helper class called GridBagConstraints is used to hold all the layout position information. When you add a component, you use the add(Component, Object) version of the add() method, passing an instance of GridBagConstraints as the Object parameter. 1. import java.awt.*; 2. 3. public class GB1 extends Panel { 4. private Panel tallPanel = new Panel(); 5. private Panel tallPanel2 = new Panel(); 6. 7. public GB1() { 8. tallPanel.setLayout(new GridLayout(3, 1)); 9. tallPanel.add(new Button(“Press”)); 10. tallPanel.add(new Button(“Any”)); 11. tallPanel.add(new Button(“One”)); 12. 13. tallPanel2.setLayout(new GridLayout(3, 1)); 14. tallPanel2.add(new Button(“Don’t”)); 15. tallPanel2.add(new Button(“Press”)); 16. tallPanel2.add(new Button(“These”)); 17. 18. setLayout(new GridBagLayout()); 19. 20. GridBagConstraints c = new GridBagConstraints(); 21. c.gridx = 0; c.gridy = 0; 22. add(new Button(“topleft”), c); 23. c.gridx = 1; 24. add(new Button(“topmiddle”), c); 25. c.gridx = 2; 26. add(new Button(“topright”), c); 27. 28. c.gridx = 0; c.gridy = 1; 29. add(new Button(“lefthandsidemiddle”), c); 30. c.gridx = 1; 31. add(tallPanel, c); 32. 33. c.gridy = 2; // note, sets _y_ 34. add(new Button(“bottomcenter”), c); 35. c.gridx = 2; 36. add(tallPanel2, c); 37. } 38. 39. public static void main(String args[]) { 40. Frame f = new Frame(“GridBag 1 example”); 41. f.add(new GB1()); 42. f.pack(); 43. f.setVisible(true); 44. } 45. }
为什么我的代码不能够让我的component折行显示呢? 总是只显示JComboBox或者是显示JComboBox和JLabelprivate Component createComponent(){ JPanel pane = new JPanel(); pane.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints();
pane.setLayout(gbl); //set to change component size both in heritical and vertical gbc.fill = GridBagConstraints.BOTH;JComboBox comboBox = new JComboBox(); gbc.weightx = 1.0; gbc.gridx = 0; gbc.gridy = GridBagConstraints.RELATIVE; //c.gridwidth = GridBagConstraints.RELATIVE; gbl.setConstraints(comboBox,gbc); pane.add(comboBox);JLabel label = new JLabel("create small button"); //gbc.gridy = 1; gbc.gridx = GridBagConstraints.RELATIVE; gbc.gridwidth = GridBagConstraints.REMAINDER; //我想折行 gbl.setConstraints(label, gbc); pane.add(label);JList dirlist = new JList(); gbc.weightx = 0.0; //c.gridx = 0; gbc.gridy = 1; //c.gridwidth = GridBagConstraints.RELATIVE; gbl.setConstraints(dirlist, gbc); pane.add(dirlist); //这个list总是没有显示出来JList filelist = new JList(); gbc.gridy = GridBagConstraints.RELATIVE; gbc.gridwidth = GridBagConstraints.REMAINDER; gbl.setConstraints(filelist, gbc); pane.add(filelist); return pane; }
import java.awt.*;
import java.util.*;
import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
} public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints(); setFont(new Font("Helvetica", Font.PLAIN, 14));
setLayout(gridbag); c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c); c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c); setSize(300, 100);
} public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1);
f.pack();
f.setSize(f.getPreferredSize());
f.show();
}
}
这个就是API上的例子,我昨天研究了一天还是如在云雾里,不明白:-(
The GridBag Layout Manager
GridBag is by far the most powerful layout manager. It can perform the
work of the Flow, Grid, and Border layouts if appropriately programmed,
and is capable of much more besides, often without the need for nesting multiple
panels so often required with the other layout managers.
The GridBag layout divides its container into an array of cells, but (unlike
the cells of a Grid layout manager) different cell rows can have different
heights, and different cell columns can have different widths. A component
can occupy part or all of a region that is based on either a single cell or a rectangle
made up of multiple cells. A GridBag layout manager requires a lot of
information to know where to put a component. A helper class called
GridBagConstraints is used to hold all the layout position information.
When you add a component, you use the add(Component, Object) version
of the add() method, passing an instance of GridBagConstraints as the
Object parameter.
1. import java.awt.*;
2.
3. public class GB1 extends Panel {
4. private Panel tallPanel = new Panel();
5. private Panel tallPanel2 = new Panel();
6.
7. public GB1() {
8. tallPanel.setLayout(new GridLayout(3, 1));
9. tallPanel.add(new Button(“Press”));
10. tallPanel.add(new Button(“Any”));
11. tallPanel.add(new Button(“One”));
12.
13. tallPanel2.setLayout(new GridLayout(3, 1));
14. tallPanel2.add(new Button(“Don’t”));
15. tallPanel2.add(new Button(“Press”));
16. tallPanel2.add(new Button(“These”));
17.
18. setLayout(new GridBagLayout());
19.
20. GridBagConstraints c = new GridBagConstraints();
21. c.gridx = 0; c.gridy = 0;
22. add(new Button(“topleft”), c);
23. c.gridx = 1;
24. add(new Button(“topmiddle”), c);
25. c.gridx = 2;
26. add(new Button(“topright”), c);
27.
28. c.gridx = 0; c.gridy = 1;
29. add(new Button(“lefthandsidemiddle”), c);
30. c.gridx = 1;
31. add(tallPanel, c);
32.
33. c.gridy = 2; // note, sets _y_
34. add(new Button(“bottomcenter”), c);
35. c.gridx = 2;
36. add(tallPanel2, c);
37. }
38.
39. public static void main(String args[]) {
40. Frame f = new Frame(“GridBag 1 example”);
41. f.add(new GB1());
42. f.pack();
43. f.setVisible(true);
44. }
45. }
总是只显示JComboBox或者是显示JComboBox和JLabelprivate Component createComponent(){
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
pane.setLayout(gbl);
//set to change component size both in heritical and vertical
gbc.fill = GridBagConstraints.BOTH;JComboBox comboBox = new JComboBox();
gbc.weightx = 1.0;
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
//c.gridwidth = GridBagConstraints.RELATIVE;
gbl.setConstraints(comboBox,gbc);
pane.add(comboBox);JLabel label = new JLabel("create small button");
//gbc.gridy = 1;
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridwidth = GridBagConstraints.REMAINDER; //我想折行
gbl.setConstraints(label, gbc);
pane.add(label);JList dirlist = new JList();
gbc.weightx = 0.0;
//c.gridx = 0;
gbc.gridy = 1;
//c.gridwidth = GridBagConstraints.RELATIVE;
gbl.setConstraints(dirlist, gbc);
pane.add(dirlist); //这个list总是没有显示出来JList filelist = new JList();
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(filelist, gbc);
pane.add(filelist); return pane;
}
注释掉的代码没有用(或多余)因为我没有用JBuilder什么的,(我用的是JCreator),所以布局管理好烦哪
不过,解决了问题,这也极好用:-)private Component createComponent(){
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
pane.setLayout(gbl);
//set to change component size both in heritical and vertical
gbc.fill = GridBagConstraints.BOTH;
JComboBox comboBox = new JComboBox();
gbc.weightx = 1.0;//设置这一行的component在行有剩余空间是得到分配
gbc.weighty = 0.0;//设置这一行在垂直方向有剩余的时候也不得到分配
gbc.gridx = 0;
gbc.gridy = 0;//设置将要加入的元素的位置在左上角
//c.gridwidth = GridBagConstraints.RELATIVE;
gbl.setConstraints(comboBox,gbc);
pane.add(comboBox);
JLabel label = new JLabel("create small button");
//gbc.gridy = 1;
gbc.gridx = GridBagConstraints.RELATIVE;//紧跟刚才加入的元素
gbc.gridwidth = GridBagConstraints.REMAINDER;//下一个开始换行了
gbl.setConstraints(label, gbc);
pane.add(label);
JList dirlist = new JList(new String[]{"get","set","load"});
gbc.weighty = 1.0;//如果垂直方向显示区域有剩余,则本行的元素得到分配
//gbc.weightx = ;
//c.gridx = 0;
gbc.gridy = 1;//这是第二行,按照API在上面折行了,这行应是多余,实际不
//行,思考中
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbl.setConstraints(dirlist, gbc);
pane.add(dirlist);
JList filelist = new JList(new String[]{"top","left","bottom","ritht"});
gbc.gridy = GridBagConstraints.RELATIVE;//紧跟上一个元素
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(filelist, gbc);
pane.add(filelist);
//大功告成,呵呵,让大家烦劳啦
return pane;
}问题解决了就此结贴