当前位置:  技术问答>java相关

谁给看看???

    来源: 互联网  发布时间:2017-04-17

    本文导语:  import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.undo.*; import javax.swing.event.*; public class UndoTest extends JFrame {         protected Container pane;         protected UndoAction un...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import javax.swing.event.*;

public class UndoTest extends JFrame {

        protected Container pane;
        protected UndoAction undoAction=new UndoAction();
        protected RedoAction redoAction=new RedoAction();
        protected UndoManager undoManager=new UndoManager(); 
       
    public static void main(String args[]) {
        UndoTest ut=new UndoTest();     
        ut.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ut.setSize(400,300); 
        ut.setVisible(true); 
    }
     
    public void UndoTest() {
        BuildLayout(); 
        



    }
     
    public void BuildLayout() {
        pane=getContentPane();
        JMenuBar jmb=new JMenuBar();
        setJMenuBar(jmb);
        JMenu jm=new JMenu("Edit");
        jmb.add(jm); 
        jm.add(new JMenuItem("Test"));
        jm.add(undoAction); 
        jm.add(redoAction);
        
        JTextField textfield=new JTextField(10); 
        createUndoListener(textfield.getDocument());
        pane.setLayout(new BorderLayout());
        pane.add(textfield,BorderLayout.SOUTH);
        
        undoAction.refreshUndoState();
        redoAction.refreshRedoState();          
               
                      
    }   
    
     protected void createUndoListener(Document document) {
         document.addUndoableEditListener(new UndoableEditListener() {
             public void undoableEditHappened(UndoableEditEvent uee){
                 undoManager.addEdit(uee.getEdit());
                 undoAction.refreshUndoState();
                 redoAction.refreshRedoState();
                                                       
             }

         });  
     

     }
         
     class UndoAction extends AbstractAction {
         public UndoAction() {
             super("Undo");
         }
         public void actionPerformed(ActionEvent event) {
            try {
                undoManager.undo();  
            } catch (CannotUndoException cue) {
                JOptionPane.showMessageDialog(pane,"Unable to Undo:"+cue);  
            
            } 
            refreshUndoState();
            redoAction.refreshRedoState();
         } 
         protected void refreshUndoState() {
             if (undoManager.canUndo()) {
                 setEnabled(true); 
                 putValue(Action.NAME,undoManager.getUndoPresentationName());
             }
              else {
                 setEnabled(false);
                 putValue(Action.NAME,"Undo"); 
             }
         
         }  
     }  
     
     class RedoAction extends AbstractAction {
         public  RedoAction() {
             super("Redo");  
         }    
         public void actionPerformed(ActionEvent event) {
             try {
                 undoManager.redo();
             }  catch(CannotRedoException cre) {
                 JOptionPane.showMessageDialog(pane,"Unable to redo:"+cre);     
             } 
             refreshRedoState();
             undoAction.refreshUndoState();
         }
         protected void refreshRedoState() {
             if (undoManager.canRedo()) {
                 setEnabled(true);
                 putValue(Action.NAME,undoManager.getRedoPresentationName());
   
             }
              else {
                 setEnabled(false);
                 putValue(Action.NAME,"Redo"); 
             }
         }           
     }        
}

这个程序是书上抄的,编译可以通过,可是却看不到程序中要显示的内容,谁给看看

|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import javax.swing.event.*;

public class UndoTest extends JFrame {
    protected Container pane;
    protected UndoAction undoAction=new UndoAction();
    protected RedoAction redoAction=new RedoAction();
    protected UndoManager undoManager=new UndoManager();
    public static void main(String args[]) {
        UndoTest ut=new UndoTest();
        // ut.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ut.setSize(400,300);
        ut.setVisible(true);
    }


    public void BuildLayout() {
        pane=getContentPane();
        JMenuBar jmb=new JMenuBar();
        JMenu jm=new JMenu("Edit");
        jm.add(new JMenuItem("Test"));
        jm.add(new JMenuItem("Test2"));
        jm.add(undoAction);
        jm.add(redoAction);
        jmb.add(jm);
        this.setJMenuBar(jmb);

        JTextField textfield=new JTextField(10);
        createUndoListener(textfield.getDocument());
        pane.setLayout(new BorderLayout());
        pane.add(textfield,BorderLayout.SOUTH);
        undoAction.refreshUndoState();
        redoAction.refreshRedoState();
        this.validate();
    }

    protected void createUndoListener(Document document) {
        document.addUndoableEditListener(new UndoableEditListener() {
            public void undoableEditHappened(UndoableEditEvent uee){
                undoManager.addEdit(uee.getEdit());
                undoAction.refreshUndoState();
                redoAction.refreshRedoState();

            }

        });


    }

    class UndoAction extends AbstractAction {
        public UndoAction() {
            super("Undo");
        }
        public void actionPerformed(ActionEvent event) {
            try {
                undoManager.undo();
            } catch (CannotUndoException cue) {
                JOptionPane.showMessageDialog(pane,"Unable to Undo:"+cue);

            }
            refreshUndoState();
            redoAction.refreshRedoState();
        }
        protected void refreshUndoState() {
            if (undoManager.canUndo()) {
                setEnabled(true);
                putValue(Action.NAME,undoManager.getUndoPresentationName());
            }
            else {
                setEnabled(false);
                putValue(Action.NAME,"Undo");
            }

        }
    }

    class RedoAction extends AbstractAction {
        public  RedoAction() {
            super("Redo");
        }
        public void actionPerformed(ActionEvent event) {
            try {
                undoManager.redo();
            }  catch(CannotRedoException cre) {
                JOptionPane.showMessageDialog(pane,"Unable to redo:"+cre);
            }
            refreshRedoState();
            undoAction.refreshUndoState();
        }
        protected void refreshRedoState() {
            if (undoManager.canRedo()) {
                setEnabled(true);
                putValue(Action.NAME,undoManager.getRedoPresentationName());

            }
            else {
                setEnabled(false);
                putValue(Action.NAME,"Redo");
            }
        }
    }

    public UndoTest()
    {
        BuildLayout();
    }

}

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。














站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3