HelloWorld 善戰者,求之于勢,不責于人;故能擇人而任勢。

          知止而后有定,定而后能靜,靜而后能安,安而后能慮,慮而后能得。物有本末,事有終始。知所先后,則近道矣。

            BlogJava :: 首頁 ::  :: 聯系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 40 評論 :: 0 Trackbacks

          import java.awt.BorderLayout;
          import java.awt.Container;
          import java.awt.Dimension;
          import java.awt.Font;
          import java.awt.event.ActionEvent;
          import java.io.File;
          import java.io.FileReader;
          import java.io.FileWriter;
          import java.io.IOException;
          import java.util.Hashtable;
          import java.util.Map;

          import javax.swing.AbstractAction;
          import javax.swing.Action;
          import javax.swing.ImageIcon;
          import javax.swing.JButton;
          import javax.swing.JFileChooser;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JMenu;
          import javax.swing.JMenuBar;
          import javax.swing.JOptionPane;
          import javax.swing.JPanel;
          import javax.swing.JScrollPane;
          import javax.swing.JTextArea;
          import javax.swing.JToolBar;
          import javax.swing.text.DefaultEditorKit;
          import javax.swing.text.JTextComponent;


          public class EditPlus extends JFrame {
              
          private Action openAction;
              
          private Action saveAction;
              
          private Action closeAction;
              
              
          private JTextComponent textComp;
              
          private Map actionMap = new Hashtable();
              
              
          public EditPlus() {
                  
          super("EditPlus學習");
                  textComp 
          = createTextCompoent();
                  
          this.setSize(400300);
                  
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  
                  initActionProperty();
                  Container c 
          = this.getContentPane();
                  c.add(
          new JScrollPane(textComp), BorderLayout.CENTER);
                  c.add(createToolBar(), BorderLayout.NORTH);
                  
          this.setJMenuBar(createMenuBar());        
                  
              }
              
              
          protected JTextComponent createTextCompoent() {
                  JTextArea a 
          = new JTextArea();
                  a.setLineWrap(
          true);
                  
          return a;
              }
              
              
          // 初始化Action的一些信息,如圖標等
              protected void initActionProperty() {
                  Action a 
          = null;
                  a 
          = textComp.getActionMap().get(DefaultEditorKit.cutAction);
                  a.putValue(Action.SMALL_ICON, 
          new ImageIcon("icons/cut.gif"));
                  a.putValue(Action.NAME, 
          "剪切");
                  
                  a 
          = textComp.getActionMap().get(DefaultEditorKit.copyAction);
                  a.putValue(Action.SMALL_ICON, 
          new ImageIcon("icons/copy.gif"));
                  a.putValue(Action.NAME, 
          "復制");

                  a 
          = textComp.getActionMap().get(DefaultEditorKit.pasteAction);
                  a.putValue(Action.SMALL_ICON, 
          new ImageIcon("icons/paste.gif"));
                  a.putValue(Action.NAME, 
          "粘貼");
                  
                  a 
          = textComp.getActionMap().get(DefaultEditorKit.selectAllAction);
                  a.putValue(Action.NAME, 
          "全部選定");
              }
              
              
          // 創建ToolBar
              protected JToolBar createToolBar() {
                  JToolBar bar 
          = new JToolBar();
                  bar.add(getOpenAction()).setText(
          "");
                  bar.add(getSaveAction()).setText(
          "");
                  bar.addSeparator();

                  JButton cutBtn 
          = bar.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));
                  JButton copyBtn 
          = bar.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));
                  JButton pasterBtn 
          = bar.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));
                  cutBtn.setText(
          "");
                  copyBtn.setText(
          "");
                  pasterBtn.setText(
          "");
                  
          return bar;
              }
              
              
          // 創建MenuBar
              protected JMenuBar createMenuBar() {
                  JMenuBar bar 
          = new JMenuBar();
                  JMenu file 
          = new JMenu("文件");
                  JMenu edit 
          = new JMenu("編輯");
                  bar.add(file);
                  bar.add(edit);
                  
                  file.add(getOpenAction());
                  file.add(getSaveAction());
                  file.add(getCloseAction());
                  
                  edit.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));
                  edit.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));
                  edit.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));
                  edit.add(textComp.getActionMap().get(DefaultEditorKit.selectAllAction));        
                  
          return bar;
              }
              
              
          protected Action getOpenAction() {
                  
          if (openAction == null)
                      openAction 
          = new OpenAction();
                  
          return openAction;
              }
              
              
          protected Action getSaveAction() {
                  
          if (saveAction == null)
                      saveAction 
          = new SaveAction();
                  
          return saveAction;
              }
              
              
          protected Action getCloseAction() {
                  
          if (closeAction == null)
                      closeAction 
          = new CloseAction();
                  
          return closeAction;
              }
              
              
          protected JTextComponent getTextComponent() {
                  
          return textComp;
              }
              
              
          public class OpenAction extends AbstractAction {
                  
          public OpenAction() {
                      
          super("打開"new ImageIcon("icons/open.gif"));
                  }
                  
          public void actionPerformed(ActionEvent e) {
                      JFileChooser fc 
          = new JFileChooser();
                      
          if (fc.showOpenDialog(EditPlus.this!= JFileChooser.APPROVE_OPTION) 
                          
          return;
                      File f 
          = fc.getSelectedFile();
                      
          if (f == null
                          
          return;
                      FileReader fr 
          = null;
                      
          try {
                          fr 
          = new FileReader(f);
                          textComp.read(fr, 
          null);
                      } 
          catch (Exception ee) {
                          showWarnDialog(
          "讀文件異常!",ee.toString());
                      } 
          finally {
                          
          if (fr != null) {
                              
          try {
                                  fr.close();
                              } 
          catch (IOException e1) {
                                  e1.printStackTrace();
                              }
                          }
                      }
                  }
              }
              
              
          public class SaveAction extends AbstractAction {
                  
          public SaveAction() {
                      
          super("保存"new ImageIcon("icons/save.gif"));
                  }
                  
                  
          public void actionPerformed(ActionEvent e) {
                      JFileChooser fc 
          = new JFileChooser();
                      
          if (fc.showSaveDialog(EditPlus.this!= JFileChooser.APPROVE_OPTION)
                          
          return;
                      File f 
          = fc.getSelectedFile();
                      
          if (f == null)
                          
          return;
                      
                      FileWriter fw 
          = null;
                      
          try {
                          fw 
          = new FileWriter(f);
                          textComp.write(fw);
                      } 
          catch (Exception ee) {
                          showWarnDialog(
          "保存文件異常!",ee.toString());
                      } 
          finally {
                          
          try {
                              
          if (fw != null)
                                  fw.close();
                          } 
          catch (IOException eee) {
                              eee.printStackTrace();
                          }
                      }
                  }
              }
              
              
          public class CloseAction extends AbstractAction {
                  
          public CloseAction() {
                      
          super("關閉");
                  }
                  @Override
                  
          public void actionPerformed(ActionEvent e) {
                      System.exit(
          0);
                  }        
              }
              
              
          public void showWarnDialog(String msg, String warn) {
                  JPanel p 
          = new JPanel();
                  JLabel label 
          = new JLabel(msg);
                  label.setFont(label.getFont().deriveFont(Font.BOLD));
                  JTextArea area 
          = new JTextArea(warn);
                  area.setOpaque(
          false);
                  area.setLineWrap(
          true);
                  area.setPreferredSize(
          new Dimension(280100));
                  p.setLayout(
          new BorderLayout());
                  p.add(
          new JLabel(" "), BorderLayout.CENTER);
                  p.add(label, BorderLayout.NORTH);
                  p.add(
          new JScrollPane(area), BorderLayout.SOUTH);
                  JOptionPane.showMessageDialog(EditPlus.
          this, p, "異常信息"
                          JOptionPane.WARNING_MESSAGE);

              }
              
              
          public static void main(String args[]) {
                  Utils.setLookAndFeel();
                  EditPlus ep 
          = new EditPlus();
                  ep.setVisible(
          true);
              }
          }


          </script>

          posted on 2008-03-05 22:25 helloworld2008 閱讀(458) 評論(0)  編輯  收藏 所屬分類: java - swing

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 习水县| 上饶市| 高雄县| 黄骅市| 呼和浩特市| 乌拉特前旗| 叙永县| 都江堰市| 苍梧县| 永寿县| 南雄市| 醴陵市| 大丰市| 阿图什市| 璧山县| 五常市| 商都县| 蕲春县| 阳城县| 巴东县| 墨脱县| 武乡县| 仙居县| 海口市| 玛多县| 巩留县| 集安市| 五莲县| 揭西县| 和静县| 白沙| 峨边| 石渠县| 荣昌县| 阳新县| 玉环县| 青海省| 锦屏县| 潞西市| 福清市| 唐海县|