图像用户界面
1. 抽象窗口工具包(AWT)依赖于来创建图像用户界面(GUI)。
AWT包中提供了3大类:
1、容器类(container)
2、UI 组件类(UI component)
3、帮助类(layoutmanager 、 graphics 、 color)
2.JDK1.2版本后,引入新的包 Javax.swing.(轻重行)swing GUI组件在awt的基础上增加了很多功能。
Java.Awt包中的类体系结构
UI组件不能独立地显示出来,必须将组件放在一定的容器中才能显示出来。
在component 类中定义了AWT组件具有的一般功能如大小和位置控制,外形控制,组件的状态控制等。
AWT支持的组件(基本上UI组件):按钮、标签、文本框、文本区、复选框、单选按钮、滚动条、表格、菜单等。
容器
1、容器是component的子类,容器本身也是一个组件。另外具有容纳其他组件和容器的功能,通过容器来组织其他界面成分和元素。
2、容器类具有的功能是组件管理和布局管理。在组件管理中,包含的方法有add()、remove()、getcomponent()等,分别用于添加、删除组件和获得某个组件。
3、Conlainer类可直接或间接派生出两个常用容器:框架(frame)和面板(panel类)。
帮助类
1.Graphics类
2.布局管理类(layoutmanager):可调用容器类中的setlayout()方法。
3.Color(颜色)和font(字体)类。
容器类名组件—1
1.容器分为顶层容器和非顶层容器两大类。
顶层容器可以独立的窗口,不需要其他组件支持。
顶层容器的类是Windows,其重要子类是frame(框架)和dialog(对话框)。
2.非顶层容器不是独立窗口,必须位于窗口之内,
非顶层容器包括panle及scrollpane等,panel的重要子类是applet类。
布局类型
Java中的布局类型包括以下几种:
Flowlayout (流式布局)
Borderlasyout(边界布局)
Gridlayout(网格布局)
Cardlayout(卡片布局)
Boxlayout(框布局)
Gridbaglayout(网格包布局)
如何设置布局
1.每个容器(container对象)都有一个与它相关的缺省的布局管理器。
Applet的缺省布局是FlowLayout,Frame的缺省布局是BorderLayout,Panel的缺省布局是Flowlayout.
2.在没有设置新的布局前,在容器中添加组件都按照该容器的缺省布局排列。
3.可以通过setLayout()方法为容器设置新的布局。
FlowLayout 的构造函数有:
FlowLayout():生成一个默认的流式布局
FlowLayout(int alignment):可以设定每一行组件的对齐方式
FlowLayout(int alignment, int horz, int vert):可以设定组件间的水平和垂直距离。
第一节课代码:
package com.imau.gui;
import java.awt.*;
import javax.swing.*;
public class FrameDemo extends JFrame {
private JLabel label;
private JButton button;
private JButton bts[]=new JButton[16];
private JTextField field;
private void init() {
label=new JLabel("姓名");
label.setBounds(10,10,40,40);
//label.setSize(40, 40);
//label.setLocation(60, 10);
//label.setHorizontalAlignment(JLabel.CENTER);
//label.setForeground(Color.blue);
//label.setBackground(Color.green);
button=new JButton("按钮");
button.setSize(40,40);
button.setLocation(10, 10);
//按钮受否可点击
//button.setEnabled(false);
for(int i=0;i<bts.length;++i)
{
bts[i]=new JButton("按钮"+i);
}
field=new JTextField("aaaa",20);
}
public FrameDemo() {
init();
//2.设置布局
//空布局:添加组件的时候,组件没有大小,没有位置;创建组件时给定大小和位置
setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(label);
this.add(button);
this.add(field);
//add(bts[0],BorderLayout.NORTH);
//add(bts[1],BorderLayout.EAST );
//add(bts[2],BorderLayout.WEST);
//add(bts[3],BorderLayout.SOUTH);
//add(bts[4],BorderLayout.CENTER);
//1.设置标题 图标 大小 位置 可见性 窗口的事件
this.setTitle("这是一个窗口");
this.setSize(300, 300);
this.setLocation(400, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//2.用对象形式创建
public void test() {
JFrame frame= new JFrame();
frame.setTitle("这是一个窗口");
frame.setSize(300, 300);
frame.setLocation(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
登陆界面(这是分割线)
package com.imau.gui;
import javax.swing.*;
import java.awt.*;
public class Login extends JFrame {
private JLabel lname;
private JLabel lpass;
private JTextField tname;
private JTextField tpass;
private JButton login;
private void init() {
lname=new JLabel("用户名");
lpass=new JLabel("密 码");
tname=new JTextField(14);
tpass=new JTextField(14);
login=new JButton("登录");
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(lname);
this.add(tname);
this.add(lpass);
this.add(tpass);
this.add(login);
//1.获取屏幕大小
//2.设置为大小
this.setResizable(false);
this.setTitle("QQ登录");
this.setSize(260, 150);
this.setLocation(400, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Login() {
init();
}
}