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

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

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

    HelloWorld 善戰(zhàn)者,求之于勢(shì),不責(zé)于人;故能擇人而任勢(shì)。

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

      BlogJava :: 首頁 ::  :: 聯(lián)系 ::  :: 管理 ::
      167 隨筆 :: 1 文章 :: 40 評(píng)論 :: 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學(xué)習(xí)");
            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的一些信息,如圖標(biāo)等
        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, 
    "復(fù)制");

            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, 
    "全部選定");
        }
        
        
    // 創(chuàng)建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;
        }
        
        
    // 創(chuàng)建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("關(guān)閉");
            }
            @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) 評(píng)論(0)  編輯  收藏 所屬分類: java - swing

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久免费看少妇高潮V片特黄| 自拍偷自拍亚洲精品第1页| 中文字幕av免费专区| 亚洲午夜无码久久久久软件| 亚洲v高清理论电影| 亚洲国产高清在线一区二区三区| 69成人免费视频无码专区| 久久精品无码精品免费专区| 九九免费观看全部免费视频| 亚洲一区二区三区国产精华液| 亚洲午夜在线电影| 亚洲国产三级在线观看| 亚洲成?v人片天堂网无码| 青青青国产免费一夜七次郎| 亚洲性线免费观看视频成熟| 91精品国产免费久久国语蜜臀| 中文精品人人永久免费| 精品免费AV一区二区三区| 亚洲一区二区三区在线观看网站| 亚洲欧洲精品久久| 亚洲午夜未满十八勿入| 亚洲成a人片在线观看无码专区| 国产偷窥女洗浴在线观看亚洲| 亚洲成aⅴ人片久青草影院| 国产无遮挡色视频免费视频| 永久久久免费浮力影院| 青苹果乐园免费高清在线| AV无码免费永久在线观看| 88xx成人永久免费观看| 亚洲国产精品免费视频| 另类免费视频一区二区在线观看| 美女无遮挡拍拍拍免费视频| 久久免费99精品国产自在现线| 一级特黄a免费大片| 一级大黄美女免费播放| 国产精品免费观看视频| 精品免费久久久久国产一区| 精品97国产免费人成视频| 99精品视频免费| 大地影院MV在线观看视频免费| 拍拍拍无挡免费视频网站|