gridbaglayout

来源:互联网 发布:板金下料软件 编辑:程序博客网 时间:2024/04/26 07:46

how to use gridbaglayout
 1,the way the program specifies the size and position characteristics of its components is by spedifying constraints  for each component.to specify constraints,you set instance variables in a GridBagConstraints object and tell the GridBagLayout(with the setConstraints method) to associate the constraints with the component.
 2,Specifying Constraints
   typical code:
  {JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//For each component to be added to this container:
//...Create the component...
//...Set instance variables in the GridBagConstraints instance...
pane.add(theComponent, c)}

{  "c.ipady = 40;      //make this component tall
        c.weightx = 0.0;
        c.gridwidth = 3;" what is ipady,weightx and gridwidth? and"c.anchor = GridBagConstraints.PAGE_END; //bottom of space
        c.insets = new Insets(10,0,0,0);  //top padding"????}


{ /*
   * GridBagLayoutDemo.java requires no other files.
   */

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class GridBagLayoutDemo {
    final static boolean shouldFill = true;// when the value of the 'shouldFill' has been changed to false,the buttons that we will define will be filled in the middle of the pane.
    final static boolean shouldWeightX = true;//when the value of the 'shouldWeightX' has been changed to false,the buttons that will define will be filled without the 'following' relationship with the pane when the pane is en larged.
    final static boolean RIGHT_TO_LEFT = false;// what are the three members for????when the value of  //RIGHT_TO_LEFT has been changed to true,the buttons that we will define will be filled in reverse.

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        JButton button;
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        if (shouldFill) {
            //natural height, maximum width
            c.fill = GridBagConstraints.HORIZONTAL;
        }

        button = new JButton("Button 1");
        if (shouldWeightX) {
            c.weightx = 0.5;
        }
        c.gridx = 0;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Button 2");
        c.gridx = 1;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Button 3");
        c.gridx = 2;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Long-Named Button 4");
        c.ipady = 40;      //make this component tall
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(button, c);

        button = new JButton("5");
        c.ipady = 0;       //reset to default
        c.weighty = 1.0;   //request any extra vertical space
        c.anchor = GridBagConstraints.PAGE_END; //bottom of space
        c.insets = new Insets(10,0,0,0);  //top padding
        c.gridx = 1;       //aligned with button 2
        c.gridwidth = 2;   //2 columns wide
        c.gridy = 2;       //third row
        pane.add(button, c);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("GridBagLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
4 "As you might have guessed from the above example, you can reuse the same GridBagConstraints instance for multiple components,even if the components have different constraints."
  ?? i know the "different constraints",for the
      ' c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 1; '
 has been different.
5,"The GridBagLayout extracts the constraint values and doesn't use the GridBagConstraints again."
      ?? where does the 'extraction' occur?
6,"ipadx, ipady
Specifies the internal padding: how much to add to the minimum size of the component. The default value is zero. The width of the component will be at least its minimum width plus ipadx*2 pixels, since the padding applies to both sides of the component. Similarly, the height of the component will be at least its minimum height plus ipady*2 pixels."
   ??what does that --ipadx and ipady--means???
   ??internal padding?minimun size of the component?

*******************************************************************************************************

(continued from yesterday)
7,the most flexible --and comlex.
8,in a grid of rows and columns(是成列成行的意思么?)
9,Not all rows necessarily have the same hidth(那么这个row的高度是不是必须是单个grid的倍数呢?)
10,Essentially,GridBagLayout places components in rectangles(cells) in a grid,and then uses the components' preferred sizes to determine how big the cells should be.(在这,rectangles与cells的大小关系?在determine时,这个cells是不是可能会覆盖不少的grids呢??)
11,the grid has three rows and three columns.(从这看去,grid这个词是说整体的网格,而不是单个的一个单元格)
12,WEIGHTX AND WEIGHTY
 If you enlarge the window as shown in the following figure, you'll notice that the bottom row, which contains Button 5, gets all the new vertical space. The new horizontal space is split evenly among all the columns. This resizing behavior is based on weights the program assigns to individual components in the GridBagLayout. You'll also notice that each component takes up all the available horizontal space — but not (as you can see with button 5) all the available vertical space. This behavior is also specified by the program.  
 “distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior”这句话很重要,可不知道怎么来描述现在理解后的心情了 :)
在此,关键词是weight,可以理解成“分配的份额”,这样别的问题都可以应刃而解。
 注意:weightx的值都相同(只是说,最终的效果相同,而里面的指定值并不相同:对button1,2,3来说,weightx都是0.5,而对button4是0.0,button5是1.0,这个1.0好像好理解,那么那个0.0呢?是不是说,像它这样要独占一行的就不需再另行指定了呢),而weighty的值并没有指定,默认值为:0.0(unless you specify at least one nonzero value for weightx or weighty,all the components clump together in the center of their container.this is because when the weight is 0.0(the default),the GridBagLayout puts any extra space between its grid if cells and the edges of the container),对its grid of cells的理解:是grid中的cells么?
 
 “For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in.”对这个column的理解?单个column里会有多个component么?
“权值的绝对值总和并不重要,重要的是相对大小  
  比如最小尺寸需要400像素的宽度才能显示组件。  
  但是容器宽度为600像素。这时GridBagLayout就要确定多余的200像素如何分配了  
  假如有三列,权值分别为20,40,50;  
  则第一列分配(20/(20+40+50))*200;  
  第二列分配(40/(20+40+50))*200  
  第三列分配(50/(20+40+50))*200  
   
  每一列的权值是根据这一列的最大权值确定  
  如这一列有三行,权值分别为20,30,40;  
  则这一列的权值为40”找了个方面的一个解释,可这里还是有个问题:上面的那个示例程序中,也是三行的,可这种交叉行列怎么来实现这种权值呢???
 
问题不少,以后再在应用中逐渐体会再理解吧,现在有应用完全可以用那个示例程序来作参照,开始时可以比猫画虎嘛!
(to be continued...)

原创粉丝点击