知识点总结
窗体事件:监听器隶属于awt.event包,class MyListener implements WindowListener(七个方法).
直接实现的话需要覆盖该接口的7个抽象方法,而需要用的方法可能只有一个WindowListener 的子类WindowAdapter(适配器)实现了该接口,并覆盖了所有方法,我们只需继承WindowAdapter类并覆盖我们需要的方法即可.
WindowEvent:即窗体事件,指示窗口状态改变的事件。当打开、关闭、激活、停用、图标化或取消图标化 Window 对象时,或者焦点转移到Window 内或移出 Window 时,由 Window 对象生成此事件.
鼠标事件: Button的父类Component方法void addMouseListener(MouseListener l) 添加鼠标侦听器,使用MouseListener接口及其适配器,进行鼠标事件的侦听.ActionListener事件监听可以对鼠标和键盘事件都做出反馈;MouseListener 鼠标监听只监听鼠标事件.鼠标监听可以根据鼠标事件的动作来决定,例如可以设置每次操作点击鼠标次数来决定反应.
键盘事件:使用Component中方法void addKeyListener(KeyListener l)添加针对键盘事件的监听器,使用KeyListener接口及其适配器,使用KeyEvent控制规则.
记事本部分功能实现
package Lemon;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.EventObject;
public class Note implements ActionListener{
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu;
JMenuItem newItem,openItem,saveItem,closeItem,exitItem;
ImageIcon newIcon,openIcon,saveIcon,closeIcon;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser;
File file;
private JMenuItem item;
public Note(){
frame = new JFrame("记事本");
bar = new JMenuBar();
fileMenu = new JMenu("文件");
editMenu = new JMenu("编辑");
formatMenu = new JMenu("格式");
viewMenu=new JMenu("查找");
helpMenu=new JMenu("帮助");
area =new JTextArea();
scroll = new JScrollPane(area);
newItem = new JMenuItem("新建(N)",newIcon);
openItem = new JMenuItem("打开(O)",openIcon);
saveItem = new JMenuItem("另存为(s)",saveIcon);
closeItem = new JMenuItem("关闭(C)",closeIcon);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
newItem.setMnemonic(KeyEvent.VK_N);
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,java.awt.Event.CTRL_MASK));
openItem.setMnemonic(KeyEvent.VK_O);
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,java.awt.Event.CTRL_MASK));
saveItem.setMnemonic(KeyEvent.VK_S);
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,java.awt.Event.CTRL_MASK));
closeItem.setMnemonic(KeyEvent.VK_C);
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,java.awt.Event.CTRL_MASK));
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(viewMenu);
bar.add(helpMenu);
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setSize(800,500);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if(obj instanceof JMenuItem){
JMenuItem item = (JMenuItem) obj;
if(item == newItem){
new Note();
}
}else if(item == openItem){
chooser = new JFileChooser();
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
try{
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[fis.available()];
fis.read(b);
area.append(new String(b));
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}else if(item == closeItem){
System.exit(1);
}
}
package Lemon;
public class Test {
public static void main(String[] args) {
Note note = new Note();
}
}