9 Java 图形用户界面

目录

 AWT

1.常用容器

2.布局管理器LaooutManager


AWT  abstract window toolkit 

Swing

均是java图形化用户界面GUI(Graphics User Interface)


 AWT

包:java.awt

主要有组件(Component),容器(Container),布局管理器(LayoutManager)。

组件必须放在一定的容器中才可以显示出来不能单独存在。组件父类Component

Container是Component的子类,是一个容器,也是一个组件。

部分方法:

getComponent(int x, int y); 获取坐标(x,y)上的组件对象

getForeground() 获取组件字体

getName() 获取组件名字

getSize()获取组件大小

paint(Graphics g) 绘制组件

repaint() 重新绘制组件

update()刷新组件

setVisible(Boolean b) 设置组件可见性

setSize(Dimension b) 设置组件大小

setName(String name) 设置组件名字

LayoutManager是一个接口。负责组件大小和位置,即布局管理器

FlowLayout 

BorderLayout

GridLayout

CardLayout

GridBagLayout


1.常用容器

常见容器:Window,Panel、ScrollPane

1. 窗口Frame类,是Component子类中的Container的Window的Frame。

import java.awt.Color;
import java.awt.Frame;

public class Test01{
    public static void main(String[] args) {
        Frame fr = new Frame("hapi");
        String name = fr.getName();
        
        System.out.println("InitName :" + name);
        System.out.println("Font :" + fr.getFont());
        System.out.println("Size :" + fr.getSize());
        System.out.println("=======================");
        
        fr.setName("what");
       fr.setSize(500,600);
       fr.setBackground(Color.BLUE);
       fr.setVisible(true);
       
        System.out.println("InitName :" + fr.getName());
        System.out.println("Font :" + fr.getFont());
        System.out.println("Size :" + fr.getSize());
       }
 }      

2.Panel 是  Component下Container的Panel

package TestChart9;
import java.awt.Panel;
import java.awt.Frame;
import java.awt.Color;
/**
 *
 * @author Administrator
 */
public class Test03 {
    
    /** Creates a new instance of Test03 */
    public static void main(String[] args){
        Frame fr = new Frame("helowman!!");
        Panel pl = new Panel();
        fr.setSize(500,800);
        fr.setBackground(Color.YELLOW);
        fr.setLayout(null);
        pl.setSize(200,300);
        pl.setBackground(Color.PINK);
        fr.add(pl);
        fr.setVisible(true);
    }
    
}

2.布局管理器LaooutManager

1.FlowLayout 

Panel和Applet 默认有这个布局管理器,容器(组件)放置规则从上到下,从左到右

import java.awt.*;
public class Test04       {
    private Frame f;
    private Button b1;
    private Button b2;
    public static void main(String[] args){
        Test04 two = new Test04();
        two.go();
    }
    
    public void go(){
        f = new Frame("Hello !!");
        f.setLayout(new FlowLayout());
        f.setSize(500,800);
        f.setBackground(Color.YELLOW);
        Panel pl = new Panel();
        pl.setSize(200,200);
        pl.setBackground(Color.PINK);
        
        b1 = new Button("bitch!");
        b1.setBackground(Color.orange);
        b2 = new Button("good");
        b2.setBackground(Color.GREEN);    
        
        pl.add(b1);
        pl.add(b2);
       
        f.add(pl);
       
        f.setVisible(true);
    
    } 
}

效果图:

FlowLayout(FlowLayout.RIGHT,20,40)

参数1:设置组件之间的对齐方式,默认则居中

参数2:设置组件之间横向间隔

参数3:设置组件之间上下间隔

 若一行只能放下一个组件,则窗口自动调整


2.BorderLayout 布局管理

BorderLayout是window ,Frame, Dialog的默认布局。

左西右东,上北下南,中5个方向把布局分为。

添加组件是需要备注添加方位,

import java.awt.*;
public class Test05 {
    
    /** Creates a new instance of Test05 */
   public static void main(String[] args){
        Frame fr = new Frame("BorderTest");
        fr.setBackground(Color.GRAY);
        fr.setLayout(new BorderLayout());
        fr.add("North", new Button("北"));
        fr.add("South", new Button("男"));
        fr.add("West", new Button("西"));
        fr.add("East", new Button("东"));
        fr.add("Center", new Button("中间"));
        fr.setVisible(true);
        fr.setSize(200,200);
    }
    
}

效果图:


3. GridLayout 布局管理

使组件在布局中是一个网格,平均占用容器的空间。和FlowLayout差不多的放置顺序,从上到下,从左到右;

GridLayout类构造函数GridLayout(int x, int y), x代表行,y代表列

import java.awt.*;
public class Test06 {
    
    /** Creates a new instance of Test06 */
   public static void main(String[] args){
        Frame f = new Frame("Grid Layout");
        f.setLayout(new GridLayout(2,3));
        f.setBackground(Color.BLUE);
        f.add(new Button("Test"));
        f.add(new Button("you"));
        f.add(new Button("Grdinman"));
        f.add(new Button("haha"));
        f.add(new Button("Come"));
        f.add(new Button("On"));
        f.setSize(300,300);
        f.setVisible(true);
        }
    
}

 效果图:


4.CardLayout 

卡片布局管理器,每一次像一张牌,只能看到这一张。每层只能放置一个组件可以使用Panel来实现复杂的组件。

import java.awt.*;
import java.awt.event.*;
public class Test07 implements ActionListener {
    
    /** Creates a new instance of Test07 */
   private Panel p1,p2,p3;
   private Button b1,b2,b3;
   private Frame f;
   private CardLayout CLayout = new CardLayout();
   
   public void create(){
    b1 = new Button("1");
    b2 = new Button("2");
    b3 = new Button("3");
    p1 = new Panel();  
    p1.setBackground(Color.RED);
    p1.setSize(10,10);
    p2 = new Panel();
    p2.setBackground(Color.PINK);
    p2.setSize(10,10);
    p3 = new Panel();
    p3.setBackground(Color.GRAY);
    p3.setSize(10,10);
    
    f = new Frame("test");
    p1.add(b1);
    b1.addActionListener(this);
    p2.add(b2);
    b2.addActionListener(this);
    p3.add(b3);
    b3.addActionListener(this);
    
    f.setLayout(CLayout);
    f.add(p1,"第一次");
    f.add(p2,"第er次");
    f.add(p3,"第san次");
    f.setSize(300,300);
    f.setVisible(true);
   
   }
   
   public static void main(String[] args){
    Test07 t = new Test07();
    t.create();
   
   }
   public void actionPerformed(ActionEvent e){
    CLayout.next(f);
   }
    
}

5.容器嵌套

组件放在容器中,该容器也可以放入一个容器当中

import java.awt.*;
public class Test08 {
    private Frame f;
    private Panel p;
    private Button bw,bc;
    private Button bfile,bhelp;
    private TextArea ta;
    
    
    public void create(){
        f = new Frame("Loop!");
        ta = new TextArea("文本框");
        bw = new Button("West");
        bc = new Button("工作区");
        
        f.add(bw,"West");
        f.add(bc,"Center");
        
        p = new Panel();
        p.setLayout(new BorderLayout());
        f.add(p,"North");
        
        bfile = new Button("文件");
        bhelp = new Button("帮助");
        
        p.add(bfile,"North");
        p.add(bhelp,"West");
        p.add(ta,"Center");
        
        f.pack();
        f.setVisible(true);  
    }
    
    public static void main(String[] args){
        Test08 tn = new Test08();
        tn.create();    
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值