package peng_jun; import java.awt.*;
import java.awt.event.*; import javax.swing.*; import java.io.*; import javax.swing.filechooser.*; import java.awt.datatransfer.*; public class Text4 extends JFrame {
public static void main(String[] args) throws Exception {
new Text4();
} JTextArea area = new JTextArea();;// 文本域
String name = null;// 文件名
String board = null;
Clipboard cb;// 系统粘贴板 此类实现一种使用剪切/复制/粘贴操作传输数据的机制 JMenu font = new JMenu("字体");
JMenuItem bold = new JMenuItem("加粗");
JMenuItem italics = new JMenuItem("斜体");
JMenu size = new JMenu("字号");
JMenuItem size_18 = new JMenuItem("");
JMenuItem size_19 = new JMenuItem("");
JMenuItem size_20 = new JMenuItem("");
JMenuItem size_21 = new JMenuItem("");
JMenu color = new JMenu("颜色");
JMenuItem red = new JMenuItem("红色");
JMenuItem blue = new JMenuItem("蓝色"); JMenu file = new JMenu("文件");// 定义“文件”菜单项
JMenuItem rebuild = new JMenuItem("新建");
JMenuItem save = new JMenuItem("保存");
JMenuItem open = new JMenuItem("打开");
JMenuItem anotherSave = new JMenuItem("另存为");
JMenuItem clear = new JMenuItem("清除");
JMenuItem close = new JMenuItem("退出"); JMenu edit = new JMenu("编辑");// 定义“编辑”菜单项
JMenuItem cut = new JMenuItem("剪切");
JMenuItem copy = new JMenuItem("复制");
JMenuItem plaster = new JMenuItem("粘贴");
JMenuItem all = new JMenuItem("全选");
JMenuItem replice = new JMenuItem("替换");
JMenuItem check = new JMenuItem("查找"); JMenu help = new JMenu("帮助");// 定义“帮助”菜单项
JMenuItem about = new JMenuItem("关于"); Toolkit kit = Toolkit.getDefaultToolkit();// Toolkit获得本机系统的屏幕的参数
JMenuBar jmb = new JMenuBar();// 创建菜单条 Text4() throws Exception {// 构造函数
setTitle("记事本"); // 设置快捷键
bold.setAccelerator(KeyStroke.getKeyStroke("ctrl B"));
italics.setAccelerator(KeyStroke.getKeyStroke("ctrl I"));
rebuild.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
save.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
copy.setAccelerator(KeyStroke.getKeyStroke("ctrl C"));
open.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
cut.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));
plaster.setAccelerator(KeyStroke.getKeyStroke("ctrl V"));
all.setAccelerator(KeyStroke.getKeyStroke("ctrl A"));
replice.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));
check.setAccelerator(KeyStroke.getKeyStroke("ctrl F")); add(jmb);
setJMenuBar(jmb);// 设置菜单条
add(area);
file.add(rebuild);
file.add(save);
file.add(open);
file.add(anotherSave);
file.addSeparator();
file.add(clear);
file.add(close);
// JScrollPane scroldPane=new JScrollPane(area); //设置滚动轴
// add(scroldPane); edit.add(cut);
edit.add(copy);
edit.add(plaster);
edit.add(all);
edit.addSeparator();
edit.add(replice);
edit.add(check); font.add(bold);
font.add(italics);
size.add(size_18);
size.add(size_19);
size.add(size_20);
size.add(size_21);
color.add(red);
color.add(blue);
font.add(size);
font.add(color); help.add(about);
// 添加事件
clear.addActionListener(new HandleAct());
close.addActionListener(new HandleAct());
bold.addActionListener(new HandleAct());
italics.addActionListener(new HandleAct());
size_18.addActionListener(new HandleAct());
size_19.addActionListener(new HandleAct());
size_20.addActionListener(new HandleAct());
size_21.addActionListener(new HandleAct());
red.addActionListener(new HandleAct());
blue.addActionListener(new HandleAct());
replice.addActionListener(new HandleAct());
check.addActionListener(new HandleAct()); jmb.add(file);
jmb.add(edit);
jmb.add(font);
jmb.add(help);
setSize(, );// 大小信息
// setLocation(300, 200);// 文本框位置
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cb = Toolkit.getDefaultToolkit().getSystemClipboard();// 调用系统剪贴板
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
saveText();
} catch (Exception ex) {
}
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
openText();
} catch (Exception ex) {
}
}
});
rebuild.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
area.setText("");
name = null;
} catch (Exception ex) {
}
}
});
anotherSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
anotherSaveText();
}
}); size_18.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Font oldF = area.getFont();// 获取当前字体
String name = oldF.getName();// 获取当前字体的名字
area.setFont(new Font(name, oldF.getStyle(), ));
}
}); cut.addActionListener(new ActionListener() // 剪切
{
public void actionPerformed(ActionEvent e) {
board = area.getSelectedText();
cb.setContents(new StringSelection(board), null);
area.replaceRange("", area.getSelectionStart(),
area.getSelectionEnd());
}
});
copy.addActionListener(new ActionListener() // 复制
{
public void actionPerformed(ActionEvent e) {
board = area.getSelectedText();
cb.setContents(new StringSelection(board), null);
}
});
plaster.addActionListener(new ActionListener() // 粘贴
{
public void actionPerformed(ActionEvent e) {
try {
Transferable content = cb.getContents(null);
String st = (String) content
.getTransferData(DataFlavor.stringFlavor);
area.replaceRange(st, area.getSelectionStart(),
area.getSelectionEnd());
} catch (Exception ex) {
}
}
});
all.addActionListener(new ActionListener() // 全选
{
public void actionPerformed(ActionEvent e) {
area.setSelectionStart();
area.setSelectionEnd(area.getText().length());
}
}); about.addActionListener(new ActionListener() {// 帮助
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame("关于");
frame.setSize(, );
frame.setLocation(, );
JTextArea area1 = new JTextArea("java简易文本编辑器开发人员:" + '\n'
+ "彭军军 201321091103," + '\n' + "鲍静瑶201321091080,"
+ '\n' + "刘瑾 201321091081.");
frame.add(area1);
frame.setVisible(true);
}
});
} public void openText() // 打开
{
/*
* 在选择文件方法中,首先创建JFileChooser文件选择器,
* 使用JFileChooser类的setFileFilter()方法创建文件扩展名过滤器,再使用JFileChooser类
* 的setFileSelectionMode()方法设置文件选择模式为文件,
* 通过JFileChooser类的showOpenDialog()方法显示文件打开对话框,确定用户按下打开按钮,
* 而非取消按钮后,通过JFileChooser类的getSelectedFile()方法获取用户选择的文件对象,
* 使用JTextField类的setText()方法显示文件信息到文本框。
*/
JFileChooser chooser = new JFileChooser();//创建JFileChooser文件选择器
FileNameExtensionFilter filter = new FileNameExtensionFilter("Files",
"txt", "java","html","css","js");//文件过滤器
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result = chooser.showOpenDialog(Text4.this);//打开当前文件夹
if (result == JFileChooser.APPROVE_OPTION) {//用户按下了yes
name = chooser.getSelectedFile().getPath();//获取文件对象
setTitle(name);
try {
BufferedReader in = new BufferedReader(new FileReader(name));
String line = null;
String datas = "";//保存数据
//用while循环来判断读取是否结束 此处用的是行读取
while ((line = in.readLine()) != null) {
if (datas == "") {
datas = datas + line;
} else {
datas = datas + "\n" + line;
}
}
area.setText(datas);
in.close();
} catch (Exception ex) {
}
}
} public void saveText() // 保存
{
if (name == null) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Files", "txt", "java");// 限制扩展名,只能杜渠道txt和java类型的文件
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));//设置当前目录
int result = chooser.showSaveDialog(Text4.this);
if (result == JFileChooser.APPROVE_OPTION) {//用户按下了yes
name = chooser.getSelectedFile().getPath();
try {
OutputStream out = new FileOutputStream(name);
String datas = area.getText();
out.write(datas.getBytes());
out.close();
} catch (Exception ex) {
}
}
}
} public void anotherSaveText() // 另存为
{
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Files",
"txt", "java","html","js","css");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result = chooser.showSaveDialog(Text4.this);
if (result == JFileChooser.APPROVE_OPTION) {//用户按下了yes
name = chooser.getSelectedFile().getPath();
try {
OutputStream out = new FileOutputStream(name);
String datas = area.getText();
out.write(datas.getBytes());
out.close();
} catch (Exception ex) {
}
}
} class HandleAct implements ActionListener {// 内部类 public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Font oldF = area.getFont();// 获取当前字体
String name = oldF.getName();// 获取当前字体的名字
int size = oldF.getSize();// 当前字体的大小
if (e.getSource() == size_18)
area.setFont(new Font(name, oldF.getStyle(), ));
else if (e.getSource() == size_19)
area.setFont(new Font(name, oldF.getStyle(), ));
else if (e.getSource() == size_20)
area.setFont(new Font(name, oldF.getStyle(), ));
else if (e.getSource() == size_21)
area.setFont(new Font(name, oldF.getStyle(), )); if (e.getSource() == red) {
area.setForeground(Color.red);// 设置文字颜色
} else if (e.getSource() == blue) {
area.setForeground(Color.blue);// 设置文字颜色
} if (e.getSource() == bold) {
area.setFont(new Font(name, Font.BOLD, size));
} else if (e.getSource() == italics) {
area.setFont(new Font(name, Font.ITALIC, size));
} if (e.getSource() == clear) {
area.setText(" ");
} else if (e.getSource() == close) {
System.exit();
} if (e.getSource() == replice) {
//替换弹出框的布局
JPanel replacePanel = new JPanel();
JLabel lookupLabel = new JLabel("要替换的内容");
JTextField inputText = new JTextField();
JLabel replaceLabel = new JLabel("替换为");
JTextField changetoText = new JTextField();
replacePanel.add(lookupLabel);
replacePanel.add(inputText);
replacePanel.add(replaceLabel);
replacePanel.add(changetoText);
//布局结束
JOptionPane.showMessageDialog(null, replacePanel);
String text = area.getText();//获得整个文本内容
String changeText = text.replace(inputText.getText(),
changetoText.getText());//启用replace功能函数
area.setText(changeText);
}
else if (e.getSource() == check) {
//查找弹出框的布局
JPanel swapPanel=new JPanel();
JLabel seekLabel=new JLabel("要查找的内容");
JTextField inputText=new JTextField();
swapPanel.add(seekLabel);
swapPanel.add(inputText);
JOptionPane.showMessageDialog(null,swapPanel);
//布局结束
String text=area.getText();//获得整个文本内容
int fromIndex=area.getCaretPosition();//取得当前的光标位置
int lastfromIndex=text.lastIndexOf(inputText.getText(),fromIndex);//获得查找后的位置
area.setCaretPosition(lastfromIndex);
area.moveCaretPosition(lastfromIndex+inputText.getText().length());//使查找到的子字符串显示出来
} } } }