/*
* 4、模仿老师上课示例,做一个记事本,实现新建,打开,保存,复制,剪切,粘贴等功能,
* 要有菜单,工具栏,右键菜单,并实现对应功能(要求使用内部类监听或自监听的方式处理事件)。
*/
package com.yema.jre.h20100331;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Notepad extends JFrame implements ActionListener{
//菜单
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("文件");
JMenu m2 = new JMenu("编辑");
JMenu m3 = new JMenu("格式");
JMenu m4 = new JMenu("查看");
JMenu m5 = new JMenu("帮助");
//工具栏
JToolBar tb = new JToolBar();
JButton btnNew = new JButton(new ImageIcon(getClass().getResource("/resource/new.gif")));
JButton btnOpen = new JButton(new ImageIcon(getClass().getResource("/resource/open.gif")));
JButton btnSave = new JButton(new ImageIcon(getClass().getResource("/resource/save.gif")));
JButton btnCopy = new JButton(new ImageIcon(getClass().getResource("/resource/copy.gif")));
JButton btnCut = new JButton(new ImageIcon(getClass().getResource("/resource/cut.gif")));
JButton btnPaste = new JButton(new ImageIcon(getClass().getResource("/resource/paste.gif")));
//文本输入区
JTextArea ta = new JTextArea();
//右键
JPopupMenu right = new JPopupMenu();
JMenuItem iCopy = new JMenuItem("复制");
JMenuItem iCut = new JMenuItem("剪切");
JMenuItem iPaste = new JMenuItem("粘贴");
//状态栏
JLabel status = new JLabel(String.format("%tT", System.currentTimeMillis()),JLabel.RIGHT);
public Notepad(){
super("记事本");
//添加菜单
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
mb.add(m5);
setJMenuBar(mb);
//右键
right.add(iCopy);
right.add(iCut);
right.add(iPaste);
//工具栏
tb.add(btnNew);
tb.add(btnOpen);
tb.add(btnSave);
tb.add(btnCopy);
tb.add(btnCut);
tb.add(btnPaste);
add(tb,BorderLayout.NORTH);
//添加文本框
add(new JScrollPane(ta));
//状态栏
add(status,BorderLayout.SOUTH);
status.setForeground(Color.RED);
//加载外部字体
try {
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("FX-LED002.TTF"));//导入字体
font = font.deriveFont(Font.PLAIN, 20);//deriveFont(int style, float size)通过复制此 Font 对象并应用新样式和大小,创建一个新 Font 对象。
status.setFont(font);
} catch (FontFormatException e2) {
e2.printStackTrace();
} catch (IOException e2){
e2.printStackTrace();
}
status.setBorder(BorderFactory.createEtchedBorder(1));
//自监听事件
btnNew.addActionListener(this);
btnOpen.addActionListener(this);
btnSave.addActionListener(this);
btnCopy.addActionListener(this);
btnCut.addActionListener(this);
btnPaste.addActionListener(this);
// ta.addMouseListener(this);
new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e){
status.setText(String.format("%tT", System.currentTimeMillis()));
}
}).start();//start开始发送事件,stop停止发送事件
setBounds(200, 200, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Notepad();
}
public void mouseactionPerformed(MouseEvent e1){
if (e1.getButton()!=MouseEvent.BUTTON3){
return;
}
if(e1.getClickCount()!=1){
return;
}
right.show(ta,e1.getX(),e1.getY());
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnNew){
}
}
}