------- android培训、java培训、期待与您交流! ----------
创建图形化界面:
1.创建frame窗体。
2.对窗体进行基本设置。比如大小,位置,布局。
3.定义组件。
4.将组件通过窗体的add方法添加到窗体中。
5.让窗体显示,通过setVisible(true);
时间监听机制的特点:1.事件源。2.事件。3.监听器。4.事件处理。
事件源:awt包或者swing包中的那些图形界面组件。
事件:每一个时间源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了。
直接获取其对象来用就可以了。
我们要做的事情是,就是对产生的动作进行处理。
例:windows关闭操作。
import java.awt.*;
import java.awt.event.*;
class Demo
{
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 WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.out.println("关");
System.exit(0);
}
public void windowActivated(WindowEvent e)
{
System.out.println("active");
}
public void windowOpened(WindowEvent e)
{
System.out.println("open");
}
});
f.setVisible(true);
}
}
/*
class MyWin implements WindowListener
{
//需要覆盖7个方法。可我只用到了关闭的动作。
//其他的动作都没有用到,可是却必须复写。
}
*/
//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。
//并覆盖了期中的所有方法。那么我只要继承自WindowAdapter覆盖我需要的方法即可。
class MyWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
// System.out.println("window closing---"+e.toString());
System.exit(0);
}
}
例2:做一个文档实现打开文件和保存文件。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuDemo
{
private Frame f;
private MenuBar bar;
private Menu fileMenu;
private MenuItem closeItem,openItem,saveItem;
private TextArea ta;
private FileDialog openDia,saveDia;
private File file;
MyMenuDemo()
{
init();
}
public void init()
{
f=new Frame("my window");
f.setBounds(300,100,500,600);
// f.setLayout(new FlowLayout());
ta=new TextArea();
bar=new MenuBar();
fileMenu=new Menu("文件");
closeItem=new MenuItem("退出");
openItem=new MenuItem("打开");
saveItem=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.flush();
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");
}
bufr.close();
}
catch(IOException ex)
{
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);
}
});
}
}
class Demo
{
public static void main(String[] args)
{
new MyMenuDemo();
}
}