package another;
//引入包
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.io.*;
public class TextFilter extends JFrame implements ActionListener{
//声明成员变量
private File file; //文件对象,用来表示当前文件
private JTextArea text; //文本域
private JFileChooser fchooser; //选择文件对话框
private boolean b = false; //用来标记原来是不是已经有了文本
private JComboBox checkFont, checkNumber; //这是字体和字号的复选框
private JCheckBox checkbold,checkstyle;//粗体斜体选择框
//声明构造方法
public TextFilter()
{
super("文本文件编辑器");
this.setBounds(400, 400, 500, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text = new JTextArea(); //实例化文本域
this.getContentPane().add(new JScrollPane(this.text)); //对文本域添加滚动条,并且将滚动条对象添加到文本文件编辑器
JMenuBar menubar = new JMenuBar(); //创建菜单栏对象
this.setJMenuBar(menubar); //为文本编辑器窗口添加菜单栏
String menustr[] = {"文件(F)","编辑(E)","插入(C)","格式(O)","工具","帮助(H)"}; //这是菜单栏上的菜单选项
JMenu menu[] = new JMenu[menustr.length]; //菜单选项
for(int i = 0 ; i < menu.length ; i++)
{
menu[i] = new JMenu(menustr[i]); //实例化菜单选项
menubar.add(menu[i]); //将菜单选项添加入菜单栏
}
String menuitemstr[] = {"新建(N)","打开(O)","保存(S)","另存为(Q)","退出(X)"};
JMenuItem menuitem[] = new JMenuItem[menuitemstr.length];
for(int i = 0 ; i < menuitem.length ; i++ )
{
menuitem[i] = new JMenuItem(menuitemstr[i]);
menu[0].add(menuitem[i]);
menuitem[i].addActionListener(this);
if(i == 3)
menu[0].addSeparator();
}
//添加快捷键
menuitem[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
menuitem[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
menuitem[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
menuitem[3].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));
menuitem[4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.ALT_MASK));
String menuitemstr1[] = {"复制(C)","粘贴(V)","剪切(X)"};
JMenuItem menuitem1[] = new JMenuItem[menuitemstr1.length];
for(int i = 0 ; i < menuitem1.length ; i++)
{
menuitem1[i] = new JMenuItem(menuitemstr1[i]);
menu[1].add(menuitem1[i]);
menuitem1[i].addActionListener(this);
}
//添加快捷键
menuitem1[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
menuitem1[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
menuitem1[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
//添加颜色功能
String color = "颜色";
JMenuItem menuitem2 = new JMenuItem(color);
menu[3].add(menuitem2);
menuitem2.addActionListener(this);
String insertstr[] = {"图片","文件"};
JMenuItem insert[] = new JMenuItem[insertstr.length];
for(int i = 0;i<insert.length;i++)
{
insert[i] = new JMenuItem(insertstr[i]);
menu[2].add(insert[i]);
}
String helpstr[] = {"查看帮助","有关记事本"};