/**
*文本编辑器-记事本
*文件菜单--打开,保存,清除,退出,
*编辑菜单--复制,粘贴,剪切,全选
*字体--字体大小改变,颜色改变,字体改变,
*帮助文本
*
*缺憾----打开和保存文件对话框没有文件过滤功能
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Jishiben extends JFrame implements ActionListener
{
JTextArea text;
public Jishiben()
{
super("简单的文本编辑器");
JMenuBar menubar=new JMenuBar();
//菜单
JMenu textMenu=new JMenu("文件");
JMenu editMenu=new JMenu("编辑");
JMenu fontMenu=new JMenu("字体");
JMenu colorMenu=new JMenu("颜色");
JMenu sizeMenu=new JMenu("大小");
JMenu helpMenu=new JMenu("帮助");
//菜单选项
JMenuItem open=new JMenuItem("打开");
JMenuItem save=new JMenuItem("保存");
JMenuItem clear=new JMenuItem("清除");
JMenuItem exit=new JMenuItem("退出");
JMenuItem copy=new JMenuItem("复制");
JMenuItem paste=new JMenuItem("粘贴");
JMenuItem cut=new JMenuItem("剪切");
JMenuItem selectAll=new JMenuItem("全选");
JCheckBoxMenuItem formal=new JCheckBoxMenuItem("正常");
JCheckBoxMenuItem big=new JCheckBoxMenuItem("粗体");
JCheckBoxMenuItem xie=new JCheckBoxMenuItem("斜体");
ButtonGroup buttongroup=new ButtonGroup();
JRadioButtonMenuItem red=new JRadioButtonMenuItem("红");
JRadioButtonMenuItem green=new JRadioButtonMenuItem("绿");
JRadioButtonMenuItem blue=new JRadioButtonMenuItem("蓝");
buttongroup.add(red);
buttongroup.add(green);
buttongroup.add(blue);
ButtonGroup sizegroup=new ButtonGroup();
JRadioButtonMenuItem sizeone=new JRadioButtonMenuItem("12");
JRadioButtonMenuItem sizetwo=new JRadioButtonMenuItem("24");
JRadioButtonMenuItem sizethree=new JRadioButtonMenuItem("36");
sizegroup.add(sizeone);
sizegroup.add(sizetwo);
sizegroup.add(sizethree);
JMenuItem help=new JMenuItem("查看");
//添加菜单,菜单项,构成整个菜单条
textMenu.add(open);
textMenu.add(save);
textMenu.add(clear);
textMenu.add(exit);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(cut);
editMenu.add(selectAll);
fontMenu.add(formal);
fontMenu.add(big);
fontMenu.add(xie);
colorMenu.add(red);
colorMenu.add(green);
colorMenu.add(blue);
sizeMenu.add(sizeone);
sizeMenu.add(sizetwo);
sizeMenu.add(sizethree);
helpMenu.add(help);
menubar.add(textMenu);
menubar.add(editMenu);
menubar.add(fontMenu);
menubar.add(colorMenu);
menubar.add(sizeMenu);
menubar.add(helpMenu);
this.setJMenuBar(menubar);
//添加文本区域
text=new JTextArea(15,10);
this.add(text);
this.setSize(500,400);
this.setLocation(400,200);
this.setVisible(true);
//添加事件监听
this.addWindowListener(new WindowListenerAction());
open.addActionListener(this);
save.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
cut.addActionListener(this);
selectAll.addActionListener(this);
formal.addActionListener(this);
big.addActionListener(this);
xie.addActionListener(this);
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
sizeone.addActionListener(this);
sizetwo.addActionListener(this);
sizethree.addActionListener(this);
}
//事件处理方法
public void actionPerformed(ActionEvent e)
{
try
{
String command=e.getActionCommand();
if(command=="打开")
{
FileDialog fopen=new FileDialog(this,"打开",FileDialog.LOAD);
fopen.setFilenameFilter(new MyFileFilter());
fopen.setVisible(true);
String url=fopen.getDirectory()+fopen.getFile();
FileReader fr=new FileReader(url);
BufferedReader bf=new BufferedReader(fr);
String str=bf.readLine();
while(str!=null)
{
this.text.append("\n"+str);
str=bf.readLine();
}
fr.close();
bf.close();
}
else if(command=="保存")
{
FileDialog fsave=new FileDialog(this,"保存",FileDialog.SAVE);
fsave.setFilenameFilter(new MyFileFilter());
fsave.setVisible(true);
String url=fsave.getDirectory()+fsave.getFile();;
FileWriter fw=new FileWriter(url);
PrintWriter pw=new PrintWriter(fw);
String str=this.text.getText();
pw.print(str);
fw.flush();
fw.close();
pw.flush();
pw.close();
}
else if(command=="清除")
{
this.text.setText(" ");
}
else if(command=="退出")
{
System.exit(0);
}
else if(command=="全选")
{
this.text.selectAll();
}
else if(command=="复制")
{
this.text.copy();
}
else if(command=="粘贴")
{
this.text.paste();
}
else if(command=="剪切")
{
this.text.cut();
}
else if(command=="红"||command=="蓝"||command=="绿")
{
}
else if(command=="12"||command=="24"||command=="36")
{
Font font=this.text.getFont();
this.text.setFont(new Font(font.getName(),font.getStyle(),Integer.parseInt(command)));
}
else if(command=="正常"||command=="粗体"||command=="斜体")
{
}
}
catch (Exception ee)
{
System.out.println("在事件处理时出现了异常!!!");
}
}
//内部窗体事件处理类
class WindowListenerAction extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
/**
*
*实现文件过滤
*
*/
class MyFileFilter implements FilenameFilter
{
public boolean accept(File dir,String name)
{
if(name.endsWith(".JAVA"))
{
return true;
}
else if(name.endsWith(".txt"))
{
return true;
}
else if(name.endsWith(".CLASS"))
{
return true;
}
else
{
return false;
}
}
}
public static void main(String args[])
{
try
{
new Jishiben();
}
catch (Exception e)
{
System.out.println("Meet Exception in here!!");
}
}
}