内容概述
- GUI概述
- awt和swing包概述
- GUI继承体系图
- 布局管理器
- 事件监听机制和适配器模式
- GUI案例
一、GUI概述
什么是GUI:
Graphical User Interface(图形用户接口)
什么是CLI:
Command line User Interface (命令行用户接口)
awt包和swing包:
1.awt包:
1).Abstract Window ToolKit (抽象窗口工具包)
2).是最早出现的,桌面应用程序开发工具包;
3).调用本地方法(底层操作系统的类库)实现功能;不是跨平台的,属于重量级的工具包;运行速度快;
2.swing包:
1).在AWT包的基础上建立的图形用户界面工具包;
2).是后期出现的;
3).不调用本地方法;它是跨平台的,属于轻量级的工具包;纯Java编写的;运行速度要稍微慢一些;
三、GUI继承体系图
四、布局管理器
五、事件监听机制和适配器模式
为窗体添加事件监听
public class MyListener implements WindowListener {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你要关了我么?好吧,那拜拜了.....");
//结束程序,我们这里就直接结束JVM
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("我被最小化了......");
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
/*
* 添加事件监听:
*
* 1.事件源:Frame;
* 2.可能会发生什么事:点击"关闭"按钮;
* 3.如果发生这样的事,该怎么办:
* 自定义类,实现WindowListener接口,重写windowClosing()方法;
* 4.关联
*/
public class Demo {
public static void main(String[] args) {
Frame f = new Frame("为窗体添加关闭事件");
f.setBounds(400, 150, 500, 400);
f.addWindowListener(new MyListener());
f.setVisible(true);
}
}
适配器模式
public interface IA {
public void fun1();
public void fun2();
public void fun3();
public void fun4();
public void fun5();
public void fun6();
public void fun7();
}
public abstract class AbstractA implements IA{
@Override
public void fun1() {
}
@Override
public void fun2() {
}
@Override
public void fun3() {
}
@Override
public void fun4() {
}
@Override
public void fun5() {
}
@Override
public void fun6() {
}
@Override
public void fun7() {
}
}
public class Test1 extends AbstractA{
//需要哪个方法,就重写哪个方法就可以了
@Override
public void fun1() {
System.out.println("fun1()");
}
}
适配器模式更改窗体关闭事件
/*
* Java类库中,几乎所有的XxxxListener类都有相应的适配器类:XxxxAdapter
*/
public class Demo {
public static void main(String[] args) {
Frame f = new Frame("使用适配器模式");
f.setBounds(400,150,500,500);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}
六、GUI案例
为窗体添加按钮
/*
* 为窗体添加按钮:
* 1.需要一个窗体:Frame;
* 2.需要一个按钮:Button;并添加事件;
* 按钮事件,只需要一个事件ActionListener(接口),它里面就一个方法,实现即可;
* 3.将按钮添加到窗体;
* 4.显示窗体;
*/
public class Demo {
public static void main(String[] args) {
final Frame f = new Frame("带按钮的窗体");
f.setBounds(400, 150, 500, 500);
//将窗体的布局管理器设为:null
f.setLayout(null);
//添加一个按钮
final Button but = new Button("按我啊");
but.setSize(100,50);
but.setLocation(200, 150);
but.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Random rdm = new Random();
int x = rdm.nextInt(f.getWidth()) ;
int y = rdm.nextInt(f.getHeight());
if(x + but.getWidth() > f.getWidth()){
x = f.getWidth() - but.getWidth();
}
if(y + but.getHeight() > f.getHeight()){
y = f.getHeight() - but.getHeight();
}
but.setLocation(x, y);
}});
//将按钮添加到窗体
f.add(but);
//为窗体添加关闭事件
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//显示窗体
f.setVisible(true);
}
}
练习_模拟打地鼠
public class ButListener implements ActionListener {
private Label lab = null;
public ButListener(Label lab){
this.lab = lab;
}
@Override
public void actionPerformed(ActionEvent e) {
if(Demo.flag == false){
//取lab中的数字
String s = lab.getText();
int num = Integer.parseInt(s);
num++;
lab.setText(String.valueOf(num));
Demo.flag = true;
}
}
}
public class MyThread extends Thread{
private Frame frame ;
private Button[] butArray;
private Label label;
public MyThread(Frame f,Button[] butArray,Label lab){
this.frame = f;
this.butArray = butArray;
this.label = lab;
}
@Override
public void run() {
//1.先全部隐藏
for(Button but : butArray){
but.setVisible(false);
}
//随机抽取一个,并显示此按钮
Random rdm = new Random();
while(true){
int index = rdm.nextInt(butArray.length);
butArray[index].setVisible(true);
Demo.flag = false;
//只显示1秒,然后就隐藏
try {
Thread.sleep(1500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
butArray[index].setVisible(false);
}
}
}
public class Demo {
private static int currentIndex = -1;
public static boolean flag = false;
public static void main(String[] args) {
final Frame f = new Frame("打地鼠");
f.setBounds(400,150,600,600);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setLayout(null);
//添加一个标签,计数
final Label lab = new Label("0");
lab.setBounds(10, 30, 100, 50);
f.add(lab);
//添加按钮
Button but1 = new Button("老鼠1");
Button but2 = new Button("老鼠2");
Button but3 = new Button("老鼠3");
Button but4 = new Button("老鼠4");
Button but5 = new Button("老鼠5");
Button but6 = new Button("老鼠6");
but1.addActionListener(new ButListener(lab));
but2.addActionListener(new ButListener(lab));
but3.addActionListener(new ButListener(lab));
but4.addActionListener(new ButListener(lab));
but5.addActionListener(new ButListener(lab));
but6.addActionListener(new ButListener(lab));
//定义位置:
but1.setBounds(100, 80, 100, 50);
but2.setBounds(210, 80, 100, 50);
but3.setBounds(160, 180, 100, 50);
but4.setBounds(360, 190, 100, 50);
but5.setBounds(120, 290, 100, 50);
but6.setBounds(240, 300, 100, 50);
//添加按钮
f.add(but1);
f.add(but2);
f.add(but3);
f.add(but4);
f.add(but5);
f.add(but6);
//定义一个六个按钮的数组
final Button[] butArray = new Button[6];
butArray[0] = but1;
butArray[1] = but2;
butArray[2] = but3;
butArray[3] = but4;
butArray[4] = but5;
butArray[5] = but6;
//添加一个start按钮
Button butStart = new Button("开始");
butStart.setSize(100,50);
butStart.setLocation(500, 520);
butStart.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new MyThread(f,butArray,lab).start();
}
});
f.add(butStart);
f.setVisible(true);
}
}
练习_把文本框的值转移到文本域案例
/*
* 把文本框的值转移到文本域案例
* 需要的组件:
* 1.窗体;
* 2.文本框:TextField
* 3.按钮:Button
* 4.文本域:TextArea
*/
public class Demo {
public static void main(String[] args) {
Frame f = new Frame("练习:将文本框中的数据转移到文本域");
f.setBounds(400,150,300,300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//将窗体设定为:流式布局管理器
f.setLayout(new FlowLayout());
//实例化所需的控件
final TextField txt = new TextField(20);
final Button but = new Button("转移数据");
final TextArea txtArea = new TextArea(10,30);
//为按钮添加事件
but.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//获取文本框中的内容;
String s = txt.getText();
if(s.trim().length() == 0){
return;
}
//清空文本框
txt.setText("");
//转移到文本域中
//取出原文本域中的内容
String oldStr = txtArea.getText();
if(oldStr.trim().length() == 0){
txtArea.setText(s);
}else{
txtArea.setText(oldStr + "\r\n" + s);
}
//请求焦点
txt.requestFocusInWindow();
}});
//将控件添加到窗体
f.add(txt);
f.add(but);
f.add(txtArea);
f.setVisible(true);
}
}
通过鼠标移动到按钮上更改背景色案例
/*
* 通过鼠标移动到按钮上更改背景色案例
* 需要的组件:
* 1.窗体;
* 2.四个按钮;
* 3.为四个按钮添加事件;
*/
public class Demo {
public static void main(String[] args) {
final Frame f = new Frame("改变窗体背景颜色");
f.setBounds(400,150,500,500);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//设置流式布局管理器
f.setLayout(new FlowLayout());
//实例化一些按钮
Button redBut = new Button("红色");
Button greenBut = new Button("绿色");
Button blueBut = new Button("蓝色");
Button resetBut = new Button("还原");
//设置事件
redBut.addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e) {
//改变窗体背景色
f.setBackground(Color.red);
}
});
greenBut.addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e) {
//改变窗体背景色
f.setBackground(Color.green);
}
});
blueBut.addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e) {
//改变窗体背景色
f.setBackground(Color.blue);
}
});
resetBut.addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e) {
//改变窗体背景色
f.setBackground(Color.white);
}
});
//将按钮添加到窗体
f.add(redBut);
f.add(greenBut);
f.add(blueBut);
f.add(resetBut);
f.setVisible(true);
}
}
练习_多级菜单
/*
* 多级菜单:
*/
public class Demo {
public static void main(String[] args) {
final Frame f = new Frame("多级菜单");
f.setBounds(400,150,500,500);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//制作菜单
MenuBar bar = new MenuBar();
//两个Menu
Menu menu1 = new Menu("文件");
Menu menu2 = new Menu("更改标题");
//五个MenuItem
MenuItem item1 = new MenuItem("打开记事本");
MenuItem item2 = new MenuItem("退出系统");
MenuItem item3 = new MenuItem("好好学习");
MenuItem item4 = new MenuItem("天天向上");
MenuItem item5 = new MenuItem("恢复标题");
item1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Runtime time = Runtime.getRuntime();
try {
time.exec("notepad");
} catch (IOException e1) {
e1.printStackTrace();
}
}});
item2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}});
item3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
f.setTitle("好好学习");
}});
item4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
f.setTitle("天天向上");
}});
item5.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
f.setTitle("多级菜单");
}});
//关联菜单
menu2.add(item3);
menu2.add(item4);
menu2.add(item5);
menu1.add(menu2);
menu1.add(item1);
menu1.add(item2);
bar.add(menu1);
f.setMenuBar(bar);
f.setVisible(true);
}
}