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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM