GUI
Graphical User Interface(图形用户借口)
用图形的方式,来显示计算机操作的界面,这样更方便更直观。
CLI
Comand line User Interface(命令行用户借口)
就是常见的DOS命令行操作。
需要记忆一些常用的命令,操作不直观。
Java为GUI提供的对象都存在Java.Awt和javax.Swing两个包中。
Awt与Swing
Java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属于重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属于轻量级控件。
-------------------------
布局管理器
容器中的组件的排放方式,就是布局。
常见的布局管理器:
Flow Layout(流式布局管器)
从左到右的顺序排列。
Panel默认的布局管理器
BorderLayout(边界布局管理器)
东,南,西,北,中
Frame默认的布局管理器。
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
事件监听机制组成
事件源(组件)
事件(Event)
监听器(Lisener)
事件处理(引发事件后处理方式)
事件监听机制的特点:
1,事件源:就是awt包或者swing包中的那些图形界面组件.
2,事件:每一个事件源都有自己特有的对应事件和共性事件
3,监听器:将可以触发某一个事件的动作都已经封装到了监听器中
4,事件处理
实现代码:
class AwtDemo
{
public static void main(String[] args)
{
Frame f = new Frame("my awt");
f.setSize(500,400);
f.setLocation(300,200);
f.setLayout(new FlowLayout());
Button b = new Button("我是一个按钮");
f.add(b);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("我关");
System.exit(0);
}
public void windowActivated(WindowEvent e)
{
System.out.println("我活了。");
}
public void windowOpened(WindowEvent e)
{
System.out.println("我被打开了,hahahhahah");
}
});
f.setVisible(true);
//System.out.println("Hello World!");
}
}
class MyWin implements WindowListener
{
覆盖7个方法。可以我只用到了关闭的动作。
其他动作都没有用到,可是却必须复写。
}
因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。
并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可。
class MyWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.out.println("window closing---"+e.toString());
System.exit(0);
}
}
事件监听机制流程图:
Action事件代码演示:
import java.awt.*;
import java.awt.event.*;
class FrameDemo
{
定义该图形中所需的组件的引用。
private Frame f;
private Button but;
FrameDemo()
{
init();
}
public void init()
{
f = new Frame("my frame");
对frame进行基本设置。
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but = new Button("my button");
将组件添加到frame中
f.add(but);
加载一下窗体上事件。
myEvent();
显示窗体;
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
让按钮具备退出程序的功能
按钮就是事件源。
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。
需要查看该组件对象的功能。
通过查阅button的描述。发现按钮支持一个特有监听addActionListener。
but.addActionListener(new ActionListener()
{
private int count = 1;
public void actionPerformed(ActionEvent e)
{
System.out.println("退出,按钮干的");
System.exit(0);
f.add(new Button("Button-"+(count++)));
f.setVisible(true);
f.validate();
//System.out.println(e.getSource());
Button b = (Button)e.getSource();
Frame f1 = (Frame)b.getParent();
f1.add(new Button("button-"+count++));
f1.validate();
}
});
}
public static void main(String[] args)
{
new FrameDemo();
}
}
鼠标键盘事件代码演示:
import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent
{
private Frame f;
private Button but;
private TextField tf;
MouseAndKeyEvent()
{
init();
}
public void init()
{
f = new Frame("my frame");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(20);
but = new Button("my button");
f.add(tf);
f.add(but);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9))
{
System.out.println(code+".....是非法的");
e.consume();
}
}
});
给But添加一个键盘监听。
but.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
System.exit(0);
System.out.println("ctrl+enter is run");
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("action ok");
}
});
but.addMouseListener(new MouseAdapter()
{
private int count = 1;
private int clickCount = 1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入到该组件"+count++);
}
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
System.out.println("双击动作"+clickCount++);
}
});
}
public static void main(String[] args)
{
new MouseAndKeyEvent();
}
}