zoukankan      html  css  js  c++  java
  • GridBagLayout 例

    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
    
    import javax.swing.*;
    
    
    public class Layout2 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            new Layout2Frame().setVisible(true);
    
        }
    
    }
    class Layout2Frame extends JFrame {
        Layout2Frame(){
            this.setTitle("Layout2 Test");
            this.setSize(800,800);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel jp = new JPanel();
            jp.setLayout(new GridBagLayout());
            
            jp.add(new JScrollPane(new JTextArea(1,5)),new Constraint(0,0,1,3));
            
            JPanel p1 = new JPanel();
            p1.setLayout(new GridLayout(2,1));
            p1.setBorder(BorderFactory.createEtchedBorder());
            p1.add(new JLabel("标  签"));
            p1.add(new JTextField(5));
            
            jp.add(p1,new Constraint(3,0));
            jp.add(new MPanel2(),new Constraint(4,0,1,2));
            jp.add(new MPanel2(),new Constraint(6,0,1,1));
            
            
            JPanel p2 = new JPanel();
            p2.setLayout(new GridLayout(2,1));
            p2.setBorder(BorderFactory.createEtchedBorder());
            p2.add(new JButton("按钮"));
            p2.add(new JButton("按钮"));
            
            
            jp.add(p2,new Constraint(0,1));
            jp.add(new MPanel2(),new Constraint(1,1));
            jp.add(new MPanel2(),new Constraint(2,1));
            jp.add(new JScrollPane(new JTextArea(1,5)),new Constraint(3,1));
            MPanel2 mp = new MPanel2();
            /*mp.add(new JButton("hellp"));
            mp.add(new JButton("hellp"));
            mp.add(new JButton("hellp"));
            mp.add(new JButton("hellp"));*/
            
            jp.add(mp,new Constraint(4,1));
            jp.add(new MPanel2(),new Constraint(5,1));
            jp.add(new MPanel2(),new Constraint(6,1));
            
            
            
            add(jp);
            
        }
    }
    class MPanel2 extends JPanel {
        MPanel2(){
            /*Dimension d = new Dimension(100,100);
            //this.setPreferredSize(new Dimension(60,60));
            this.setMaximumSize(d);
            this.setMinimumSize(d);*/
            add(new JLabel("面板"));
            
            this.setBorder(BorderFactory.createEtchedBorder());
        }
    }
    class Constraint extends GridBagConstraints {
        Constraint(int gridx,int gridy) {
            
            this.gridx = gridx;  //gridx,gridy 指表格坐标.指此物件的开始位置在(x,y)处,也即在 x列,y行处 ,x,y 都从0开始.
            this.gridy = gridy;
            this.setFill(Constraint.BOTH); //默认物作是居中的.设成both后如果物件比所占area小就会在横竖两个方向拉伸
            //this.setWeight(100, 100);//空白区域怎么分配.
            this.setInsets(1);//边界
        }
        Constraint(int gridx,int gridy,int gridheight,int gridwidth) {
            this(gridx,gridy);    //高和宽指物件占用的空间 , 四个参数可理解 了 ,x,y,dy,dx
            this.gridheight = gridheight;
            this.gridwidth = gridwidth;
        }
        public Constraint setFill(int fill) {
            this.fill = fill;
            return this;
        }
        public Constraint setAnchor(int anchor) {
            this.anchor = 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 weightx,double weighty) {
            this.weightx = weightx;
            this.weighty = weighty;
            return this;
        }
        public Constraint setInsets(int distance){
            this.insets = new Insets(distance,distance,distance,distance);
            return this;
        }
    }
  • 相关阅读:
    声明式事务
    AOP面向切面编程笔记
    IOC容器和Bean的配置实例
    IOC容器和Bean的配置
    nlogn的最长不下降子序列【tyvj1254挑选士兵】
    POJ1703 Find them, Catch them
    [code]poj3349 Snowflake Snow Snowflakes
    hdu 4607 Park Visit 树的直径
    hdu 1520 Anniversary party
    hdu 4863 Centroid of a Tree 树dp
  • 原文地址:https://www.cnblogs.com/qqjue/p/2620840.html
Copyright © 2011-2022 走看看