package 文本编辑器;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class TextEditBox extends JFrame {
// 加入属性
private JComboBox combox_name, combox_size;// 字体、字号组合框
private JButton button_larger,button_smaller,button_color;//字体变大变小和颜色选择器
private JCheckBox checkb_bold, checkb_italic;// 粗体、斜体复选框
private JPopupMenu popupmenu;
private JTextArea ta = new JTextArea();
private JScrollPane sp = new JScrollPane(ta);
//查找对话框属性
private JTextField tf_search;
private JButton button_next;
//
private int key=0;
public TextEditBox(String str) {
super(str);
this.setDefaultCloseOperation(WindowConstants.EXIT\_ON\_CLOSE);
Dimension dim = getToolkit().getScreenSize(); // 获得屏幕分辨率
this.setBounds(dim.width / 4, dim.height / 4, 700, 480);
JToolBar toolbar = new JToolBar(); // 创建工具栏
this.add(toolbar, BorderLayout.NORTH); // 工具栏加入到窗格北部
this.add(sp);
ta.setLineWrap(true);// 换行
//////////////////字体
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String\[\] fontsName = ge.getAvailableFontFamilyNames(); // 获得系统字体
combox\_name = new JComboBox(fontsName);
toolbar.add(combox\_name);
combox\_name.addActionListener(new ActionListener() {// 字号
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
ta.setFont(new Font(fontname, style, size));
}
});
/////////////////字号
String sizestr\[\] = { "20", "30", "40", "50", "60", "70" ,"80","90","100"};
combox\_size = new JComboBox(sizestr);
combox\_size.setEditable(true);
toolbar.add(combox\_size);
combox\_size.addActionListener(new ActionListener() {// 字号
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
int size = Integer.parseInt((String)combox\_size.getSelectedItem());
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
ta.setFont(new Font(fontname, style, size));
}
});
////////////////////字号加减按钮
button\_larger=new JButton("A+");
toolbar.add(button\_larger);
button\_larger.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize()+5;
ta.setFont(new Font(fontname, style, size));
}
});
button\_smaller=new JButton("A-");
toolbar.add(button\_smaller);
button\_smaller.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize()-5;
ta.setFont(new Font(fontname, style, size));
}
});
/////////////////J
/////////////////粗体和斜体
checkb\_bold = new JCheckBox("粗体"); //字形复选框
toolbar.add(checkb\_bold);
checkb\_bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
style = style ^ 1;
ta.setFont(new Font(fontname, style, size));
}
});
checkb\_italic = new JCheckBox("斜体");
toolbar.add(checkb\_italic);
checkb\_italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
style = style ^ 2;
ta.setFont(new Font(fontname, style, size));
}
});
////////////////
JRadioButton radiob\_color\[\];
String colorstr\[\]={"红","绿","蓝"};
ButtonGroup bgroup\_color = new ButtonGroup(); //按钮组
radiob\_color = new JRadioButton\[colorstr.length\]; //颜色单选按钮数组
for (int i=0; i<radiob\_color.length; i++){
radiob\_color\[i\]=new JRadioButton(colorstr\[i\]);
bgroup\_color.add(radiob\_color\[i\]); //加入到按钮组
toolbar.add(radiob\_color\[i\]); //加入到工具栏
}
radiob\_color\[0\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.red);// 设置颜色
}
});
radiob\_color\[1\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.green);
}
});
radiob\_color\[2\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.blue);
}
});
///////////////颜色选择器
button\_color=new JButton("其它");
toolbar.add(button\_color);
button\_color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color color;
color=JColorChooser.showDialog(TextEditBox.this,"颜色选择", Color.black);
ta.setForeground(color);// 设置颜色
}
});
////////////////鼠标事件
ta.addMouseListener(new MouseAdapter() {// 鼠标事件处理方法。右击弹出菜单
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == MouseEvent.BUTTON3\_MASK) // 单击的是鼠标右键
popupmenu.show(ta, e.getX(), e.getY()); // 在鼠标单击处显示快捷菜单
}
});
////////////////
this.addmyMenu(); //调用自己定义方法,加入菜单
this.setVisible(true);
}
private void addmyMenu() {// 加入主菜单、快捷菜单、对话框
JMenuBar menubar = new JMenuBar(); // 菜单条
this.setJMenuBar(menubar); // 加入菜单条
String menustr\[\] = { "文件", "编辑", "工具", "帮助" };
JMenu menu\[\] = new JMenu\[menustr.length\];
for (int i = 0; i < menustr.length; i++) {
menu\[i\] = new JMenu(menustr\[i\]); // 菜单
menubar.add(menu\[i\]); // 菜单条中加入菜单
}
////////////////////////////////
JMenuItem menuitem\_open = new JMenuItem("打开");
menu\[0\].add(menuitem\_open);
menuitem\_open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
int result = filechooser.showOpenDialog(TextEditBox.this);
if (result == JFileChooser.APPROVE\_OPTION) {
try {
File file = filechooser.getSelectedFile();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
ta.setText("");
String text;
while ((text = br.readLine()) != null) {
ta.append(text);
}
fr.close();
br.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(TextEditBox.this,"打开文档出错!");
}
}
}
});
JMenuItem menuitem\_save = new JMenuItem("保存");
menu\[0\].add(menuitem\_save);
menuitem\_save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
int result = filechooser.showSaveDialog(TextEditBox.this);
if (result == JFileChooser.APPROVE\_OPTION) {
try {
File file = filechooser.getSelectedFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
String text=ta.getText();
fw.write(text);
fw.close();
bw.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(TextEditBox.this,"打开文档出错。");
}
}
}
});
menu\[0\].addSeparator(); // 加分隔线
JMenuItem menuitem\_exit = new JMenuItem("退出");
menu\[0\].add(menuitem\_exit);
menuitem\_exit.addActionListener(new ActionListener() {// 退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
/////////////////////////////
JMenu menu\_style = new JMenu("字形");
JCheckBoxMenuItem checkboxmenuitem\_bold = new JCheckBoxMenuItem("粗体");
menu\_style.add(checkboxmenuitem\_bold);
JCheckBoxMenuItem checkboxmenuitem\_italic = new JCheckBoxMenuItem("斜体");
menu\_style.add(checkboxmenuitem\_italic);
menu\[1\].add(menu\_style); // 菜单加入到菜单中成为二级菜单
checkboxmenuitem\_bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
style = style ^ 1;
ta.setFont(new Font(fontname, style, size));
}
});
checkboxmenuitem\_italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox\_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
style = style ^ 2;
ta.setFont(new Font(fontname, style, size));
}
});
////////////////////////////
JMenu menu\_color = new JMenu("颜色");
menu\[1\].add(menu\_color);
ButtonGroup buttongroup = new ButtonGroup();
String colorstr\[\] = { "红", "绿", "蓝" };
JRadioButtonMenuItem rbmi\_color\[\] = new JRadioButtonMenuItem\[colorstr.length\];
for (int i = 0; i < rbmi\_color.length; i++) {
rbmi\_color\[i\] = new JRadioButtonMenuItem(colorstr\[i\]); // 单选菜单项
buttongroup.add(rbmi\_color\[i\]);
menu\_color.add(rbmi\_color\[i\]);
}
rbmi\_color\[0\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.red);
}
});
rbmi\_color\[1\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.green);
}
});
rbmi\_color\[2\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.blue);
}
});
/////////////////////////////////
JMenuItem menuitem\_countwordsnum = new JMenuItem("字数统计");
menu\[2\].add(menuitem\_countwordsnum);
menuitem\_countwordsnum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count=0;
for(int i=0;i<ta.getText().length();i++){
if(!ta.getText().substring(i,i+1).equals(" ")){
count++;
}
}
JOptionPane.showMessageDialog(TextEditBox.this, "文本框中一共同拥有"+count+"个字符!
");
}
});
menu[2].addSeparator(); // 加分隔线
JMenuItem menuitem_search = new JMenuItem("查找");
menu[2].add(menuitem_search);
menuitem_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new MessageJDialog();
button\_next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str\_search=tf\_search.getText();
int len = str\_search.length();
for (int i = key; i < ta.getText().length() - len + 1; i++) {
String str\_record = ta.getText().substring(i, i + len);
if (str\_record.equals(str\_search)) {
key = i + 1;
ta.requestFocus();
ta.select(i, i + len);
return;
}
}
}
});
key=0;
}
});
JMenuItem menuitem\_replace = new JMenuItem("替换");
menu\[2\].add(menuitem\_replace);
menuitem\_replace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str\_replace=JOptionPane.showInputDialog(TextEditBox.this,
"请输入你要替换的字符串:" );
String str\_replacelater=JOptionPane.showInputDialog(TextEditBox.this,
"请输入你要用来替换的内容:" );
int len=str\_replace.length();
for(int i=0;i<ta.getText().length()-len+1;i++){
String str\_record=ta.getText().substring(i, i+len);
if(str\_record.equals(str\_replace)){
ta.replaceRange(str\_replacelater,i, i+len);
}
}
}
});
/////////////////////////////////
JMenuItem menuitem\_about = new JMenuItem("关于");
menu\[3\].add(menuitem\_about);
menuitem\_about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(TextEditBox.this,"文本编辑器v1.0 开发人员:hhtx");
}
});
////////////////////////////////////////////////// 快捷菜单对象
popupmenu = new JPopupMenu();
String menuitemstr\[\] = { "剪切", "复制", "粘贴" };
JMenuItem popmenuitem\[\] = new JMenuItem\[menuitemstr.length\];
for (int i = 0; i < popmenuitem.length; i++) {
popmenuitem\[i\] = new JMenuItem(menuitemstr\[i\]);// 菜单项
popupmenu.add(popmenuitem\[i\]);// 快捷菜单加入菜单项
}
popmenuitem\[0\].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK\_X,
InputEvent.CTRL\_MASK));// 设置快捷键Ctrl+X
popmenuitem\[1\].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK\_C,
InputEvent.CTRL\_MASK));// 设置快捷键Ctrl+C
popmenuitem\[2\].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK\_V,
InputEvent.CTRL\_MASK));// 设置快捷键Ctrl+V
popmenuitem\[0\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.cut(); //将选中文本剪切送系统剪贴板
}
});
popmenuitem\[1\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.copy(); //将选中文本复制送系统剪贴板
}
});
popmenuitem\[2\].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.paste();//剪贴板的文本粘贴在当前位置
}
});
ta.add(popupmenu); // 文本区加入快捷菜单
}
//
private class MessageJDialog extends JDialog {
private JLabel lable\_tip;
private JPanel panel\_next = new JPanel();
private JPanel panel\_search = new JPanel();
private JPanel panel\_tip = new JPanel();
public MessageJDialog() {
super(TextEditBox.this, "查找");
this.setSize(300, 170);
this.setLocation(TextEditBox.this.getX() + 200,
TextEditBox.this.getY() + 200);
this.setLayout(new GridLayout(3, 1));
//
ImageIcon imageIcon = new ImageIcon("img/search.png");
lable\_tip = new JLabel("请输入你要查找的字符串:", imageIcon, JLabel.LEFT);
panel\_tip.add(lable\_tip);
this.add(panel\_tip);
tf\_search = new JTextField(20);
panel\_search.add(tf\_search);
this.add(panel\_search);
button\_next = new JButton("查找下一个");
panel\_next.add(button\_next);
this.add(panel\_next);
this.setVisible(true);
}
}
public static void main(String args\[\]) {
new TextEditBox("文本编辑器v1.0");
}
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTcwMDIwMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTcwMDIwMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
手机扫一扫
移动阅读更方便
你可能感兴趣的文章