GUI 图形用户界面
GUI
Graphical User Interface(图形用户接口)
用图形的方式,来显示计算机操作的界面
CLI
Command line User Interface(命令行用户接口)
Java为GUI提供的对象都存在java.Awt和javax.Swing包中
java.Awt:Abstract Window Toolkit(抽象窗口工具包),
需要调用本地系统方法实现功能。和系统的依赖性相对较强。
java.Swing:在Awt的基础上,建立的一套图形界面系统,
其中提供了更多的组件,而且完全由java实现,增强了
移植性。和系统的依赖性相对较弱。
Eclipse是纯java编写的,用的是Swt界面。
Component 构建{Container容器(Window窗口(Frame框架,Dialog对
话框(FileDialog)),Panel面板),(Button,Label,
CheckBox,TextComponent文本组件(TextArea文本域
多行文本,TextField文本框 单行))}
Container:是一个特殊的组件,该组件可以通过add方法添加其他组件进来
布局管理器
容器中的组件的排放方式即布局。
常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列,默认组件都是居中的
Panel默认的布局管理器
BorderLayout(边界布局管理器)
东 南 西 北 中
Frame默认的布局管理器
若未指定方向,会以窗体最大面积填充窗体,添加新组建会覆盖
GridLayout(网络布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
用户对组件的操作就是一个事件。产生事件的组件就是事件源。
接受并处理事件,与用户进行交互的行为就是事件处理器。
这些处理方式都封装在监听器中。
事件监听机制的特点:
1.事件源 :就是awt包或者swing包中的那些图形界面组件
2.事件 :每一个事件源都有自己特有的对应事件和共性事件
3.监听器 :将可以触发某一事件的动作(不止一个动作)都已经封装到了监听器中
4.事件处理
以上三者,在java中都已经定义好了。
直接获取其对象用即可。
GUI
Graphical User Interface(图形用户接口)
用图形的方式,来显示计算机操作的界面
CLI
Command line User Interface(命令行用户接口)
Java为GUI提供的对象都存在java.Awt和javax.Swing包中
java.Awt:Abstract Window Toolkit(抽象窗口工具包),
需要调用本地系统方法实现功能。和系统的依赖性相对较强。
java.Swing:在Awt的基础上,建立的一套图形界面系统,
其中提供了更多的组件,而且完全由java实现,增强了
移植性。和系统的依赖性相对较弱。
Eclipse是纯java编写的,用的是Swt界面。
Component 构建{Container容器(Window窗口(Frame框架,Dialog对
话框(FileDialog)),Panel面板),(Button,Label,
CheckBox,TextComponent文本组件(TextArea文本域
多行文本,TextField文本框 单行))}
Container:是一个特殊的组件,该组件可以通过add方法添加其他组件进来
布局管理器
容器中的组件的排放方式即布局。
常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列,默认组件都是居中的
Panel默认的布局管理器
BorderLayout(边界布局管理器)
东 南 西 北 中
Frame默认的布局管理器
若未指定方向,会以窗体最大面积填充窗体,添加新组建会覆盖
GridLayout(网络布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
用户对组件的操作就是一个事件。产生事件的组件就是事件源。
接受并处理事件,与用户进行交互的行为就是事件处理器。
这些处理方式都封装在监听器中。
事件监听机制的特点:
1.事件源 :就是awt包或者swing包中的那些图形界面组件
2.事件 :每一个事件源都有自己特有的对应事件和共性事件
3.监听器 :将可以触发某一事件的动作(不止一个动作)都已经封装到了监听器中
4.事件处理
以上三者,在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");
//创建一个标题为my awt的窗体.构造一个最初不可见的Frame新实例.
//默认的是边界式布局管理器
f.setSize(500,100);//(宽,高)
f.setLocation(300,100);//设置本地位置,(宽,高)
f.setLayout(new FlowLayout());//设置布局管理器
Button b = new Button("我是一个按钮");
//创建一个按钮,但此时按钮跟窗体没有联系。
//需要使用容器的add方法加入按钮
//因是边界布局管理器且没指定方向,会全窗体填充
f.add(b);
f.addWindowListener(new MyWin());
/*或者写成匿名内部类
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(Winevent e)
{
System.out.println("window closing");
System.exit(0);
}
}
);
*/
f.setVisible(true);
//setVisible(boolean b);根据参数b的值显示或隐藏此组件
System.out.println("Hello World!");
}
}
/*
class MyWin implements WindowListener
{
//覆盖7个方法,但只用到关闭的动作。
//其他动作都没用到,但却必须要复写。
}
*/
//因WindowListener的子类WindowAdapter已经实现了WindowListener接口,
//并覆盖了其中所有的方法。那么只要继承自WindowAdapter覆盖需要的方法即可。
class MyWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
//WindowEvent 接收窗体事件
{
System.out.println("window closing---"+e.toString());
//e.toString(); 将事件打印出来
System.exit(0);//退出java虚拟机
}
public void windowActivated(WindowEvent e)
{
System.out.println("window Activated--"+e.toString());//窗体置于最前端
}
public void windowOpened(WindowEvent e)
{
System.out.println("window Opened--"+e.toString()); //窗口首次变为可见时调用
}
}
事件中的匿名内部类的方法名开头必须小写
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.setSize(int w,int h);//awt——>Component
//f.setLocation(int x,int y);//移动到指定位置 awt——>Component
f.setBounds(300,100,600,400);//(x,y,w,h)移动并调整大小
f.setLayout(new FlowLayout());//设置布局管理器
but = new Button("Exit");
//将组建添加到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()
{
public void actionPerformed(ActionEvent e)
{
//System.out.println("按钮干的,退出");
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;
private Button butE;
private TextField tf;
MouseAndKeyEvent()
{
init();
}
public void init()
{
f = new Frame("My Frame");
f.setBounds(300,300,500,500);
f.setLayout(new FlowLayout());
tf = new TextField(10); //只能指定列数
but = new Button("Button");
butE = new Button("Esc");
f.add(tf);//添加文本框
f.add(but);
f.add(butE);
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))
//此处的0~9是主键盘中的不是小键盘中的
{
System.out.println(KeyEvent.getKeyText(code)+"..无效");
e.consume();//不按照默认方式(其他字符输入到文本框中)进行。
}
}
}
);
//给But添加一个键盘监听
but.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
System.out.println(e.getKeyChar()+"...."+e.getKeyCode()+
"..."+KeyEvent.getKeyText(e.getKeyCode()));
//打印键与其对应的码
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//Esc对应的码是27
System.exit(0);
}
}
);
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)
{
//System.out.println("点击动作"+clickCount++);
if(e.getClickCount()==2)
System.out.println("双击动作"+clickCount++);
}
}
);
butE.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
}
);
}
public static void main(String[] args)
{
new MouseAndKeyEvent();
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
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,300,500,500);
f.setLayout(new FlowLayout());
tf = new TextField(30);
but = new Button("转到");
ta = new TextArea(15,40);//(行,列)
d = new Dialog(f,"提示信息",true);
//d是一个窗体,不要加到f中。因若加入,f打开d也会打开。
//模式为true,此对话框不操作,其所属窗体操作不了。
//模式若为假,对话框不操作所属窗体也能操作。
d.setBounds(400,200,240,70);
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()
{
/*
d.addWindowListener(new WindowAdapter()
{
public void dialogClosing(WindowEvent e)
{
d.setVisible(false);//只关闭对话框
}
}
);*/
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);//只关闭对话框
}
}
);
tf.addKeyListener(new KeyAdapter() //文本框是事件源
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
showDir();
}
}
}
);
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/*
String text = tf.getText();//返回此文本组件表示的文本
//System.out.println("Text:"+text);
ta.setText(text);
tf.setText("");//将文本框中数据转到文本域后文本框中数据清空*/
String dirPath = tf.getText();
File dir = new File(dirPath);//需封装成文件以便后续判断
if(dir.exists()&&dir.isDirectory()) //必须存在且为目录
{
ta.setText("");//若不清空,多次输入路径后显示的文件都会在。
String[] names = dir.list();
//list();返回一个字符串数组,这些字符串指定此抽象路
//径名表示的目录中的文件和目录
for(String name:names)
{
/*
ta.setText(name+"\r\n");
//文本域中只会出现一个文件,因为会被覆盖。*/
ta.append(name+"\r\n");//追加文本
//将给定文本追加到文本区的当前文本。
}
}
else
{
String info = "输入有误:"+dirPath+"!请重新输入。";
lab.setText(info);
d.setVisible(true);
}
//tf.setText(""); 清空文本框
}
}
);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
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);
}
}
public static void main(String[] args)
{
new MyWindowDemo();
}
}
import java.awt.*;
import java.awt.event.*;
class MyMenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m,subMenu;
private MenuItem closeItem,subItem;
MyMenuDemo()
{
init();
}
public void init()
{
f = new Frame("My WindomMenu");
f.setBounds(200,200,300,300);
f.setLayout(new FlowLayout());
mb = new MenuBar();
m = new Menu("文件");
subMenu = new Menu("子菜单");//菜单有右箭头,条目没有
subItem = new MenuItem("子条目");
closeItem = new MenuItem("退出");
subMenu.add(subItem);
m.add(subMenu);
m.add(closeItem);
mb.add(m);
f.setMenuBar(mb);
MyEvent();
f.setVisible(true);
}
private void MyEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
}
public static void main(String[] args)
{
new MyMenuDemo();
}
}
package MyMenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuTest
{
<span style="white-space:pre"> </span>private Frame f;
<span style="white-space:pre"> </span>private MenuBar mb;
<span style="white-space:pre"> </span>private TextArea ta;
<span style="white-space:pre"> </span>private Menu fileMenu;
<span style="white-space:pre"> </span>private MenuItem openI,saveI,closeI;
<span style="white-space:pre"> </span>private FileDialog openDia,saveDia;
<span style="white-space:pre"> </span>private File file;
<span style="white-space:pre"> </span>MyMenuTest()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>init();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void init()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>f = new Frame("My WindowMenu");
<span style="white-space:pre"> </span>f.setBounds(200,200,300,300);
<span style="white-space:pre"> </span>//f.setLayout(new FlowLayout());
<span style="white-space:pre"> </span>mb = new MenuBar();
<span style="white-space:pre"> </span>ta = new TextArea();
<span style="white-space:pre"> </span>fileMenu = new Menu("文件");
<span style="white-space:pre"> </span>openI = new MenuItem("打开");
<span style="white-space:pre"> </span>saveI = new MenuItem("保存");
<span style="white-space:pre"> </span>closeI = new MenuItem("退出");
<span style="white-space:pre"> </span>mb.add(fileMenu);
<span style="white-space:pre"> </span>fileMenu.add(openI);
<span style="white-space:pre"> </span>fileMenu.add(saveI);
<span style="white-space:pre"> </span>fileMenu.add(closeI);
<span style="white-space:pre"> </span>f.setMenuBar(mb);
<span style="white-space:pre"> </span>openDia = new FileDialog(f,"打开",FileDialog.LOAD);//模式默认为LOAD
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>saveDia = new FileDialog(f,"保存",FileDialog.SAVE);
<span style="white-space:pre"> </span>f.add(ta);
<span style="white-space:pre"> </span>MyEvent();
<span style="white-space:pre"> </span>f.setVisible(true);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>private void MyEvent()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>saveI.addActionListener(new ActionListener()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>public void actionPerformed(ActionEvent e)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(file==null)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>saveDia.setVisible(true);
<span style="white-space:pre"> </span>String dirPath = saveDia.getDirectory();
<span style="white-space:pre"> </span>String fileName = saveDia.getFile();
<span style="white-space:pre"> </span>if(dirPath==null||fileName==null)
<span style="white-space:pre"> </span>return ;
<span style="white-space:pre"> </span>file = new File(dirPath,fileName);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>try
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
<span style="white-space:pre"> </span>String text = ta.getText();
<span style="white-space:pre"> </span>bufw.write(text);
<span style="white-space:pre"> </span>bufw.close();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>catch (IOException ie)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>throw new RuntimeException();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>openI.addActionListener(new ActionListener()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>public void actionPerformed(ActionEvent e)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>openDia.setVisible(true);
<span style="white-space:pre"> </span>String dirPath = openDia.getDirectory();
<span style="white-space:pre"> </span>String fileName = openDia.getFile();
<span style="white-space:pre"> </span>//System.out.println(dirPath+"..."+fileName);//会在控台中打印
<span style="white-space:pre"> </span>//ta.setText(dirPath+"\\"+fileName);
<span style="white-space:pre"> </span>if(dirPath==null||fileName==null)
<span style="white-space:pre"> </span>return ;
<span style="white-space:pre"> </span>ta.setText("");
<span style="white-space:pre"> </span>file = new File(dirPath,fileName);
<span style="white-space:pre"> </span>try
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>BufferedReader bufr = new BufferedReader(new FileReader(file));
<span style="white-space:pre"> </span>String line = null;
<span style="white-space:pre"> </span>while((line=bufr.readLine())!=null)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>ta.append(line+"\r\n");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>catch (IOException i)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>throw new RuntimeException("读取失败!");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);
<span style="white-space:pre"> </span>f.addWindowListener(new WindowAdapter()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>public void windowClosing(WindowEvent e)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>System.exit(0);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);
<span style="white-space:pre"> </span>closeI.addActionListener(new ActionListener()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>public void actionPerformed(ActionEvent e)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>System.exit(0);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void main(String[] args)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>new MyMenuTest();
<span style="white-space:pre"> </span>}
}