• 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();
    	}
    
    }
    


    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();
    	}
    
    }
    



    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();
    	}
    
    }
    



    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();
    	}
    
    }
    



    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();
    	}
    
    }
    


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

  • 相关阅读:
    Cocos2dx for Windows Phone 8 发布
    Tiny Core Linux 4.7 发布
    走进 Google 的 Go 语言
    XCache 3.0.0 发布,PHP 性能提升方案
    Entropy Broker 2.0 发布,加密安全随机数
    Windows Phone 8 同步工具已提供,功能对比表
    PolarSSL 1.2.0 发布,SSL 加密库
    YUI 2 存在SWF漏洞,YUI 3 不受影响
    Google改进Dart并提供SDK
    Expression Blend实例中文教程(3) 布局控件快速入门Grid
  • 原文地址:https://www.cnblogs.com/wanglaoda/p/4937114.html
Copyright © 2020-2023  润新知