MENU

GUI编程

July 23, 2018 • Read: 5234 • Java阅读设置

AWT

AWT(Abstract Window Toolkit)包含了很多类和接口,用于Java Application的GUI(Graphics User Interface图形用户界面)编程。使用AWT所涉及的类一般在java.awt包及其子包中。Container和Component是AWT中的两个核心包

Component&Container

  • java的图形用户界面最基本组成部分是Component,Component类及其子类的对象用来描述以图形化的方式显示在屏幕上并能与其用户进行交互的GUI元素,例如,一个按钮,一个标签等。
  • 一般的Component对象不能独立地显示出来,必须“放在”某一Container对象中才可以显示出来
  • Container是Component子类,Container子类对象可以“容纳”别的Component对象
  • Container对象可使用方法add(...)向其中添加其他Component对象
  • Container是Component的子类,因此Container对象也可被当作Component对象添加到其他Container对象中
  • 有两种常用的Container:

    • Window:其对象表示自由停泊的顶级窗口
    • Panel:其对象可作为容纳其它Componet对象,但不能独立存在,必须被添加到其它Container中(如Window或Applet)

Frame

  • Frame是Window的子类,由Frame或其子类创建的对象为一个窗体
  • Frame的常用构造方法:

    • Frame()
    • Frame(String s)创建标题栏为字符串s的窗口
setBounds(int x,int y,int width,int height)//设置窗体位置和大小,x,y是左上角坐标,width和height是宽度和高度
setSize(int width,int height)//设置窗体的大小,width和height分别是宽度和高
setLocation(int x,int y)//度设置窗体的位置,x,y是左上角坐标
setBackground(Color c)//设置背景颜色,参数为Color对象
setVisible(boolean b)//设置是否可见
setTitle(String name)//设置窗体标题
setResizable(boolean b)//设置是否可以调整大小
import java.awt.*;
public class TestFrame {
    public static void main(String[] args) {
        Frame f = new Frame("My First Test");
        f.setSize(600,600);//窗体大小,单位像素
        f.setBackground(Color.red);//窗体背景色
        f.setVisible(true);//窗体是否可见
    }
}

上面是绘制使用Frame类绘制窗口的一个示例,如果不调用setVisible方法是无法看到窗口的。整个电脑左上角是坐标原点,往右是x轴增大的方向,往下是y轴增大的方向,setLocation(300,300)是窗口最左上角点的坐标,显示效果见下图

import java.awt.*;
public class TestMultiFrame {
    public static void main(String[] args) {
        MyFrame f1 = new MyFrame(100,100,400,400,Color.red);
        MyFrame f2 = new MyFrame(500,100,400,400,Color.yellow);
        MyFrame f3 = new MyFrame(100,500,400,400,Color.blue);
        MyFrame f4 = new MyFrame(500,500,400,400,Color.green);
    }
}
class MyFrame extends Frame {
    private static int id = 0;
    MyFrame(int x,int y,int w,int h,Color color) {
        super("MyFrame" + (++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }
}

Panel

  • Panel对象可以看成可以容纳Component的空间
  • Panel对象可以拥有自己的布局管理器
  • Panel类拥有其父类继承来的

    • setBounds(int x,int y,int width,int height)
    • setSize(int width,int height)
    • setLocation(int x,int y)
    • setBackground(Color c)
    • SetLayout(LayoutManager mgr)
  • Panel的构造方法

    • Panel():使用默认的FlowLayout类布局管理器初始化
    • Panel(LayoutManager layout):使用指定的布局管理器初始化
import java.awt.*;
public class Testpanel {
    public static void main(String[] args) {
        Frame f = new Frame("Java Frame with Panel");
        Panel p = new Panel(null);
        f.setLayout(null);
        f.setBounds(300,300,500,500);
        f.setBackground(new Color(0,0,102));
        p.setBounds(50,50,400,400);
        p.setBackground(new Color(204,204,255));
        f.add(p);
        f.setVisible(true);
    }
}

有一点要注意,Frame的setBounds方法参数中x,y的位置是相对于屏幕,而Panel装到哪个里面,x,y就相对于哪一个窗口的位置,在这里就是相对于外层的Frame

import java.awt.*;
class MyFrame2 extends Frame {
    private Panel p1,p2,p3,p4;
    MyFrame2(String s,int x,int y,int w,int h) {
        super(s);
        setLayout(null);
        p1 = new Panel(null);p2 = new Panel(null);
        p3 = new Panel(null);p4 = new Panel(null);
        p1.setBounds(0,0,w/2,h/2);
        p2.setBounds(0,h/2,w/2,h/2);
        p3.setBounds(w/2,0,w/2,h/2);
        p4.setBounds(w/2,h/2,w/2,h/2);
        p1.setBackground(Color.red);
        p2.setBackground(Color.yellow);
        p3.setBackground(Color.blue);
        p4.setBackground(Color.green);
        add(p1);add(p2);add(p3);add(p4);
        setBounds(x,y,w,h);
        setVisible(true);
    }
}
public class TestMultiPanel {
    public static void main(String[] args) {
        new MyFrame2("MyFrame with Panel",300,300,400,300);
    }
}

作业

设计一个如下图所示的窗口

Last Modified: May 12, 2021
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

2 Comments
  1. ............ ............

    作业???

    1. mathor mathor

      @............一个小任务而已