Swing004——箱式布局

Posted 江州益彤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swing004——箱式布局相关的知识,希望对你有一定的参考价值。

一、API简介



二、实例

2.1、默认布局

package com.box;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestBox 
	public static void main(String[] args) 
		// 1、创建一个顶层容器
		JFrame jFrame = new JFrame();
		// 设置标题
		jFrame.setTitle("箱式布局");
		// 设置关闭时推出虚拟机JVM
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 创建按钮
		JButton jButton1 = new JButton("按钮1");
		JButton jButton2 = new JButton("按钮2");
		JButton jButton3 = new JButton("按钮3");
		JButton jButton4 = new JButton("按钮4");
		JButton jButton5 = new JButton("按钮5");

		// 创建一个水平箱子容器
		Box hBox1 = Box.createHorizontalBox();
		hBox1.add(jButton1);
		hBox1.add(jButton2);
		hBox1.add(jButton3);

		// 创建第二个水平箱子容器
		Box hBox2 = Box.createHorizontalBox();
		hBox2.add(jButton4);
		// 添加一个水平胶状不可见的组件
		hBox2.add(Box.createHorizontalGlue());
		hBox2.add(jButton5);

		// 创建一个装上面两个盒子的竖直盒子
		Box vBox = Box.createVerticalBox();
		vBox.add(hBox1);
		vBox.add(hBox2);

		// 将大盒子添加到面板中
		jFrame.setContentPane(vBox);
		// 让组件中的空包去除,适应当前格局大小
		jFrame.pack();
		// 居中
		jFrame.setLocationRelativeTo(null);
		// 显示窗口
		jFrame.setVisible(true);
	

2.2、自定义宽度

package com.box;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestBox 
	public static void main(String[] args) 
		// 1、创建一个顶层容器
		JFrame jFrame = new JFrame();
		// 设置标题
		jFrame.setTitle("箱式布局");
		// 设置关闭时推出虚拟机JVM
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 创建按钮
		JButton jButton1 = new JButton("按钮1");
		JButton jButton2 = new JButton("按钮2");
		JButton jButton3 = new JButton("按钮3");
		JButton jButton4 = new JButton("按钮4");
		JButton jButton5 = new JButton("按钮5");

		// 创建一个水平箱子容器
		Box hBox1 = Box.createHorizontalBox();
		hBox1.add(jButton1);
		hBox1.add(jButton2);
		hBox1.add(jButton3);

		// 创建第二个水平箱子容器
		Box hBox2 = Box.createHorizontalBox();
		hBox2.add(jButton4);
		// 添加一个水平胶状不可见的组件
		hBox2.add(Box.createHorizontalStrut(100));// 自定义固定宽度

		hBox2.add(jButton5);

		// 创建一个装上面两个盒子的竖直盒子
		Box vBox = Box.createVerticalBox();
		vBox.add(hBox1);
		vBox.add(hBox2);

		// 将大盒子添加到面板中
		jFrame.setContentPane(vBox);
		// 让组件中的空包去除,适应当前格局大小
		jFrame.pack();
		// 居中
		jFrame.setLocationRelativeTo(null);
		// 显示窗口
		jFrame.setVisible(true);
	


2.3、自定义高度

package com.box;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestBox 
	public static void main(String[] args) 
		// 1、创建一个顶层容器
		JFrame jFrame = new JFrame();
		// 设置标题
		jFrame.setTitle("箱式布局");
		// 设置关闭时推出虚拟机JVM
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 创建按钮
		JButton jButton1 = new JButton("按钮1");
		JButton jButton2 = new JButton("按钮2");
		JButton jButton3 = new JButton("按钮3");
		JButton jButton4 = new JButton("按钮4");
		JButton jButton5 = new JButton("按钮5");

		// 创建一个水平箱子容器
		Box hBox1 = Box.createHorizontalBox();
		hBox1.add(jButton1);
		hBox1.add(jButton2);
		hBox1.add(jButton3);

		// 创建第二个水平箱子容器
		Box hBox2 = Box.createHorizontalBox();
		hBox2.add(jButton4);
		// 添加一个水平胶状不可见的组件
		hBox2.add(Box.createHorizontalGlue());

		hBox2.add(jButton5);

		// 创建一个装上面两个盒子的竖直盒子
		Box vBox = Box.createVerticalBox();
		vBox.add(hBox1);
		vBox.add(Box.createVerticalStrut(100));// 自定义固定高度
		vBox.add(hBox2);

		// 将大盒子添加到面板中
		jFrame.setContentPane(vBox);
		// 让组件中的空包去除,适应当前格局大小
		jFrame.pack();
		// 居中
		jFrame.setLocationRelativeTo(null);
		// 显示窗口
		jFrame.setVisible(true);
	

以上是关于Swing004——箱式布局的主要内容,如果未能解决你的问题,请参考以下文章