真爱无限的知识驿站

学习积累技术经验,提升自身能力

JAVA学习-Java高级-GUI之布局管理器

Java高级-GUI之布局管理器

容器内可以存放各种组件,而组件的位置和大小都是由容器内的布局管理器来决定的。在AWT中为我们提供了以下5种布局管理器:

FlowLayout 流式布局管理器

BorderLayout 边界布局管理器

GridLayout 网格布局管理器

CardLayout 卡片布局管理器

GridBagLayout 网格包布局管理器

容器中组件的布局通常由布局管理器控制。每个Container(比如:Panel、Frame)都有一个与它相关的缺省布局管理器,Panel容器默认的是FlowLayout,Frame容器默认的是BorderLayout,我们可以通过setLayout()来改变布局管理器。

在设置空布局管理器之后,必须对所有的组件调用setLocation()、setSize()或setBounds(),将它们定位在容器中。


1、流式布局管理器

demo:

package pkg1.awt;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
public class FlowLayoutDemo {
public static void main(String[] args) {
MyFrame frame=new MyFrame("流式布局");
frame.init();
}
}
class MyFrame extends Frame{
public MyFrame(String title){
super(title);
}
public void init(){
FlowLayout layout=new FlowLayout(FlowLayout.LEFT,10,10);
this.setLayout(layout);
this.setBackground(Color.GRAY);
this.add(new Button("btn1"));
this.add(new Button("btn2"));
this.add(new Button("btn3"));
this.add(new Button("btn4"));
this.setSize(400, 300);
this.setLocation(800, 500);
this.setVisible(true);
}
}

效果:


2、边界布局管理器

demo:

package pkg1.awt;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
public class BorderLayoutDemo {
public static void main(String[] args) {
MyFrame2 frame=new MyFrame2("边界布局");
frame.init();
}
}
class MyFrame2 extends Frame{
public MyFrame2(String title){
super(title);
}
public void init(){
//Frame默认布局管理器是BorderLayout,所以不用再设置
this.setBackground(Color.GRAY);
this.add(new Button("btn1"),BorderLayout.NORTH);
this.add(new Button("btn2"),BorderLayout.SOUTH);
this.add(new Button("btn3"),BorderLayout.WEST);
this.add(new Button("btn4"),BorderLayout.EAST);
this.add(new Button("btn5"),BorderLayout.CENTER);
this.setSize(400, 300);
this.setLocation(800, 500);
this.setVisible(true);
}
}

效果:


3、网格布局管理器

demo:

package pkg1.awt;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
public class GridLayoutDemo {
public static void main(String[] args) {
MyFrame3 frame=new MyFrame3("网格布局");
frame.init();
}
}
class MyFrame3 extends Frame{
public MyFrame3(String title){
super(title);
}
public void init(){
GridLayout layout = new GridLayout(2, 3);
this.setLayout(layout);
this.setBackground(Color.GRAY);
this.add(new Button("btn1"));
this.add(new Button("btn2"));
this.add(new Button("btn3"));
this.add(new Button("btn4"));
this.add(new Button("btn5"));
this.setSize(400, 300);
this.setLocation(800, 500);
this.setVisible(true);
}
}

效果:


4、卡片布局管理器

demo:

package pkg1.awt;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
public class CardLayoutDemo {
public static void main(String[] args) {
MyFrame4 frame = new MyFrame4("卡片布局");
frame.init();
}
}
class MyFrame4 extends Frame {
private Panel card_panel = null;// 卡片面板(放卡片布局管理器)
private Panel ctl_panel = null;// 控件面板(放流式布局管理器)
private CardLayout cardLayout = null;// 卡片布局管理器
private FlowLayout flowLayout = null;// 流式布局管理器
private Label lb1, lb2, lb3, lb4;
private TextField txt = null;
private Button btn_first,btn_pre,btn_next,btn_last;
public MyFrame4(String title) {
super(title);
}
public void init() {
card_panel = new Panel();
ctl_panel = new Panel();
cardLayout = new CardLayout();
flowLayout = new FlowLayout();
card_panel.setLayout(cardLayout);
ctl_panel.setLayout(flowLayout);
// Frame默认为BorderLayout布局,把两个Panel放在Frame中,一个上一个下
this.add(card_panel, BorderLayout.CENTER);
this.add(ctl_panel, BorderLayout.SOUTH);
// 卡片布局管理器放组件
// Label、TextField 创建控件
lb1 = new Label("Content1", Label.CENTER);
lb2 = new Label("Content2", Label.CENTER);
txt = new TextField("Content3");// 文本框
lb3 = new Label("Content4", Label.CENTER);
lb4 = new Label("Content5", Label.CENTER);
// 控件添加到容器,默认显示其中第一个卡片
card_panel.add(lb1);
card_panel.add(lb2);
card_panel.add(txt);
card_panel.add(lb3);
card_panel.add(lb4);
//创建按钮对象
btn_first=new Button("first");
btn_pre=new Button("pre");
btn_next=new Button("next");
btn_last=new Button("last");
ctl_panel.add(btn_first);
ctl_panel.add(btn_pre);
ctl_panel.add(btn_next);
ctl_panel.add(btn_last);
ctl_panel.setBackground(Color.GRAY);
//现在按钮的事件还没学到,以后可以做到:点击哪个显示哪个卡片内容
this.setSize(400, 300);
this.setLocation(800, 500);
this.setVisible(true);
}
}

效果:


更多的使用可以查看java api文档

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号