java 學習 ——文本編輯框小程式

來源:互聯網
上載者:User

標籤:java   小程式   

簡易的文本編輯框小程式:

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;class TextEditorFrame extends JFrame{   File file=null;   Color color=Color.red;       TextEditorFrame(){      initTextPane();   initAboutDialog();   initToolBar();   initMenu();   }      void initTextPane(){      getContentPane().add(new JScrollPane(text));   }        JTextPane text=new JTextPane(); //這是用來做文字框的    JFileChooser filechooser=new JFileChooser(); //檔案選擇框    JColorChooser colorchooser=new JColorChooser();//    JDialog about=new JDialog(this); //關於對話方塊    JMenuBar menubar=new JMenuBar();//菜單                  JMenu[] menus=new JMenu[]{        new JMenu("檔案"),    new JMenu("編輯"),    new JMenu("協助")    };    JMenuItem menuitems[][]=new JMenuItem[][]{{new JMenuItem("建立"),new JMenuItem("開啟"),new JMenuItem("儲存"),new JMenuItem("退出")},         {new JMenuItem("複製"),         new JMenuItem("剪下"),new JMenuItem("粘貼"),},{new JMenuItem("關於")}    };     void initMenu(){              for(int i=0;i<menus.length;i++){                menubar.add(menus[i]);        for(int j=0;j<menuitems[i].length;j++){                menus[i].add(menuitems[i][j]);        menuitems[i][j].addActionListener( action );        }        }        this.setJMenuBar(menubar);     }          ActionListener action=new ActionListener(){     public void actionPerformed(ActionEvent e){        JMenuItem mi=(JMenuItem)e.getSource();    String id=mi.getText();    if(id.equals("建立")){        text.setText("");    file=null;    }else if(id.equals("開啟")){        if(file !=null)filechooser.setSelectedFile(file);                int returnVal=filechooser.showOpenDialog(TextEditorFrame.this);                if(returnVal==JFileChooser.APPROVE_OPTION){                   file=filechooser.getSelectedFile();                   openFile();                }    }else if(id.equals("儲存")){        if(file!=null) filechooser.setSelectedFile(file);    int returnVal=filechooser.showSaveDialog(TextEditorFrame.this);    if(returnVal==JFileChooser.APPROVE_OPTION){        file=filechooser.getSelectedFile();    saveFile();    }            }else if(id.equals("退出")){                        TextEditorFrame f=new TextEditorFrame();            int s=JOptionPane.showConfirmDialog(f,"你真的要退出嗎","退出程式",JOptionPane.YES_NO_CANCEL_OPTION);            if(s==JOptionPane.YES_OPTION)            System.exit(0);            }else if(id.equals("剪下")){                        text.cut();            }else if(id.equals("複製")){                        text.copy();            }else if(id.equals("粘貼")){                        text.paste();            }else if(id.equals("關於")){                         about.setSize(300,250);             about.show();            }       }     };     void saveFile(){        try{    FileWriter fw=new FileWriter(file);    fw.write(text.getText());    fw.close();    }    catch(Exception e){    e.printStackTrace();    }    }                     void openFile(){            try{             FileReader fr=new FileReader(file);             int len=(int)file.length();             char []buffer=new char[len];             fr.read(buffer,0,len);             fr.close();             text.setText(new String(buffer));             }        catch(Exception e){        e.printStackTrace();        }      }     void initAboutDialog(){        about.getContentPane().add(new JLabel("You have been making great progress."));    about.setModal(true);      }       JToolBar toolbar=new JToolBar();//我來加上工具條   JButton [] buttons=new JButton[]{               new JButton("", new ImageIcon("qin.jpg")),        new JButton("", new ImageIcon("shu.jpg")),        new JButton("", new ImageIcon("xin.jpg"))    }; void initToolBar(){      for(int i=0;i<buttons.length;i++)   toolbar.add(buttons[i]);   buttons[0].setToolTipText("複製");       buttons[0].addActionListener(new ActionListener(){              public void actionPerformed(ActionEvent e){                    text.copy();       }       });    buttons[1].setToolTipText("剪下");          buttons[1].addActionListener(new ActionListener(){              public void actionPerformed(ActionEvent e){                       text.cut();          }    });    buttons[2].setToolTipText("粘貼");buttons[2].addActionListener(new ActionListener(){          public void actionPerformed(ActionEvent e){                       text.paste();          }  });this.getContentPane().add(toolbar,BorderLayout.NORTH);}}     public class F{public static void main(String args[]){  TextEditorFrame f=new TextEditorFrame();               f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){TextEditorFrame f=new TextEditorFrame();int s=JOptionPane.showConfirmDialog(f,"你真的要退出嗎","退出程式",JOptionPane.YES_NO_OPTION);if(s==JOptionPane.YES_OPTION)System.exit(0);}});                f.setTitle("TextEditor");f.setSize(800,600);f.show();}}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java 學習 ——文本編輯框小程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.