GridBagLayoutDemo

import java.awt.*;

import javax.swing.*;

public class GridBagDemo extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = -1511810356038746853L;
    public static void main(String args[]) {
        GridBagDemo demo = new GridBagDemo();
    }

    public GridBagDemo() {
        init();
        this.setSize(600,600);
        this.setVisible(true);
    }
    public void init() {
        JLabel name = new JLabel("dasdasd");
        name.setSize(new Dimension(100,100));
        JScrollPane right = new JScrollPane();
        //right.setSize(new Dimension(500,300));
        right.setBackground(Color.RED);
        JScrollPane left = new JScrollPane();
        left.setBackground(Color.BLUE);
        left.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        

        JLabel jLabel = null;
        JPanel jPanel = new JPanel(new GridLayout(0,1,0,0));
        for(int i = 0 ; i < 100; i++) {
            jLabel = new JLabel("asdasd");
            jPanel.add(jLabel);
        }
        
        this.getContentPane().add(jPanel);
        left.setViewportView(jPanel);
        
        this.getContentPane().add(name);
        this.getContentPane().add(left);
        this.getContentPane().add(right);
        GridBagLayout layout = new GridBagLayout();
        this.setLayout(layout);
        GridBagConstraints s= new GridBagConstraints();
        
        
        s.fill = GridBagConstraints.BOTH;
        s.insets = new Insets(5, 5, 5, 5);
        s.gridx = 1;
        s.gridy = 1;
        s.gridwidth = 1;
        s.gridheight = 1;
        s.weightx = 0;
        s.weighty = 0;
        layout.setConstraints(name, s);
        
        s.fill = GridBagConstraints.BOTH;
        s.insets = new Insets(5, 5, 5, 5);
        s.gridx = 1;
        s.gridy = 2;
        s.gridwidth = 1;
        s.gridheight = 8;
        s.weightx = 0;
        s.weighty = 0;
        layout.setConstraints(left, s);
        
        
        s.fill = GridBagConstraints.BOTH;
        s.gridwidth = 0;
        s.gridheight = 9;
        s.gridx = 2;
        s.gridy = 1;
        s.weightx = 1;
        s.weighty = 1;
        layout.setConstraints(right, s);
    }
}


左上角第一个Label为基本cell所有其他grid里的都是以其作为宽高倍数判断的,而且设定大小无效

gridwidth <span style="font-family: Arial, Helvetica, sans-serif;">当前元素面积为基本cell的倍数</span>
gridx 元素位置
weightx <span style="font-family: Arial, Helvetica, sans-serif;">窗体放大系数</span>
还有有个问题困扰我,如何在JPanel上添加 带滚动条的JPanel,而不是在JFrame上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值