GUI
Graphical User Internet(图形用户接口)。
用图形的方式,来显示计算机操作的界面,这样更方便更直观。
CLI
Command line User Interface(命令行用户接口)
就是常见的Dos命令行操作。
需要记忆一些常用的命令。
举例:
比如:创建文件夹,或者删除文件夹等。
Java为GUI提供的对象都存在Java.Awt和javax.Swing两个包中。
Awt与Swing
java.Awt:Abstract Window ToolKit(抽象窗口工具包)。需要调用本地系统方法实现功能,属于重量级控件。
javax.Swing:在AWT基础上,建立了一套图形界面系统,其中提供了更多的组建,而且完全由JAVA实现。增强了移植性,属于轻量级控件。
继承关系图
布局管理器
容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列。
Panel默认的布局管理器。
BorderLayout(边界布局管理器)
东 南 西 北 中
Frame默认的布局管理器。
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
事件监听机制
事件源(组件):就是awt包或者swing包中的那些图形界面组件。
事件(Event):每一个事件源都有自己特有的对应事件和共性事件。
监听器(Listener):将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
事件处理:(引发事件后处理方式)
事件源,事件和监听器在java中都已经定义好了。直接获取其对象来用就可以了。我们要做的事情,就是对产生的动作进行处理。
事件监听机制流程图:
窗体事件
import java.awt.*;
import java.awt.event.*;
/*
创建图形化界面:
1.创建frame窗体。
2.对窗体进行基本设置。
比如大小 位置 布局。
3.定义组件。
4.将组件通过窗体的add方法添加到窗体中。
5.让窗体显示,通过setVisible(true)。
*/
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 MyWin());
f.setVisible(true);
}
}
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的api描述,发现按钮支持一个特有监听addActionListener
*/
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new FrameDemo();
}
}
鼠标和键盘事件
import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent
{
//定义该图形中所需的组件的引用。
private Frame f;
private Button but;
MouseAndKeyEvent()
{
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);
}
});
but.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入到该组件");
}
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
System.out.println("鼠标双击");
}
});
//给but添加一个键盘监听。
but.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ESC)
System.exit(0);
//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());
}
});
}
public static void main(String[] args)
{
new MouseAndKeyEvent();
}
}
练习-列出指定目录内容
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class MyWindowDemo
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyWindowDemo()
{
init();
}
public void init()
{
f= new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(30);
but = new Button("转到");
ta = new TextArea(15,40);
d = new Dialog(f,"提示信息-self",true);
d.setBounds(400,200,240,150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyWindowDemo();
}
private void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())
{
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "您输入的信息"+dirPath+"是错误的,请重新输入";
lab.setText(info);
d.setVisible(true);
}
tf.setText("");
}
}
菜单练习:
打开文件
保存文件
package mymenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuDemo
{
private Frame f;
private MenuBar mb;
private TextArea ta;
private Menu m,subMenu;
private File file;
private MenuItem openItem,saveItem,closeItem,subItem;
private FileDialog openDia,saveDia;
MyMenuDemo()
{
init();
}
public void init()
{
f= new Frame("my window");
f.setBounds(300,100,650,600);
//f.setLayout(new FlowLayout());
mb = new MenuBar();
ta = new TextArea();
m = new Menu("文件");
subMenu = new Menu("子菜单");
subItem = new MenuItem("子条目");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
closeItem = new MenuItem("退出");
subMenu.add(subItem);
m.add(subMenu);
m.add(openItem);
m.add(saveItem);
m.add(closeItem);
mb.add(m);
f.setMenuBar(mb);
openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);
saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
saveItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath==null || fileName==null)
return;
file = new File(dirPath,fileName);
}
try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}
catch (IOException b)
{
throw new RuntimeException("存储失败");
}
}
});
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
//System.out.println(dirPath+".."+fileName);
if(dirPath==null || fileName==null)
return;
ta.setText("");
File file = new File(dirPath,fileName);
try
{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
}
catch (IOException x)
{
throw new RuntimeException("读取失败");
}
}
});
closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyMenuDemo();
}
}