2021-05-17

《化羽Java GUI学习笔记》          我自己的学习笔记以后方便复习,也希望可以给找不到方法学习的人一点点帮助。如果有错误请大家告诉一下相互学习。

 除了创建Box实例的静态方法之外,Box类中还提供了一些创建不可见(invisible)组件的方法
 例如:
     public static Component createHorizontalGlue()
     public static Component createVerticalGlue()
     public static Component createHorizontalStrut(int width)
     public static Component createVerticalStrut(int height)
     public static Component createRigidArea(Dimension d)
 
 运行结果:
    Box 1 是没有添加不可见组件时的形式
    Box 2,3和4是分别添加了不可见组件Glue,Strut和Rigid之后的形式,Glue将填满所有剩余
       水平(或垂直)空间,Strut和Rigid则具有指定的宽度(或高度)

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

public class GlueAndStrut {
	private JFrame frame;
	private Box b1,b2,b3,b4;

	public static void main(String[] args) {
		GlueAndStrut that = new GlueAndStrut();
        that.go();
	}
	
	void go() {
		frame = new JFrame("Glue and Strut example");
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new GridLayout(4,1));   //4行1列
		
		b1 = Box.createHorizontalBox();
		b1.add(new JLabel("Box 1:"));
		b1.add(new JButton("Yes"));
		b1.add(new JButton("No"));
		b1.add(new JButton("Cancel"));
		
		b2 = Box.createHorizontalBox();
		b2.add(new JLabel("Box 2:"));
		b2.add(new JButton("Yes"));
		b2.add(new JButton("No"));
		b2.add(Box.createHorizontalGlue());
		b2.add(new JButton("Cancel"));
		
		b3 = Box.createHorizontalBox();
		b3.add(new JLabel("Box 3:"));
		b3.add(new JButton("Yes"));
		b3.add(new JButton("No"));
		b3.add(Box.createHorizontalStrut(20));
		b3.add(new JButton("Cancel"));
		
		b4 = Box.createHorizontalBox();
		b4.add(new JLabel("Box 3:"));
		b4.add(new JButton("Yes"));
		b4.add(new JButton("No"));
		b4.add(Box.createRigidArea(new Dimension(50,90)));
		b4.add(new JButton("Cancel"));
		
		contentPane.add(b1);
		contentPane.add(b2);
		contentPane.add(b3);
		contentPane.add(b4);
		
		frame.setSize(300,200);
		frame.setVisible(true);
		
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值