黑马程序员 GUI(图形用户界面)
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
1.概述
GUI
(1)Graphical User Interface(图形用户接口)
(2)用图形的方式,来显示计算机操作的界面,这样更方便直观
CLI
(1)Command line User Interface(命令行用户接口)
(2)就是常见的Dos命令行操作
(3)需要记忆一些常用的命令行,操作不直观。
2.java为GUI提供的对象都存在java.Awt和javax.Swing两个包中。
3.Awt
java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属于重量级控件。
4.javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由java实现。增强了移植性,属轻量级控件。
5.
Container:为容器,是一个特殊的组件,该组件中可通过add方法添加其他组件进来。
6.布局管理器
(1)常见的组件的排放方式,就是布局
(2)常见的布局管理器:
(a)FlowLayout(流式布局管理器):从左到右的顺序排列;Panel默认的布局管理器。
(b)BorderLayout(边界布局管理器):东,南,西,北,中;Frame默认的布局管理器。
(c)GridLayout(网格布局管理器):规则的矩阵
(d)CardLayout(卡片布局管理器):选项卡
(e)GridBagLayout:(网格布局管理器):非规则的矩阵。
7.Frame
/*创建图形化界面
1.创建frame窗体
2.对窗体进行基本设置比如大小,位置,布局
3.将组件通过窗体的add方法添加到窗体中
4.让窗体显示,通过setVisible(true)*/
import java.awt.*;
class AwtDemo
{
public static void main(String[] args)
{
Frame f=new Frame("my awt");//创建frame窗体
f.setSize(500,400);//设置窗体大小
f.setLocation(300,200);//设置窗体位置
f.setLayout(new FlowLayout());//设置边界布局管理器
Button b=new Button("按钮");//创建按钮
f.add(b);//添加按钮组件
f.setVisible(true);//显示窗体
}
}
8.事件监听机制的特点:
(1)事件源:就是awt包或者swing包中的那些图形界面组件。
(2)事件:每一个事件源都有自己特有的对应事件和共性事件。
(3)监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
(4)事件处理:以上三者,在java中都已经定义好了,直接获取对象来使用就可以,然而要做的是对产生的动作进行处理。
9.
//窗体事件,Action事件,鼠标事件和键盘事件运用
import java.awt.event.*;
import java.awt.*;
class WindowAdapterDemo
{ private Frame f;
private Button but;
private TextField tf;
public static void main(String[] args)
{
new WindowAdapterDemo();
}
//建立构造函数,使该函数一初始化就出现图形化界面
WindowAdapterDemo()
{
init();
}
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);
}
});
*/
//添加活动监听器
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++);
else
System.out.println("点击动作"+clickCount++);
}
});
//添加键盘监听器,事件源是按钮
but.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
/*根据哈希值获得键盘对应该的字符串
e.getKeyChar()不能获取shift,enter等等键,
所以用KeyEvent.getKeyText(e.getKeyCode())*/
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"......"+e.getKeyCode());
//按下ESC退出
if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
System.exit(0);
//组合键
if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ESCAPE)
System.out.println("ctrl+enter is run");
}
});
//添加键盘监听器,事件源是文本框
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
//只能输入0-9的数,否则是非法的
if(!(e.getKeyCode()>=KeyEvent.VK_0 && e.getKeyCode()<=KeyEvent.VK_9))
{
System.out.println(e.getKeyCode()+"......是非法的");
e.consume();
}
}
});
}
public void init()
{
f=new Frame("my frame");
f.setBounds(300,100,600,500);//对frame进行基本设置
f.setLayout(new FlowLayout());
tf=new TextField(20);//创建文本框
but=new Button("my button");//创建按钮组件
f.add(but);//添加按钮到窗体中
f.add(tf);//添加文本框到窗体中
myEvent();
f.setVisible(true);//显示窗体
}
}
10.
//练习:列出指定目录内容
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindow
{
private Frame f;//定义窗体变量
private TextField tf;//定义文本框变量
private Button but;//定义按钮变量
private TextArea ta;//定义文本区域变量
private Dialog d;//定义对话框变量
private Label lab;//定义标签变量
private Button okBut;
MyWindow()
{
init();
}
public void init()
{
f=new Frame("My Window");//创建窗体对象
f.setBounds(300,100,600,500);//对窗体设置位置和大小
f.setLayout(new FlowLayout());//设置流式布局管理器
tf=new TextField(60);//创建文本框并设置长度
but=new Button("转到");//创建按钮
ta=new TextArea(25,70);//创建文本区域并设置大小
d=new Dialog(f,"提示信息-self",true);//创建对话框对象
d.setBounds(400,200,260,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);//在窗体显示组件
}
public void myEvent()
{
/*添加活动监听器,事件源是按钮
点击"转到"按钮,显示目录内容*/
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();
}
});
/*添加活动监听器,事件源是按钮
点击"确定"按钮,将对话框关闭*/
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
/*添加窗体监听器,事件源是对话框
点击对话框关闭图标,将对话框关闭*/
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
//添加窗体监听器,可以关闭窗体
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public 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 into="你输入的信息:"+dirPath+"是错误的,请重新输入!";
lab.setText(into);//添加标签
d.setVisible(true);
}
}
public static void main(String[] args)
{
new MyWindow();
}
}
11.
//练习2:创建菜单栏,菜单中有打开文件,保存文件和退出功能
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuTest
{
private Frame f;//定义窗体变量
private MenuBar bar;//定义菜单栏变量
private Menu fileMenu;//定义菜单变量
private MenuItem openItem,saveItem,closeItem;//定义菜单项变量
private FileDialog openDia,saveDia;//定义对话框变量
private TextArea ta;//定义文本区域变量
private File file;//定义文本变量
MyMenuTest()
{
init();
}
public void init()
{
f=new Frame("my window");//创建窗体对象
f.setBounds(300,100,650,600);//设置窗体位置和大小
bar=new MenuBar();//创建菜单栏对象
ta=new TextArea();//创建文本区域对象
fileMenu=new Menu("文件");//创建菜单对象
//创建菜单项对象
openItem=new MenuItem("打开");
saveItem=new MenuItem("保存");
closeItem=new MenuItem("退出");
//将菜单项添加到菜单中
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
bar.add(fileMenu);//将菜单添加到菜单栏中
f.setMenuBar(bar);//将菜单栏添加到窗体中
//创建打开和保存对话框对象
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.close();//关闭流
}
catch (IOException ex)
{
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=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");//追加文本,将读取的数据写到文本区域中
}
}
catch (IOException ex)
{
throw new RuntimeException("读取失败");
}
}
});
//添加窗体监听器
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 MyMenuTest();
}
}