javaUI之三种布局管理
程序员文章站
2022-06-08 16:32:07
...
边界布局:唯一一个不用用this.setLayout();的,默认网格布局
package com.SwingIn51;
import java.awt.*;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame {
JButton an1,an2,an3,an4,an5;
public BorderLayoutDemo() {
an1=new JButton("东方");
an2=new JButton("西方");
an3=new JButton("南方");
an4=new JButton("北方");
an5=new JButton("中部");
this.add(an1,BorderLayout.EAST);//设置添加的位置
this.add(an2,BorderLayout.WEST);
this.add(an3,BorderLayout.NORTH);
this.add(an4,BorderLayout.SOUTH);
this.add(an5,BorderLayout.CENTER);//中间可以不写,直接this.add(an5);
this.setTitle("边界布局BorderLayout");
this.setSize(380, 320);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
public static void main(String[] args) {
BorderLayoutDemo b=new BorderLayoutDemo();
}
}
流式布局:package com.SwingIn51;
import java.awt.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame{
JButton[] an= {null,null,null,null,null,null};
public static void main(String[] args) {
FlowLayoutDemo z=new FlowLayoutDemo();
}
public FlowLayoutDemo() {
an[0]=new JButton("话梅");
an[1]=new JButton("果脯");
an[2]=new JButton("薯片");
an[3]=new JButton("饼干");
an[4]=new JButton("巧克力");
an[5]=new JButton("开心果");
this.setLayout(new FlowLayout());
this.add(an[0]);
this.add(an[1]);
this.add(an[2]);
this.add(an[3]);
this.add(an[4]);
this.add(an[5]);
this.setTitle("FlowLaout");
this.setSize(380,320);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
}
网格布局:
package com.SwingIn51;
import java.awt.*;
import javax.swing.*;
public class GirdLayout extends JFrame {
JButton an[]= {null,null,null,null};
int s=4;
public GirdLayout() {
an[0]=new JButton("话梅");
an[1]=new JButton("福");
an[2]=new JButton("敲了李");
an[3]=new JButton("饼干");
//this.setLayout(new GridLayout(3,3));
this.setLayout(new GridLayout(3,3,12,13));
for(int i=0;i<s;i++) {
this.add(an[i]);
}
this.setTitle("网格布局");
this.setSize(380,320);
this.setLocation(200, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
GirdLayout a=new GirdLayout();
}
}
三种布局的步骤:
1。继承JFame
2.定义组件
3,创建组件
4.添加组件
5一些属性的添加
6以上2到5均在构造方法中完成
上一篇: GUI的三种布局方式
下一篇: AutoLayout约束的使用
推荐阅读