当前位置: 技术问答>java相关
谁能给讲解一下GridBagLayout的用法(API没看懂)
来源: 互联网 发布时间:2015-10-30
本文导语: 我最近想用GridBagLayout 但是看不明白API,有那位大侠给讲一下:(从书上扒下来的也可以) 有详细解释的程序更好 | 从书上扒下来的 The GridBag Layout Manager GridBag is by far the most powerful layout ma...
我最近想用GridBagLayout
但是看不明白API,有那位大侠给讲一下:(从书上扒下来的也可以)
有详细解释的程序更好
但是看不明白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. }
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. }
|
你用JBuilder直接用这个布局管理器就可以了,就是说先把容器设为null或xy布局,然后再往上放控件,调整好之后再把容器德布局管理器改为GridBagLayout,说起来很麻烦,如果自己写就更麻烦了。其实就是GridLayout的进一步细化,就是先分成大的网格(相当于GridLayout),然后再在每个网格中细分,格的宽度,坐标,在格中是居中还是靠那边,和格线的距离,主要是这些。
|
给你个例子
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();
}
}
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();
}
}