<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(457) 評論(0)  編輯  收藏 所屬分類: java - swing

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


    網站導航:
     
    主站蜘蛛池模板: 国产成人精品日本亚洲11| 永久免费无码网站在线观看| 全亚洲最新黄色特级网站 | 成年大片免费视频| 亚洲综合激情视频| 麻豆视频免费播放| 亚洲一区欧洲一区| 免费的涩涩视频在线播放| 羞羞视频免费网站入口| 免费一级e一片在线播放| 日本高清免费中文在线看| 亚洲日本在线观看视频| 十八禁视频在线观看免费无码无遮挡骂过 | 亚洲精品国产精品国自产网站| 最近2022中文字幕免费视频| 亚洲精品国产手机| 免费电影在线观看网站| 亚洲AV永久无码精品网站在线观看 | 婷婷精品国产亚洲AV麻豆不片 | 亚洲熟妇无码八V在线播放| 成人午夜视频免费| 免费大片黄在线观看| 亚洲综合无码AV一区二区| 无码一区二区三区免费| 亚洲精品第一综合99久久| 日韩一区二区免费视频| 老司机精品免费视频| 亚洲最大在线观看| 国产免费变态视频网址网站| 国产综合免费精品久久久| 亚洲激情电影在线| 免费jjzz在在线播放国产| 华人在线精品免费观看| 亚洲 日韩经典 中文字幕| 亚洲区日韩区无码区| 中文字幕免费观看| 黄页网站在线视频免费| 久久亚洲AV成人无码电影| 色视频色露露永久免费观看| 免费毛片在线看不用播放器 | 黄色成人网站免费无码av|