——- android培训、java培训、期待与您交流! ———-
一、 GUI
1、 知道GUI是一个图形用户界面,操作起来方便、直观。
2、 Java为GUI提供的对象都存在java.awt(重量级控件)和java.swing(轻量级控件)这两个包中。
3、 了解继承关系:
二、 布局管理器
重点内容
1、 布局就是面板上组件的排版方式。
2、 常见的布局管理器有五种:
1) FlowLayout(流式布局管理器):面板Panel默认的布局管理器
顺序:从左到右
2) BorderLayout(边界布局管理器):Frame默认的布局管理器
顺序:东南西北中
3) GridLayout(网格布局管理器):类似计算器按钮
顺序:规则的矩阵
4) CardLayout(卡片布局管理器):类似属性框下的各个选项卡
顺序:选项卡
5) GridBagLayout(网格包布局管理器)
顺序:非规则的矩阵
三、 Frame框架
创建窗体的步骤:
1、 创建Frame窗体
2、 对窗体进行基本设置(大小、位置、布局等)
3、 定义组件
4、 将组件通过窗体的add方法添加到窗体中
5、 让窗体显示,通过setVisible(true)方法
示例演示:
package guilianxi;
import java.awt.*;
import java.awt.event.*;
class FrameDemo
{
public static void main(String[] args)
{
Frame f=new Frame("我的窗口");
f.setVisible(true); //设置窗体可见性
f.setSize(500,400); //设置窗体的宽度和高度
f.setLocation(300,200); //设置窗体的位置
f.setLayout(new FlowLayout()); //设置布局
Button b=new Button("我是一个按钮");
f.add(b); //往窗体里添加一个组件
//f.addWindowListener(new Mywin());//关闭窗体
f.addWindowListener(new WindowAdapter() //用匿名内部类的方法代替下面新建类Mywin
{
public void windowClosing(WindowEvent e)
{
System.out.println("window closing");
System.exit(0);
}
});
}
}
/**
class Mywin extends WindowAdapter //因为WindowListener的子类WindowAdapter已经实现了WindowListener接口,
//并覆盖了其中所有的方法,那么我们只要继承自WindowAdapter,覆盖我需要的方法即可。
{
public void windowClosing(WindowEvent e)
{
System.out.println("window closing");
System.exit(0);
}
}
*/
四、 事件监听机制
1、 组成:事件源、事件、监听器、事件处理(引发事件后处理方式)
2、 流程图:
事件源:GUI的组件
事件:事件源对应的事件
监听器:封装触发事件的动作到监听器
3、 窗体事件:
1) addWindowListener(WindowListener ());添加指定的窗口监听器,用来接收窗口事件。
2) WindowListener接口:用于接收窗口事件的侦听器接口。
总结:处理窗口事件的类,要么实现此接口(及其包含的所有方法)
要么继承抽象类WindowAdapter(仅重写所需的方法即可)
3) WindowAdapter类:接收窗口事件的抽象适配器类。用于方便创建侦听器对象。继承此类可以创建WindowEvent侦听器并为所需事件重写该方法。(如果实现WindowListener接口,则必须定义该接口内的所有方法;而此抽象类将所有方法都定义为null,只需针对关心的事件定义方法即可)
4、 注意:WindowListener接口是java.awt.event包中的,所以包头处要记得导入包java.awt.event.*;
五、 Action事件(动作事件)
示例演示:在窗体中添加一个按钮,让按钮具备关闭窗体的功能
package guilianxi;
import java.awt.*;
import java.awt.event.*;
public class Action {
public static void main(String[] args) {
new FrameDemo1();
}
}
class FrameDemo1
{
private Frame f;
private Button b;
FrameDemo1()
{
method();
}
public void method()
{
f=new Frame("我的窗体");//创建一个窗体
f.setBounds(300,200,600,500);//设置窗体的位置(横纵坐标)、宽度和高度
f.setLayout(new FlowLayout());//设置流式布局管理器
b=new Button("我的按钮");
f.add(b);//将按钮添加到窗体中
myEvent();//加载窗体上的事件
f.setVisible(true);//显示窗体
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});//用匿名内部类的方法实现窗体的关闭功能
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}); //用匿名内部类的方法让按钮具备退出程序的功能
}
}
总结:上例可以发现按钮就是一个事件源,鼠标一点击按钮时,就会触发它的退出程序的事件。通过查阅API中对Button的描述,发现按钮支持一个特有监听器,addActionListener。它没有适配器,因为它里面只有一个方法,actionPerformed。Button是java中少数没有适配器的监听器之一。
六、 鼠标事件
1、 常用的鼠标方法:
1) mouseClicked(MouseEvent e):鼠标在组件上单击(按下并释放)时调用
2) mouseEntered(MouseEvent e):鼠标进入到组件上时调用
3) mouseExited(MouseEvent e):鼠标离开组件上时调用
4) mousePressed(MouseEvent e):鼠标在组件上按下时调用
5) mouseReleased(MouseEvent e):鼠标在组件上释放时调用
6) int getClickCount();返回与此事件关联的鼠标单击次数
2、 接上面例子,给按钮添加一个鼠标事件
代码如下:
b.addMouseListener(new MouseAdapter()
{
private int clickCount=1;
private int count=1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入到该组件次数:"+count++);
}
public void mouseClicked(MouseEvent e)
{
System.out.println("鼠标单击该组件次数:"+clickCount++);
}
});
七、 键盘事件
1、 常用的键盘方法:
1) keyPressed(KeyEvent e); 按下某个键时调用此方法
2) getKeyChar(); 返回与此事件中的键关联的字符
3) int getKeyCode(); 返回与此事件中的键关联的整数keyCode
4) static String getKeyText(int keyCode) 返回描述keyCode的文本
5) boolean isControlDown(); 判断control键是否是按下
2、 接上面例子,给按钮添加一个键盘事件
代码如下:
b.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
System.out.println(e.getKeyChar()+"..."+e.getKeyCode()); //返回从键盘按下的键对应的字符的数值
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());//显示出键和键对应的字符数值
if(e.getKeyCode()==KeyEvent.VK_ENTER) //如果键的字符数值和enter的字符数值相同就退出程序
System.exit(0);
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//同时按下ctrl+enter键
System.out.println("ctrl+enter is running");
}
});
3、 扩展:在文本框上添加一个键盘监听
package guilianxi;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 需求:在文本框上添加一个键盘监听
* @author ling
*
*/
public class TextFiled {
public static void main(String[] args) {
new FrameDemo2();
}
}
class FrameDemo2
{
private Frame f;
private TextField tf;
FrameDemo2()
{
method();
}
public void method()
{
f=new Frame("我的第二个窗口");
f.setBounds(300,100,500,600);
f.setLayout(new FlowLayout());
tf=new TextField(20);// 设置文本框的长度,即列数
f.add(tf);
myEvent();
f.setVisible(true);
}
public 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();
}
}
});
}
}
4、 需求:在文本框中输入目录,点击“转到”按钮,将该目录的文件和文件夹名称列在下面的文本区域里。
package guilianxi;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
/** 需求:在文本框中输入目录,点击“转到”按钮,将该目录的文件和文件夹称列在下面的文本区域里
* @param args
*/
public class TextAreaDemo {
public static void main(String[] args) {
new FrameDemo3();
}
}
class FrameDemo3
{
private Frame f;
private TextField tf;
private Button bon;
private TextArea ta;
FrameDemo3()
{
f=new Frame();
f.setBounds(300,100,500,600);
f.setLayout(new FlowLayout());
tf=new TextField(60); //设置文本的列数
bon=new Button("转到");
ta=new TextArea(25,75);//设置文本区域的行和列的长度
f.add(tf);
f.add(bon);
f.add(ta);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter() //窗体的关闭窗口的功能
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//因为文本框中的内容转到文本区域中,这个事件是在出发按钮时产生的,所以事件源是按钮
bon.addActionListener(new ActionListener()//将文本框中的文本内容转到文本区域
{
public void actionPerformed(ActionEvent e)
{
String text=tf.getText();
ta.setText(text); //将文本框中的内容传到文本区域中
tf.setText(""); //清空文本框中的内容
}
});
bon.addActionListener(new ActionListener() //保证文本框中输入的是目录
{
public void actionPerformed(ActionEvent e)
{
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");//将目录中的名称添加到文本区域
}
}
}
});
}
}
八、 对话框 Dialog
1、 加入对话框,可以优化代码,是界面看起来更美观
2、 例:将上面的例子中加入功能:如果输入的目录不存在,则弹出提示错误信息的对话框。并且输入目录后,不按“转到”按钮,直接敲回车,也可以将结果显示出来。
分步来完成:
1)在上个例子的基础上,定义一个对话框窗体,一个label标签,和一个确定按钮
private Dialog d;
private Label lab;
private Button okBon;
2)在构造方法中添加如下代码:
d=new Dialog(f,"提示信息",true);
d.setBounds(400,200,240,150);//设置错误对话框的大小
d.setLayout(new FlowLayout());
lab=new Label(); //设置错误信息显示的地方
okBon=new Button("确定");
d.add(lab);
d.add(okBon);
3)在myEvent()方法中添加如下事件
d.addWindowListener(new WindowAdapter() //错误对话框也是一个窗体
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false); //设置一开始错误对话框是不可见的
}
});
okBon.addActionListener(new ActionListener()//okBon是一个按钮,所以也要添加按钮监听器
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter() //实现在文本框中输入完目录后,敲回车键显示结果的功能
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
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);
}
}
}
});
4)在保证文本框中输入的是目录的按钮事件方法中添加一个else语句
else
{
String info="您输入的信息:"+dirPath+"是错误的!";
lab.setText(info);
d.setVisible(true);
}
九、 菜单(工具栏上的菜单)
1、 类MenuBar:菜单栏
类MenuBar封装绑定到框架的菜单栏的平台概念
方法:MenuBar中有一个方法可以将指定的菜单添加到菜单栏中。
注:为了将该菜单栏与Frame对象关联,可以调用该框架的setMenuBar方法
2、 类Menu:菜单
方法:MenuItem add(MenuItem mi):将指定的菜单添加到此菜单(即菜单下添加子菜单、子项目)
3、 类MenuItem ,它支持一个addActionListener(ActionListener),活动监听,添加指定的动作监听器,以从此菜单项接收动作事件
4、 示例演示:在一个窗体上,建一个菜单栏
package guilianxi;
import java.awt.*;
import java.awt.event.*;
/**需求:在一个窗体上,建一个菜单栏,并且单击菜单中的退出项时,程序结束
* @param args
*/
public class MyMenuDemo {
public static void main(String[] args)
{
new MyMenu();
}
}
class MyMenu
{
private Frame f;
private MenuBar mb;
private Menu m,subMenu;
private MenuItem closeItem,subItem;
MyMenu()
{
f=new Frame("我的窗口");
f.setBounds(300,100,500,600);
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);//将菜单栏与Frame相关联
MyEvent();
f.setVisible(true);
}
public 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);
}
});
}
}
十、 菜单栏(打开文件和保存文件)
1、 文件对话框:FileDialog
FileDialog类显示一个文件对话框窗口,用户可以从中选择文件
2、 FileDialog(Frame parent , String title, int mode):创建一个具有指定标题的文件对话框窗口,用于加载(打开)和保存文件。
mode—–对话框的模式,可以是FileDialog.LOAD (默认)或FileDialog.SAVE
3、 FileDialog中的方法:
1) getDirectory():获取此文件对话框的目录
2) getFile():获取文件对话框中的选定文件
4、 示例演示:实现从对话框中选定文件并打开,显示内容的功能和保存文件的功能
package guilianxi;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**需求:实现从对话框中选定文件并打开,显示内容的功能和保存文件的功能
* @param args
*/
public class MyFileDialog {
public static void main(String[] args) {
new MyFileDialog1();
}
}
class MyFileDialog1
{
private Frame f;
private MenuBar mb;
private Menu fileMenu;
private MenuItem openItem,saveItem,closeItem;
private TextArea ta;
private FileDialog openDia,saveDia;
private File file;
MyFileDialog1()
{
init();
}
public void init()
{
f=new Frame("我的窗口");
f.setBounds(300,100,500,600);
f.setLayout(new FlowLayout());
mb=new MenuBar(); //新建一个菜单栏
ta=new TextArea();
fileMenu=new Menu("文件"); //新建菜单栏上的文件菜单
saveItem=new MenuItem("保存");
closeItem=new MenuItem("打开");
openItem=new MenuItem("退出");
fileMenu.add(saveItem);
fileMenu.add(closeItem);
fileMenu.add(openItem);
mb.add(fileMenu);
f.setMenuBar(mb);
openDia=new FileDialog(f,"我要打开",FileDialog.LOAD);
saveDia=new FileDialog(f,"我要保存",FileDialog.SAVE);
f.add(ta);
MyEvent();
f.setVisible(true);
}
public 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);
}
});
openItem.addActionListener(new ActionListener()//打开文件的功能
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true); //打开文件对话框
String dirPath=openDia.getDirectory();
String fileName=openDia.getFile();
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");
bufr.close();
}
}
catch(IOException ex)
{
throw new RuntimeException("读取文件失败");
}
}
});
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 ex)
{
throw new RuntimeException("保存文件失败");
}
}
}
});
}
}