布局管理器五种。
前三种比较简单的:流式、边界、网格布局管理器。
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class SU {
public static void main(String[] args){
final Frame f=new Frame("我的窗体!");
// f.setLayout(new FlowLayout(FlowLayout.LEFT,20,30));
f.setLayout(new BorderLayout());//设置一个布局管理器
f.setSize(400, 400);
f.setLocation(300, 200);
f.setVisible(true);
Button bnt1=new Button("东部");
Button bnt2=new Button("西部");
Button bnt3=new Button("南部");
Button bnt4=new Button("北部");
Button bnt5=new Button("中部");
//创建好的按钮添加到窗体中
f.add(bnt1, BorderLayout.EAST);
f.add(bnt2, BorderLayout.WEST);
f.add(bnt3, BorderLayout.SOUTH);
f.add(bnt4, BorderLayout.NORTH);
f.add(bnt5, BorderLayout.CENTER);
final Frame f1=new Frame("我的窗体2!");
f1.setLayout(new GridLayout(4,3));
f1.setSize(400, 400);
f1.setLocation(300, 200);
for(int i=1;i<=9;i++){
Button btn1=new Button("button"+i);
f1.add(btn1);
}
f1.setVisible(true);
/*
Button btn1=new Button("第一个按钮");
f.add(btn1);
btn1.addActionListener(new ActionListener() {
private int num=1;//用来记录按钮个数
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
f.add(new Button("第"+ ++num +"个按钮"));
f.setVisible(true);
}
});
*/
f.setVisible(true);
//实现窗口可关闭。
MyWindowListener mw=new MyWindowListener();
f.addWindowListener(mw);
f1.addWindowListener(mw);
//
}
}
//让窗口能关闭
//创建MyWindowListener类实现WindowListener接口
class MyWindowListener implements WindowListener {
// 监听器监听事件对象作出处理
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
// 释放窗口
window.dispose();
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
网格包布局管理器
书上的写法:定义了Layout类继承Frame类
import java.awt.*;
class Layout extends Frame{
public Layout(String title){
//网格包布局管理器
GridBagLayout layout=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
this.setLayout(layout);
c.fill=GridBagConstraints.BOTH;//使组件足够大以填充整个显示区域
c.weightx=1;//设置横向权重为1
c.weighty=1;//设置纵向权重为1
this.addComponent("btn1",layout,c);
this.addComponent("btn2",layout,c);
this.addComponent("btn3",layout,c);
c.gridwidth=GridBagConstraints.REMAINDER;//换行的作用。
this.addComponent("btn4",layout,c);
c.weightx=0;
c.weighty=1;
addComponent("btn5",layout,c);
c.gridwidth=1;//设置组件跨一个网格(默认值)
this.addComponent("btn6", layout,c);
c.gridwidth=GridBagConstraints.REMAINDER;
this.addComponent("btn7",layout,c);
c.gridheight=2;//纵向跨两个
c.gridwidth=2;//横向跨一个
c.weightx=2;//横向权重为2
c.weighty=2;//纵向权重为2
this.addComponent("btn8",layout,c);
c.gridwidth=GridBagConstraints.REMAINDER;
c.gridheight=1;
this.addComponent("btn9",layout,c);
this.addComponent("btn10",layout,c);
this.pack();//pack方法使用所有组件的最佳大小来计算框架的高度和宽度
this.setVisible(true);
}
private void addComponent(String name,GridBagLayout layout,GridBagConstraints c){
Button bt=new Button(name);
layout.setConstraints(bt,c);
this.add(bt);
}
}
public class SU {
public static void main(String[] args){
new Layout("111");
}
}
改写。声明Frame类,实现了窗口关闭。
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import java.util.Random;
public class SU {
public static void main(String[] args){
Frame f=new Frame("frame");
GridBagLayout layout=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
f.setLayout(layout);
c.fill=GridBagConstraints.BOTH;//使组件足够大以填充整个显示区域
c.weightx=1;//设置横向权重为1
c.weighty=1;//设置纵向权重为1
addComponent("btn1",layout,c,f);
addComponent("btn2",layout,c,f);
addComponent("btn3",layout,c,f);
c.gridwidth=GridBagConstraints.REMAINDER;//换行的作用。
addComponent("btn4",layout,c,f);
c.weightx=0;
c.weighty=1;
addComponent("btn5",layout,c,f);
c.gridwidth=1;//设置组件跨一个网格(默认值)
addComponent("btn6", layout,c,f);
c.gridwidth=GridBagConstraints.REMAINDER;
addComponent("btn7",layout,c,f);
c.gridheight=2;//纵向跨两个
c.gridwidth=2;//横向跨一个
c.weightx=2;//横向权重为2
c.weighty=2;//纵向权重为2
addComponent("btn8",layout,c,f);
c.gridwidth=GridBagConstraints.REMAINDER;
c.gridheight=1;
addComponent("btn9",layout,c,f);
addComponent("btn10",layout,c,f);
f.pack();//pack方法使用所有组件的最佳大小来计算框架的高度和宽度
f.setVisible(true);
MyWindowListener mw=new MyWindowListener();
f.addWindowListener(mw);
}
public static void addComponent(String name,GridBagLayout layout,GridBagConstraints c,Frame f){
Button bt=new Button(name);
layout.setConstraints(bt,c);
f.add(bt);
}
}
class MyWindowListener implements WindowListener {
// 监听器监听事件对象作出处理
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
// 释放窗口
window.dispose();
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}
卡片布局管理器
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//定义Cardlayout继承Frame类,实现ActionListener接口
class Cardlayout extends Frame implements ActionListener {
Button nextbutton,preButton;
Panel cardPanel = new Panel(); // 定义Panel面板放置卡片
Panel controlpaPanel = new Panel(); // 定义Panel面板放置按钮
CardLayout cardLayout = new CardLayout();// 定义卡片布局对象
public Cardlayout() { // 定义构造方法,设置卡片布局管理器的属性
setSize(300, 200);
setVisible(true);
// 为窗口添加关闭事件监听器
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Cardlayout.this.dispose();
}
});
cardPanel.setLayout(cardLayout); // 设置cardPanel面板对象为卡片布局
// 在cardPanel面板对象中添加3个文本标签
cardPanel.add(new Label("第一个界面", Label.CENTER));
cardPanel.add(new Label("第二个界面", Label.CENTER));
cardPanel.add(new Label("第三个界面", Label.CENTER));
// 创建两个按钮对象
nextbutton = new Button("下一张卡片");
preButton = new Button("上一张卡片");
// 为按钮对象注册监听器
nextbutton.addActionListener(this);
preButton.addActionListener(this);
// 将按钮添加到controlpaPanel中
controlpaPanel.add(preButton);
controlpaPanel.add(nextbutton);
// 将cardPanel面板放置在窗口边界布局的中间,窗口默认为边界布局
this.add(cardPanel, BorderLayout.CENTER);
// 将controlpaPanel面板放置在窗口边界布局的南区,
this.add(controlpaPanel, BorderLayout.SOUTH);
}
// 下面的代码实现了按钮的监听触发,并对触发事件做出相应的处理
public void actionPerformed(ActionEvent e) {
// 如果用户单击nextbutton,执行的语句
if (e.getSource() == nextbutton) {
// 切换cardPanel面板中当前组件之后的一个组件,若当前组件为最后一个组件,则显示第一个组件。
cardLayout.next(cardPanel);
}
if (e.getSource() == preButton) {
// 切换cardPanel面板中当前组件之前的一个组件,若当前组件为第一个组件,则显示最后一个组件。
cardLayout.previous(cardPanel);
}
}
}
public class SU {
public static void main(String[] args) {
Cardlayout cardlayout = new Cardlayout();
}
}
取消布局管理
取消布局管理后就必须设置坐标和大小来为组件在容器中定位
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SU {
public static void main(String[] args) {
Frame f=new Frame("hello");
f.setLayout(null);//取消frame的布局管理
f.setSize(300,150);
Button btn1=new Button("press");
Button btn2=new Button("pop");
//前面两个是位置后面两个是坐标
btn1.setBounds(40,60,100,30);
btn2.setBounds(140,90,100,30);
f.add(btn1);
f.add(btn2);
f.setVisible(true);
MyWindowListener mw=new MyWindowListener();
f.addWindowListener(mw);
}
}
class MyWindowListener implements WindowListener {
// 监听器监听事件对象作出处理
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
// 释放窗口
window.dispose();
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}