java文本编辑器v2.0 图形用户界面


  1. package 文本编辑器;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Color;  
  5. import java.awt.Dimension;  
  6. import java.awt.Font;  
  7. import java.awt.GraphicsEnvironment;  
  8. import java.awt.GridLayout;  
  9. import java.awt.event.ActionEvent;  
  10. import java.awt.event.ActionListener;  
  11. import java.awt.event.InputEvent;  
  12. import java.awt.event.KeyEvent;  
  13. import java.awt.event.MouseAdapter;  
  14. import java.awt.event.MouseEvent;  
  15. import java.io.BufferedReader;  
  16. import java.io.BufferedWriter;  
  17. import java.io.File;  
  18. import java.io.FileReader;  
  19. import java.io.FileWriter;  
  20.   
  21. import javax.swing.ButtonGroup;  
  22. import javax.swing.ImageIcon;  
  23. import javax.swing.JButton;  
  24. import javax.swing.JCheckBox;  
  25. import javax.swing.JCheckBoxMenuItem;  
  26. import javax.swing.JColorChooser;  
  27. import javax.swing.JComboBox;  
  28. import javax.swing.JDialog;  
  29. import javax.swing.JFileChooser;  
  30. import javax.swing.JFrame;  
  31. import javax.swing.JLabel;  
  32. import javax.swing.JMenu;  
  33. import javax.swing.JMenuBar;  
  34. import javax.swing.JMenuItem;  
  35. import javax.swing.JOptionPane;  
  36. import javax.swing.JPanel;  
  37. import javax.swing.JPopupMenu;  
  38. import javax.swing.JRadioButton;  
  39. import javax.swing.JRadioButtonMenuItem;  
  40. import javax.swing.JScrollPane;  
  41. import javax.swing.JTextArea;  
  42. import javax.swing.JTextField;  
  43. import javax.swing.JToolBar;  
  44. import javax.swing.KeyStroke;  
  45. import javax.swing.WindowConstants;  
  46.   
  47.   
  48. @SuppressWarnings("serial")  
  49. public class TextEditBox extends JFrame {  
  50.     // 添加属性  
  51.     private JComboBox combox_name, combox_size;// 字体、字号组合框  
  52.     private JButton button_larger,button_smaller,button_color;//字体变大变小和颜色选择器  
  53.     private JCheckBox checkb_bold, checkb_italic;// 粗体、斜体复选框  
  54.     private JPopupMenu popupmenu;  
  55.     private JTextArea ta = new JTextArea();  
  56.     private JScrollPane sp = new JScrollPane(ta);  
  57.     //查找对话框属性  
  58.     private JTextField tf_search;  
  59.     private JButton button_next;  
  60.     //  
  61.     private int key=0;  
  62.   
  63.     public TextEditBox(String str) {  
  64.         super(str);  
  65.         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  66.         Dimension dim = getToolkit().getScreenSize(); // 获得屏幕分辨率  
  67.         this.setBounds(dim.width / 4, dim.height / 4, 700, 480);  
  68.         JToolBar toolbar = new JToolBar(); // 创建工具栏  
  69.         this.add(toolbar, BorderLayout.NORTH); // 工具栏添加到窗格北部  
  70.         this.add(sp);  
  71.         ta.setLineWrap(true);// 换行  
  72.         //////////////////字体  
  73.         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
  74.         String[] fontsName = ge.getAvailableFontFamilyNames(); // 获得系统字体  
  75.         combox_name = new JComboBox(fontsName);  
  76.         toolbar.add(combox_name);  
  77.         combox_name.addActionListener(new ActionListener() {// 字号  
  78.             public void actionPerformed(ActionEvent e) {  
  79.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  80.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  81.                 int style = font.getStyle();      //获得字形  
  82.                 int size = font.getSize();  
  83.                 ta.setFont(new Font(fontname, style, size));  
  84.             }  
  85.         });  
  86.         /////////////////字号  
  87.         String sizestr[] = { "20", "30", "40", "50", "60", "70" ,"80","90","100"};  
  88.         combox_size = new JComboBox(sizestr);  
  89.         combox_size.setEditable(true);  
  90.         toolbar.add(combox_size);  
  91.         combox_size.addActionListener(new ActionListener() {// 字号  
  92.             public void actionPerformed(ActionEvent e) {  
  93.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  94.                 int size = Integer.parseInt((String)combox_size.getSelectedItem());  
  95.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  96.                 int style = font.getStyle();      //获得字形  
  97.                 ta.setFont(new Font(fontname, style, size));  
  98.             }  
  99.         });  
  100.         ////////////////////字号加减按钮  
  101.         button_larger=new JButton("A+");  
  102.         toolbar.add(button_larger);  
  103.         button_larger.addActionListener(new ActionListener() {  
  104.             public void actionPerformed(ActionEvent e) {  
  105.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  106.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  107.                 int style = font.getStyle();      //获得字形  
  108.                 int size = font.getSize()+5;  
  109.                 ta.setFont(new Font(fontname, style, size));  
  110.             }  
  111.         });  
  112.         button_smaller=new JButton("A-");  
  113.         toolbar.add(button_smaller);  
  114.         button_smaller.addActionListener(new ActionListener() {  
  115.             public void actionPerformed(ActionEvent e) {  
  116.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  117.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  118.                 int style = font.getStyle();      //获得字形  
  119.                 int size = font.getSize()-5;  
  120.                 ta.setFont(new Font(fontname, style, size));  
  121.             }  
  122.         });  
  123.         /////////////////J  
  124.         /////////////////粗体和斜体  
  125.         checkb_bold = new JCheckBox("粗体"); //字形复选框  
  126.         toolbar.add(checkb_bold);  
  127.         checkb_bold.addActionListener(new ActionListener() {  
  128.             public void actionPerformed(ActionEvent e) {  
  129.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  130.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  131.                 int style = font.getStyle();      //获得字形  
  132.                 int size = font.getSize();  
  133.                 style = style ^ 1;  
  134.                 ta.setFont(new Font(fontname, style, size));  
  135.             }  
  136.         });  
  137.         checkb_italic = new JCheckBox("斜体");  
  138.         toolbar.add(checkb_italic);  
  139.         checkb_italic.addActionListener(new ActionListener() {  
  140.             public void actionPerformed(ActionEvent e) {  
  141.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  142.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  143.                 int style = font.getStyle();      //获得字形  
  144.                 int size = font.getSize();  
  145.                 style = style ^ 2;  
  146.                 ta.setFont(new Font(fontname, style, size));  
  147.             }  
  148.         });  
  149.         ////////////////  
  150.         JRadioButton radiob_color[];  
  151.         String colorstr[]={"红","绿","蓝"};  
  152.         ButtonGroup bgroup_color = new ButtonGroup();      //按钮组  
  153.         radiob_color = new JRadioButton[colorstr.length];  //颜色单选按钮数组  
  154.         for (int i=0; i<radiob_color.length; i++){  
  155.             radiob_color[i]=new JRadioButton(colorstr[i]);   
  156.             bgroup_color.add(radiob_color[i]); //添加到按钮组  
  157.             toolbar.add(radiob_color[i]);     //添加到工具栏  
  158.         }          
  159.         radiob_color[0].addActionListener(new ActionListener() {  
  160.             public void actionPerformed(ActionEvent e) {  
  161.                 ta.setForeground(Color.red);// 设置颜色  
  162.             }  
  163.         });  
  164.         radiob_color[1].addActionListener(new ActionListener() {  
  165.             public void actionPerformed(ActionEvent e) {  
  166.                 ta.setForeground(Color.green);  
  167.             }  
  168.         });  
  169.         radiob_color[2].addActionListener(new ActionListener() {  
  170.             public void actionPerformed(ActionEvent e) {  
  171.                 ta.setForeground(Color.blue);  
  172.             }  
  173.         });  
  174.         ///////////////颜色选择器  
  175.         button_color=new JButton("其他");  
  176.         toolbar.add(button_color);  
  177.         button_color.addActionListener(new ActionListener() {  
  178.             public void actionPerformed(ActionEvent e) {  
  179.                 Color color;  
  180.                 color=JColorChooser.showDialog(TextEditBox.this,"颜色选择", Color.black);  
  181.                 ta.setForeground(color);// 设置颜色  
  182.             }  
  183.         });  
  184.         ////////////////鼠标事件  
  185.         ta.addMouseListener(new MouseAdapter() {// 鼠标事件处理方法,右击弹出菜单  
  186.             public void mouseClicked(MouseEvent e) {  
  187.                 if (e.getModifiers() == MouseEvent.BUTTON3_MASK) // 单击的是鼠标右键  
  188.                     popupmenu.show(ta, e.getX(), e.getY()); // 在鼠标单击处显示快捷菜单  
  189.             }  
  190.         });  
  191.         ////////////////  
  192.         this.addmyMenu();       //调用自定义方法,添加菜单  
  193.         this.setVisible(true);  
  194.     }  
  195.   
  196.     private void addmyMenu() {// 添加主菜单、快捷菜单、对话框  
  197.         JMenuBar menubar = new JMenuBar(); // 菜单栏  
  198.         this.setJMenuBar(menubar); // 添加菜单栏  
  199.         String menustr[] = { "文件", "编辑", "工具", "帮助" };  
  200.         JMenu menu[] = new JMenu[menustr.length];  
  201.         for (int i = 0; i < menustr.length; i++) {  
  202.             menu[i] = new JMenu(menustr[i]); // 菜单  
  203.             menubar.add(menu[i]); // 菜单栏中加入菜单  
  204.         }  
  205.         ////////////////////////////////  
  206.         JMenuItem menuitem_open = new JMenuItem("打开");  
  207.         menu[0].add(menuitem_open);  
  208.         menuitem_open.addActionListener(new ActionListener() {  
  209.             public void actionPerformed(ActionEvent e) {  
  210.                 JFileChooser filechooser = new JFileChooser();  
  211.                 int result = filechooser.showOpenDialog(TextEditBox.this);  
  212.                 if (result == JFileChooser.APPROVE_OPTION) {  
  213.                     try {  
  214.                         File file = filechooser.getSelectedFile();  
  215.                         FileReader fr = new FileReader(file);  
  216.                         BufferedReader br = new BufferedReader(fr);  
  217.                         ta.setText("");  
  218.                         String text;  
  219.                         while ((text = br.readLine()) != null) {  
  220.                             ta.append(text);  
  221.                         }  
  222.                         fr.close();  
  223.                         br.close();  
  224.                     } catch (Exception ex) {  
  225.                         JOptionPane.showMessageDialog(TextEditBox.this,"打开文档出错!");  
  226.                     }  
  227.                 }  
  228.             }  
  229.         });  
  230.         JMenuItem menuitem_save = new JMenuItem("保存");  
  231.         menu[0].add(menuitem_save);  
  232.         menuitem_save.addActionListener(new ActionListener() {  
  233.             public void actionPerformed(ActionEvent e) {  
  234.                 JFileChooser filechooser = new JFileChooser();  
  235.                 int result = filechooser.showSaveDialog(TextEditBox.this);  
  236.                 if (result == JFileChooser.APPROVE_OPTION) {  
  237.                     try {  
  238.                         File file = filechooser.getSelectedFile();  
  239.                         FileWriter fw = new FileWriter(file);  
  240.                         BufferedWriter bw = new BufferedWriter(fw);  
  241.                         String text=ta.getText();  
  242.                         fw.write(text);  
  243.                         fw.close();  
  244.                         bw.close();  
  245.                     } catch (Exception ex) {  
  246.                         JOptionPane.showMessageDialog(TextEditBox.this,"打开文档出错!");  
  247.                     }  
  248.                 }  
  249.   
  250.             }  
  251.         });  
  252.         menu[0].addSeparator(); // 加分隔线  
  253.         JMenuItem menuitem_exit = new JMenuItem("退出");  
  254.         menu[0].add(menuitem_exit);  
  255.         menuitem_exit.addActionListener(new ActionListener() {// 退出  
  256.                     public void actionPerformed(ActionEvent e) {  
  257.                         System.exit(0);  
  258.                     }  
  259.                 });  
  260.         /////////////////////////////  
  261.         JMenu menu_style = new JMenu("字形");  
  262.         JCheckBoxMenuItem checkboxmenuitem_bold = new JCheckBoxMenuItem("粗体");  
  263.         menu_style.add(checkboxmenuitem_bold);  
  264.         JCheckBoxMenuItem checkboxmenuitem_italic = new JCheckBoxMenuItem("斜体");  
  265.         menu_style.add(checkboxmenuitem_italic);  
  266.         menu[1].add(menu_style); // 菜单加入到菜单中成为二级菜单  
  267.         checkboxmenuitem_bold.addActionListener(new ActionListener() {  
  268.             public void actionPerformed(ActionEvent e) {  
  269.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  270.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  271.                 int style = font.getStyle();      //获得字形  
  272.                 int size = font.getSize();  
  273.                 style = style ^ 1;  
  274.                 ta.setFont(new Font(fontname, style, size));  
  275.             }  
  276.         });  
  277.           
  278.         checkboxmenuitem_italic.addActionListener(new ActionListener() {  
  279.             public void actionPerformed(ActionEvent e) {  
  280.                 String fontname = (String)combox_name.getSelectedItem();//获得字体名  
  281.                 Font font = ta.getFont();     //获得文本区的当前字体对象  
  282.                 int style = font.getStyle();      //获得字形  
  283.                 int size = font.getSize();  
  284.                 style = style ^ 2;  
  285.                 ta.setFont(new Font(fontname, style, size));  
  286.             }  
  287.         });  
  288.         ////////////////////////////  
  289.         JMenu menu_color = new JMenu("颜色");  
  290.         menu[1].add(menu_color);  
  291.         ButtonGroup buttongroup = new ButtonGroup();  
  292.         String colorstr[] = { "红", "绿", "蓝" };  
  293.         JRadioButtonMenuItem rbmi_color[] = new JRadioButtonMenuItem[colorstr.length];  
  294.         for (int i = 0; i < rbmi_color.length; i++) {  
  295.             rbmi_color[i] = new JRadioButtonMenuItem(colorstr[i]); // 单选菜单项  
  296.             buttongroup.add(rbmi_color[i]);  
  297.             menu_color.add(rbmi_color[i]);  
  298.         }  
  299.         rbmi_color[0].addActionListener(new ActionListener() {  
  300.             public void actionPerformed(ActionEvent e) {  
  301.                 ta.setForeground(Color.red);  
  302.             }  
  303.         });  
  304.         rbmi_color[1].addActionListener(new ActionListener() {  
  305.             public void actionPerformed(ActionEvent e) {  
  306.                 ta.setForeground(Color.green);  
  307.             }  
  308.         });  
  309.         rbmi_color[2].addActionListener(new ActionListener() {  
  310.             public void actionPerformed(ActionEvent e) {  
  311.                 ta.setForeground(Color.blue);  
  312.             }  
  313.         });  
  314.         /////////////////////////////////  
  315.         JMenuItem menuitem_countwordsnum = new JMenuItem("字数统计");  
  316.         menu[2].add(menuitem_countwordsnum);  
  317.         menuitem_countwordsnum.addActionListener(new ActionListener() {  
  318.             public void actionPerformed(ActionEvent e) {  
  319.                 int count=0;  
  320.                 for(int i=0;i<ta.getText().length();i++){  
  321.                     if(!ta.getText().substring(i,i+1).equals(" ")){  
  322.                         count++;  
  323.                     }  
  324.                 }  
  325.                 JOptionPane.showMessageDialog(TextEditBox.this, "文本框中一共有"+count+"个字符!");  
  326.             }  
  327.         });  
  328.         menu[2].addSeparator(); // 加分隔线  
  329.         JMenuItem menuitem_search = new JMenuItem("查找");  
  330.         menu[2].add(menuitem_search);  
  331.         menuitem_search.addActionListener(new ActionListener() {  
  332.             public void actionPerformed(ActionEvent e) {  
  333.                 new MessageJDialog();  
  334.                   
  335.                 button_next.addActionListener(new ActionListener() {  
  336.                     public void actionPerformed(ActionEvent e) {  
  337.                         String str_search=tf_search.getText();  
  338.                         int len = str_search.length();  
  339.                         for (int i = key; i < ta.getText().length() - len + 1; i++) {  
  340.                             String str_record = ta.getText().substring(i, i + len);  
  341.                             if (str_record.equals(str_search)) {  
  342.                                 key = i + 1;  
  343.                                 ta.requestFocus();  
  344.                                 ta.select(i, i + len);  
  345.                                 return;  
  346.                             }  
  347.                         }  
  348.                     }  
  349.                 });  
  350.                   
  351.                 key=0;  
  352.             }  
  353.         });  
  354.         JMenuItem menuitem_replace = new JMenuItem("替换");  
  355.         menu[2].add(menuitem_replace);  
  356.         menuitem_replace.addActionListener(new ActionListener() {  
  357.             public void actionPerformed(ActionEvent e) {  
  358.                 String str_replace=JOptionPane.showInputDialog(TextEditBox.this,  
  359.                         "请输入你要替换的字符串:" );  
  360.                 String str_replacelater=JOptionPane.showInputDialog(TextEditBox.this,  
  361.                         "请输入你要用来替换的内容:" );  
  362.                 int len=str_replace.length();  
  363.                 for(int i=0;i<ta.getText().length()-len+1;i++){  
  364.                     String str_record=ta.getText().substring(i, i+len);  
  365.                     if(str_record.equals(str_replace)){  
  366.                         ta.replaceRange(str_replacelater,i, i+len);  
  367.                     }  
  368.                 }  
  369.             }  
  370.         });  
  371.         /////////////////////////////////  
  372.         JMenuItem menuitem_about = new JMenuItem("关于");  
  373.         menu[3].add(menuitem_about);  
  374.         menuitem_about.addActionListener(new ActionListener() {  
  375.             public void actionPerformed(ActionEvent e) {  
  376.                 JOptionPane.showMessageDialog(TextEditBox.this,"文本编辑器v1.0   开发者:hhtx");  
  377.             }  
  378.         });  
  379.         ////////////////////////////////////////////////// 快捷菜单对象  
  380.         popupmenu = new JPopupMenu();  
  381.         String menuitemstr[] = { "剪切", "复制", "粘贴" };  
  382.         JMenuItem popmenuitem[] = new JMenuItem[menuitemstr.length];  
  383.         for (int i = 0; i < popmenuitem.length; i++) {  
  384.             popmenuitem[i] = new JMenuItem(menuitemstr[i]);// 菜单项  
  385.             popupmenu.add(popmenuitem[i]);// 快捷菜单加入菜单项  
  386.         }  
  387.         popmenuitem[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,  
  388.                 InputEvent.CTRL_MASK));// 设置快捷键Ctrl+X  
  389.         popmenuitem[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,  
  390.                 InputEvent.CTRL_MASK));// 设置快捷键Ctrl+C  
  391.         popmenuitem[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,  
  392.                 InputEvent.CTRL_MASK));// 设置快捷键Ctrl+V  
  393.   
  394.         popmenuitem[0].addActionListener(new ActionListener() {  
  395.             public void actionPerformed(ActionEvent e) {  
  396.                 ta.cut(); //将选中文本剪切送系统剪贴板  
  397.             }  
  398.         });  
  399.         popmenuitem[1].addActionListener(new ActionListener() {  
  400.             public void actionPerformed(ActionEvent e) {  
  401.                 ta.copy(); //将选中文本复制送系统剪贴板  
  402.             }  
  403.         });  
  404.         popmenuitem[2].addActionListener(new ActionListener() {  
  405.             public void actionPerformed(ActionEvent e) {  
  406.                 ta.paste();//剪贴板的文本粘贴在当前位置  
  407.             }  
  408.         });  
  409.         ta.add(popupmenu); // 文本区添加快捷菜单  
  410.     }  
  411.   
  412.     //  
  413.     private class MessageJDialog extends JDialog {  
  414.         private JLabel lable_tip;  
  415.         private JPanel panel_next = new JPanel();  
  416.         private JPanel panel_search = new JPanel();  
  417.         private JPanel panel_tip = new JPanel();  
  418.   
  419.         public MessageJDialog() {  
  420.             super(TextEditBox.this, "查找");  
  421.             this.setSize(300, 170);  
  422.             this.setLocation(TextEditBox.this.getX() + 200,  
  423.                     TextEditBox.this.getY() + 200);  
  424.             this.setLayout(new GridLayout(3, 1));  
  425.             //  
  426.             ImageIcon imageIcon = new ImageIcon("img/search.png");  
  427.             lable_tip = new JLabel("请输入你要查找的字符串:", imageIcon, JLabel.LEFT);  
  428.             panel_tip.add(lable_tip);  
  429.             this.add(panel_tip);  
  430.             tf_search = new JTextField(20);  
  431.             panel_search.add(tf_search);  
  432.             this.add(panel_search);  
  433.             button_next = new JButton("查找下一个");  
  434.             panel_next.add(button_next);  
  435.             this.add(panel_next);  
  436.             this.setVisible(true);  
  437.         }  
  438.     }  
  439.       
  440.     public static void main(String args[]) {  
  441.         new TextEditBox("文本编辑器v1.0");  
  442.     }  
  443. }  





 
3
0
原文地址:https://www.cnblogs.com/aipan/p/6834855.html