GUI(图形用户接口)
用图形的方式,来显示计算机操作的界面,这样更方便更直观
CLI(命令行用户接口)
常用的Dos命令操作
Awt
java.awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
布局管理器
容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器):从左到右顺序排列
BorderLayout(边界布局管理器):东,南,西,北,中
GridLayout(网格布局管理器):规则的矩阵
CardLayout(卡片布局管理器):选项卡
GridBagLayout(网格布局管理器):非规则的矩阵
Component
组件是具有可以在屏幕上显示并且可以与用户交互的图形表示的对象。 组件的示例是典型图形用户界面的按钮,复选框和滚动条。
Frame
package com.monfolld.gui;
import javax.swing.*;
import java.awt.*;
public class FrameDemo {
public static void main(String[] args){
Frame f=new Frame("大司马金牌主厨");
/*f.setSize(500,400);
f.setLocation(400,200);*/
f.setBounds(400,200,500,400);
f.setLayout(new FlowLayout());//设置流式布局
JButton but1=new JButton("肉蛋葱鸡");
JButton but2=new JButton("红皮鸭子");
JButton but3=new JButton("鬼屎咖啡");
JButton but4=new JButton("老千层饼");
JButton but5=new JButton("炸胡橙汁");
f.add(but1);//将按钮添加到窗体中
f.add(but2);
f.add(but3);
f.add(but4);
f.add(but5);
f.setVisible(true);
System.out.println("over");
}
}
事件监听机制
事件源(组件),事件(Event),监听器(Listener),事件处理(引发事件后处理方式)
Button
void addActionListener(ActionListener l) //添加指定的动作侦听器以从此按钮接收动作事件
Interface ActionListener
用于接收动作事件的侦听器界面。 有兴趣处理动作事件的类实现此接口,并使用该组件的addActionListener
方法将使用该类创建的对象注册到组件中。 当事件发生时,将调用该对象的actionPerformed
方法。
WindowEvent
指示窗口已更改其状态的低级别事件。 当Window对象被打开,关闭,激活,停用,图标化或去隐藏时,或当焦点被转移到窗口中时,这个低级事件是由Window对象生成的。
该事件被传递给每个WindowListener
或WindowAdapter
对象,其注册使用窗口的addWindowListener
方法接收此类事件。 ( WindowAdapter
对象实现WindowListener
接口。)每个这样的侦听器对象在事件发生时得到这个WindowEvent
。
WindowListener
用于接收窗口事件的侦听器界面。 有兴趣处理窗口事件的类可以实现这个接口(和它包含的所有方法),或扩展抽象的WindowAdapter
类(仅覆盖感兴趣的方法)。 然后使用该窗口的addWindowListener
方法将该类创建的侦听器对象注册到一个窗口中。 当窗口的状态由于打开,关闭,激活或停用,图标化或去隐藏而变化时,将调用侦听器对象中的相关方法,并将
void windowActivated(WindowEvent e) //当窗口设置为活动窗口时调用。
void windowClosed(WindowEvent e) //当窗口关闭时调用窗口调用处理结果时调用。
void windowClosing(WindowEvent e) //当用户尝试从窗口的系统菜单中关闭窗口时调用。
void windowDeactivated(WindowEvent e) //当窗口不再是活动窗口时调用。
void windowDeiconified(WindowEvent e) //当窗口从最小化更改为正常状态时调用。
void windowIconified(WindowEvent e) //当窗口从正常状态更改为最小化状态时调用。
void windowOpened(WindowEvent e) //第一次调用窗口可见。
package com.monfolld.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameDemo {
public static void main(String[] args){
Frame f=new Frame("大司马金牌主厨");
/*f.setSize(500,400);
f.setLocation(400,200);*/
f.setBounds(400,200,500,400);
f.setLayout(new FlowLayout());//设置流式布局
JButton but1=new JButton("肉蛋葱鸡");
JButton but2=new JButton("红皮鸭子");
JButton but3=new JButton("鬼屎咖啡");
JButton but4=new JButton("老千层饼");
JButton but5=new JButton("炸胡橙汁");
f.add(but1);//将按钮添加到窗体中
f.add(but2);
f.add(but3);
f.add(but4);
f.add(but5);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//在按钮上加上监听
but1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
but2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
but3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
but4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
but5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.setVisible(true);
System.out.println("over");
}
}
鼠标事件
MouseEvent
指示组件中发生鼠标操作的事件。 如果且仅当鼠标光标位于组件边界的视野不足部分时,才会在特定组件中发生鼠标动作。 对于轻量级组件(如Swing的组件),如果组件上已启用鼠标事件类型,则鼠标事件仅会分派到组件。 通过将相应的基于鼠标的EventListener
添加到组件( MouseListener
或MouseMotionListener
)或通过使用适当的掩码参数( AWTEvent.MOUSE_EVENT_MASK
或AWTEvent.MOUSE_MOTION_EVENT_MASK
)调用Component.enableEvents(long)
来启用鼠标事件类型。 如果组件上尚未启用鼠标事件类型,则将相应的鼠标事件分派到已启用鼠标事件类型的第一个祖先。
MouseAdapter
用于接收鼠标事件的抽象适配器类。 这个类中的方法是空的。 此类作为创建侦听器对象的方便而存在。
鼠标事件可以让您跟踪鼠标按下,释放,点击,移动,拖动,进入组件,退出时和鼠标滚轮移动时。
void mouseClicked(MouseEvent e) //在组件上单击(按下并释放)鼠标按钮时调用。
void mouseDragged(MouseEvent e) //在组件上按下鼠标按钮然后拖动时调用。
void mouseEntered(MouseEvent e) //当鼠标进入组件时调用。
void mouseExited(MouseEvent e) //当鼠标退出组件时调用。
void mouseMoved(MouseEvent e) //当鼠标光标移动到组件上但没有按钮被按下时调用。
void mousePressed(MouseEvent e) //在组件上按下鼠标按钮时调用。
void mouseReleased(MouseEvent e) //在组件上释放鼠标按钮时调用。
void mouseWheelMoved(MouseWheelEvent e) //当鼠标滚轮旋转时调用。
package com.monfolld.gui;
import org.junit.Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseAndKeyDemo {
private Frame f;
private TextField tf;
private JButton but;
public MouseAndKeyDemo(){
init();
}
private void init(){
f=new Frame("演示键盘和鼠标监听");
f.setBounds(400,200,500,400);
f.setLayout(new FlowLayout());
tf=new TextField(35);
but=new JButton("一个按钮");
f.add(tf);
f.add(but);
myEvent();
f.setVisible(true);
}
private void myEvent(){
//关闭窗口退出
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//动作监听
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("acton run");
}
});
//在按钮上添加一个鼠标监听事件
but.addMouseListener(new MouseAdapter() {
private int count=1;
@Override
public void mouseEntered(MouseEvent e) {
//System.out.println("mouse enter"+count);
//tf.setText("mouse enter..."+count++);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount()==2)
tf.setText("mouse double click..."+count++);
//System.out.println("mouse enter"+count++);
//tf.setText("mouse clicker..."+count++);
}
});
}
public static void main(String[] args){
new MouseAndKeyDemo();
}
}
键盘事件
KeyEvent
指示组件中发生击键的事件。
当按下,释放或键入键时,此低级别事件由组件对象(例如文本字段)生成。 该事件传递给每个KeyListener
或KeyAdapter
对象,该对象使用组件的addKeyListener
方法注册接收此类事件。 ( KeyAdapter
对象实现KeyListener
接口。)当事件发生时,每个这样的侦听器对象获得这个KeyEvent
。
KeyAdapter
用于接收键盘事件的抽象适配器类。 这个类中的方法是空的。 此类作为创建侦听器对象的方便而存在。
扩展此类以创建一个KeyEvent
监听器并覆盖感兴趣的事件的方法。 (如果您实现了KeyListener
接口,则必须定义其中的所有方法,这个抽象类为它们定义了空方法,因此您只能为所关心的事件定义方法。)
void keyPressed(KeyEvent e) //按下键时调用。
void keyReleased(KeyEvent e) //当键已被释放时调用。
void keyTyped(KeyEvent e) //键入键时调用。
package com.monfolld.gui;
import org.junit.Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseAndKeyDemo {
private Frame f;
private TextField tf;
private JButton but;
public MouseAndKeyDemo(){
init();
}
private void init(){
f=new Frame("演示键盘和鼠标监听");
f.setBounds(400,200,500,400);
f.setLayout(new FlowLayout());
tf=new TextField(35);
but=new JButton("一个按钮");
f.add(tf);
f.add(but);
myEvent();
f.setVisible(true);
}
private void myEvent(){
//给文本框添加键盘监听
tf.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
//System.out.println("key run..."+KeyEvent.getKeyText(e.getKeyCode())+"::::"+e.getKeyCode());
int code=e.getKeyCode();
if (!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){
System.out.println("必须是数字");
e.consume();
}
if (e.getKeyCode()==KeyEvent.KEY_FIRST){
System.out.println("enter run...");
}
}
});
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("acton run");
}
});
//在按钮上添加一个鼠标监听事件
but.addMouseListener(new MouseAdapter() {
private int count=1;
@Override
public void mouseEntered(MouseEvent e) {
//System.out.println("mouse enter"+count);
//tf.setText("mouse enter..."+count++);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount()==2)
tf.setText("mouse double click..."+count++);
//System.out.println("mouse enter"+count++);
//tf.setText("mouse clicker..."+count++);
}
});
}
public static void main(String[] args){
new MouseAndKeyDemo();
}
}
Swing
javax.swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全java实现。增强了移植性,属轻量级控件。