Java GUI之五大布局
来源:互联网 发布:软件源代码查看器 编辑:程序博客网 时间:2024/06/06 00:51
1.默认布局FlowLayout
public class SetFlowLayout { JFrame frame; JButton[] button; SetFlowLayout() { frame = new JFrame(); frame.setLayout(new FlowLayout()); button = new JButton[3]; for (int i = 0; i < 3; i++) { button[i] = new JButton("" + i); frame.add(button[i]); } frame.setBounds(350, 100, 500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SetFlowLayout(); }}2.BorderLayout
public class SetBorderLayout { JFrame frame; JButton[] button; public SetBorderLayout() { // TODO Auto-generated constructor stub frame = new JFrame(); frame.setTitle("BorderLayout"); frame.setLayout(new BorderLayout()); button = new JButton[5]; // 先分配数组空间范围 button[0] = new JButton("北"); frame.add(button[0], BorderLayout.NORTH); button[1] = new JButton("南"); frame.add(button[1], BorderLayout.SOUTH); button[2] = new JButton("东"); frame.add(button[2], BorderLayout.EAST); button[3] = new JButton("西"); frame.add(button[3], BorderLayout.WEST); button[4] = new JButton("中"); frame.add(button[4], BorderLayout.CENTER); frame.setBounds(350, 100, 500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SetBorderLayout(); }}3.BoxLayout
public class SetBoxLayout extends JFrame { private static final long serialVersionUID = 1L; public SetBoxLayout() { setLayout(new BoxLayout(getContentPane(), BoxLayout.LINE_AXIS)); getContentPane().setBackground(Color.green); add(new Button("123")); add(new Button("123")); add(new Button("123")); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 50, 700, 400); } public static void main(String[] args) { new SetBoxLayout(); }}4.卡片布局CardLayout
public class SetCardLayout { public static void main(String[] args) { JFrame f = new JFrame(); CardLayout card = new CardLayout(); f.setLayout(card); Container c = f.getContentPane(); JButton[] b = new JButton[6]; for (int i = 0; i < b.length; i++) { b[i] = new JButton("第" + (i + 1) + "页"); c.add(b[i], "page" + (i + 1)); } b[0].setBackground(Color.blue); b[1].setBackground(Color.yellow); b[2].setBackground(Color.red); b[3].setBackground(Color.green); b[4].setBackground(Color.orange); b[5].setBackground(Color.pink); f.setSize(300, 300); f.setVisible(true); card.show(c, "1"); while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } card.next(c); } /* * for (int i = 0; i < 6; i++) { try { while (true) { * Thread.sleep(1000); card.next(c); } } catch (InterruptedException e) * { e.printStackTrace(); } } */ }}5.网格布局GridLayout
public class SetGridLayout { JFrame frame; JButton[] button; public SetGridLayout() { // TODO Auto-generated constructor stub frame = new JFrame(); frame.setLayout(new GridLayout(3, 4, 1, 1)); /* * GridLayout( ); 第一个、二哥参数表示网格分成的行、列, 第三个、四个参数表示网格之间的横纵间距 */ button = new JButton[12]; for (int i = 0; i < 12; i++) { button[i] = new JButton("" + i); // " "+1将数字转换成字符串“1” frame.add(button[i]); } frame.setBounds(350, 100, 500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SetGridLayout(); }}
阅读全文
0 0
- Java GUI之五大布局
- 五大布局之 LinearLayout
- 五大布局之RelativeLayout
- 五大布局之TableLayout
- Android之五大布局
- Android之五大布局
- Android五大布局之相对布局
- Android五大布局之网格布局
- 【Java】Java中GUI之布局管理器
- JAVA图形界面(GUI)之布局管理器
- Java之GUI-------边界布局管理器
- 十七、java-GUI之边界布局(BorderLayout)
- 十八、java-GUI之网格布局(GridLayout)
- 十九、java-GUI之卡片布局(CardLayout)
- Android 五大布局之线性布局和相对布局
- Android基础教程之五大布局对象
- Android开发之--五大布局方式
- android学习之二 五大布局
- QT如何修改字符编码格式
- QT如何修改编程语言的字体
- SharpDevelop 如何切换设计视图和代码视图
- ROBODK仿真如何设置运动速度
- 设计模式——抽象工厂模式
- Java GUI之五大布局
- VB的程序如何破解
- 对于大指数幂的处理
- Sharpdevelop如何在项目中添加类文件
- PS如何绘制虚线圆
- Windows如何安装MSMQ消息队列
- JS中的“!!”
- VC++ 提示无法打开包括文件“iostream.h”怎么办
- 实验三 链栈(c++)