熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

GridBagLayout 例

2022-06-13   來源: Java核心技術 

  import javaawtDimension;import javaawtGridBagConstraints;import javaawtGridBagLayout;import javaawtGridLayout;import javaawtInsets;import javaxswing*;public class Layout {
   
    /**
   
    * @param args
   
    */
   
    public static void main(String[] args) {
   
    new LayoutFrame()setVisible(true)
   
    }}class LayoutFrame extends JFrame {
   
    LayoutFrame(){
   
    thissetTitle(Layout Test
   
    thissetSize(
   
    thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
   
    JPanel jp = new JPanel()
   
    jpsetLayout(new GridBagLayout())
   
    jpadd(new JScrollPane(new JTextArea())new Constraint())
   
    JPanel p = new JPanel()
   
    psetLayout(new GridLayout())
   
    psetBorder(BorderFactorycreateEtchedBorder())
   
    padd(new JLabel(標  簽))
   
    padd(new JTextField())
   
    jpadd(pnew Constraint())
   
    jpadd(new MPanel()new Constraint())
   
    jpadd(new MPanel()new Constraint())
   
    JPanel p = new JPanel()
   
    psetLayout(new GridLayout())
   
    psetBorder(BorderFactorycreateEtchedBorder())
   
    padd(new JButton(按鈕))
   
    padd(new JButton(按鈕))
   
    jpadd(pnew Constraint())
   
    jpadd(new MPanel()new Constraint())
   
    jpadd(new MPanel()new Constraint())
   
    jpadd(new JScrollPane(new JTextArea())new Constraint())
   
    MPanel mp = new MPanel()
   
    /*mpadd(new JButton(hellp))
   
    mpadd(new JButton(hellp))
   
    mpadd(new JButton(hellp))
   
    mpadd(new JButton(hellp))*/
   
    jpadd(mpnew Constraint())
   
    jpadd(new MPanel()new Constraint())
   
    jpadd(new MPanel()new Constraint())
   
    add(jp)
   
    }}class MPanel extends JPanel {
   
    MPanel(){
   
    /*Dimension d = new Dimension(
   
    //thissetPreferredSize(new Dimension())
   
    thissetMaximumSize(d)
   
    thissetMinimumSize(d)*/
   
    add(new JLabel(面板))
   
    thissetBorder(BorderFactorycreateEtchedBorder())
   
    }}class Constraint extends GridBagConstraints {
   
    Constraint(int gridxint gridy) {
   
    thisgridx = gridx;  //gridxgridy 指表格坐標指此物件的開始位置在(xy)處也即在 x列y行處 xy 都從開始
   
    thisgridy = gridy;
   
    thissetFill(ConstraintBOTH) //默認物作是居中的設成both後如果物件比所占area小就會在橫豎兩個方向拉伸
   
    //thissetWeight( //空白區域怎麼分配
   
    thissetInsets(//邊界
   
    }
   
    Constraint(int gridxint gridyint gridheightint gridwidth) {
   
    this(gridxgridy)
   
    //高和寬指物件占用的空間 四個參數可理解 了 xydydx
   
    thisgridheight = gridheight;
   
    thisgridwidth = gridwidth;
   
    }
   
    public Constraint setFill(int fill) {
   
    thisfill = fill;
   
    return this;
   
    }
   
    public Constraint setAnchor(int anchor) {
   
    thisanchor = anchor; //是指物件放在所在區域的什麼位置默認是center CENTER NORTH NORTHEAST EAST SOUTHEAST SOUTH SOUTHWEST WEST
   
    //and NORTHWEST The relative values are: PAGE_START PAGE_END LINE_START LINE_END FIRST_LINE_START FIRST_LINE_END LAST_LINE_START and LAST_LINE_END The default value is CENTER
   
    return this;
   
    }
   
    public Constraint setWeight(double weightxdouble weighty) {
   
    thisweightx = weightx;
   
    thisweighty = weighty;
   
    return this;
   
    }
   
    public Constraint setInsets(int distance){
   
    thisinsets = new Insets(distancedistancedistancedistance)
   
    return this;
   
    }}


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26850.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.