java awt setlayout_java 学习 —— AWT布局组件

自己学习java中AWT开发窗体程序中的几种窗口组件,做一下笔记

FlowLayout:

import java.awt.*;

public class Wintwo2 extends Frame{

//定义一个文本框。

TextArea a=new TextArea("请准确填写信息");

//定义三个按钮组件。

Button b1=new Button("提交");

Button b2=new Button("取消");

Button b3=new Button("重置");

Wintwo2(){

//设置窗口名称。

this.setTitle("调查信息卡");

//设置布局管理器为FlowLayout。

this.setLayout(new FlowLayout());

//将按钮组件放到窗口中

this.add(a);

this.add(b1);

this.add(b2);

this.add(b3);

//设置窗口的位置和大小

this.setBounds(100, 100, 450, 350);

//设置窗口可见性

this.setVisible(true);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Wintwo2();

}

}

31978266821879bcaca0985bf89d08c3.png

BorderLayout:

import java.awt.*;

import java.awt.peer.TextAreaPeer;

public class Winthree1 extends Frame{

//定义五个按钮组件

TextArea b1=new TextArea("中");

TextArea b2=new TextArea("南");

Button b3=new Button("提交");

Button b4=new Button("取消");

Button b5=new Button("重置");

Winthree1(){

//设置窗口名称

this.setTitle("利器BorderLayout");

//设置布局管理器BorderLayout

this.setLayout(new BorderLayout());

//将按钮组件放入窗口规定位置

this.add(b1, BorderLayout.CENTER);

this.add(b2, BorderLayout.NORTH);

this.add(b3, BorderLayout.SOUTH);

this.add(b4, BorderLayout.WEST);

this.add(b5, BorderLayout.EAST);

//设置窗口位置和大小

this.setBounds(300, 200, 450, 350);

//设置窗口可见性

this.setVisible(true);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Winthree1();

}

}

239485ac1c38f037cfbdf59fe2967cba.png

GridLayout:

import java.awt.*;

public class Winfour2 extends Frame{

TextArea b1=new TextArea("甲");

TextArea b2=new TextArea("乙");

Button b3=new Button("丙");

Button b4=new Button("丁");

Button b5=new Button("戊");

Winfour2() {

this.setTitle("Girdlayout布局");

this.setLayout(new GridLayout(2, 3));

this.add(b1);

this.add(b2);

this.add(b3);

this.add(b4);

this.add(b5);

this.setBounds(100, 100, 450, 450);

this.setVisible(true);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Winfour2();

}

}

44c9ce3f5f282c65c43d8a25452f608b.png

CardLayout:

import java.awt.*;

import java.awt.event.*;

public class Winfive1 extends Frame implements ActionListener{

//定义一个面板

Panel p=new Panel();

//定义五个按钮

Button bf=new Button("第一个");

Button bl=new Button("最后一个");

Button bn=new Button("下一个");

Button bp=new Button("上一个");

Button bg=new Button("搜索");

//定义一个单行文本框

TextField tf=new TextField();

//设置布局管理器

CardLayout cl=new CardLayout();

Winfive1() {

//设置窗口名称

this.setTitle("利用CardLayout布局组件");

this.setBackground(Color.blue);

this.setResizable(false);

this.setLayout(null);

this.add(p);

//定义p面板为cardlayout布局管理器

p.setLayout(cl);

//为cardlayout布局管理器添加组件

for(int i=1; i<=3; i++){

Button btemp=new Button("布局按钮"+i);

p.add(btemp, " "+i);

}

//为每个组件设置大小位置并添加到容器中

p.setBounds(10, 40, 100, 100);

this.add(bf);

bf.addActionListener(this);

bf.setBounds(120, 40, 60, 20);

this.add(bl);

bl.addActionListener(this);

bl.setBounds(120, 70, 60, 20);

this.add(bn);

bn.addActionListener(this);

bn.setBounds(120, 100, 60, 20);

this.add(bp);

bp.addActionListener(this);

bp.setBounds(120, 130, 60, 20);

this.add(bg);

bg.addActionListener(this);

bg.setBounds(60, 160, 40, 20);

this.add(tf);

tf.setBounds(20, 160, 40, 20);

//设置窗口的位置和大小

this.setBounds(200, 200, 400, 380);

//设置窗口的可见性

this.setVisible(true);

}

//实现ActionListener接口中的actionPerformed方法

public void actionPerformed(java.awt.event.ActionEvent e){

//next方法

if(e.getSource()==bn){

cl.next(p);

}

//previous方法

if(e.getSource()==bp){

cl.previous(p);

}

//first方法

if(e.getSource()==bf){

cl.first(p);

}

//last方法

if(e.getSource()==bl){

cl.last(p);

}

//show方法

if(e.getSource()==bg){

cl.show(p, tf.getText().trim());

tf.setText(" ");

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Winfive1();

}

}

4c08b837ac20209955b206b30cbfee88.png

Null:

import java.awt.*;

import java.awt.Event.*;

public class Winnull2 extends Frame{

//定义三个按钮

Button b1=new Button("甲");

Button b2=new Button("乙");

Button b3=new Button("丙");

public Winnull2() {

//设置窗口名称

this.setTitle("null布局");

//设置null布局管理器

this.setLayout(null);

//将组件添加到容器

this.add(b1);

this.add(b2);

this.add(b3);

//设置组件的大小和位置

b1.setBounds(50, 50, 100, 50);

b2.setBounds(50, 120, 100, 50);

b3.setBounds(50, 190, 100, 50);

//设置窗口位置和大小

this.setBounds(100, 100, 450, 400);

//设置窗口可见性

this.setVisible(true);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Winnull2();

}

}

33e19aa98523faacc69f8fbc18649035.png

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值