`

javax.swing.undo类 使用实例 - 转载

    博客分类:
  • java
阅读更多
import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.event.UndoableEditEvent; 
import javax.swing.event.UndoableEditListener; 
import javax.swing.undo.CannotRedoException; 
import javax.swing.undo.UndoManager; 

public class UndoRedoTextArea extends JFrame { 
 protected JTextArea textArea = new JTextArea(); 

 protected UndoManager undoManager = new UndoManager(); 

 protected JButton undoButton = new JButton("Undo"); 

 protected JButton redoButton = new JButton("Redo"); 

 public UndoRedoTextArea() { 
   super("Undo/Redo Demo"); 

   undoButton.setEnabled(false); 
   redoButton.setEnabled(false); 

   JPanel buttonPanel = new JPanel(new GridLayout()); 
   buttonPanel.add(undoButton); 
   buttonPanel.add(redoButton); 

   JScrollPane scroller = new JScrollPane(textArea); 

   getContentPane().add(buttonPanel, BorderLayout.NORTH); 
   getContentPane().add(scroller, BorderLayout.CENTER); 

   textArea.getDocument().addUndoableEditListener( 
       new UndoableEditListener() { 
         public void undoableEditHappened(UndoableEditEvent e) { 
           undoManager.addEdit(e.getEdit()); 
           updateButtons(); 
         } 
       }); 

   undoButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
       try { 
         undoManager.undo(); 
       } catch (CannotRedoException cre) { 
         cre.printStackTrace(); 
       } 
       updateButtons(); 
     } 
   }); 

   redoButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
       try { 
         undoManager.redo(); 
       } catch (CannotRedoException cre) { 
         cre.printStackTrace(); 
       } 
       updateButtons(); 
     } 
   }); 

   setSize(400, 300); 
   setVisible(true); 
 } 

 public void updateButtons() { 
   undoButton.setText(undoManager.getUndoPresentationName()); 
   redoButton.setText(undoManager.getRedoPresentationName()); 
   undoButton.setEnabled(undoManager.canUndo()); 
   redoButton.setEnabled(undoManager.canRedo()); 
 } 

 public static void main(String argv[]) { 
   new UndoRedoTextArea(); 
 } 
}   


分享到:
评论

相关推荐

    javax.ws.rs-api-2.0.1-API文档-中文版.zip

    赠送jar包:javax.ws.rs-api-2.0.1.jar; 赠送原API文档:javax.ws.rs-api-2.0.1-javadoc.jar; 赠送源代码:javax.ws.rs-api-2.0.1-sources.jar; 赠送Maven依赖信息文件:javax.ws.rs-api-2.0.1.pom; 包含翻译后...

    javax.servlet jar包---解决找不到javax.servlet.*等问题

    - 在传统的Java Web项目中,通常将`javax.servlet.jar`和`javax.servlet.jsp.jar`添加到项目的类路径中,这可以通过在`WEB-INF/lib`目录下放置这两个jar包实现。 - 使用Maven或Gradle等构建工具时,可以在pom.xml...

    javax.servlet-api-3.1.0.jar javax.el-api-2.2.4.jar javax.el-2.2.4.jar

    `javax.servlet-api-3.1.0.jar`是Servlet 3.1规范的API接口定义,它定义了Web应用如何与Web服务器交互的一系列接口和类。Servlet 3.1引入了一些新特性,如异步处理、注解驱动的Servlet和过滤器配置、支持WebSocket...

    javax.ws.rs-api-2.0-API文档-中文版.zip

    赠送jar包:javax.ws.rs-api-2.0.jar; 赠送原API文档:javax.ws.rs-api-2.0-javadoc.jar; 赠送源代码:javax.ws.rs-api-2.0-sources.jar; 赠送Maven依赖信息文件:javax.ws.rs-api-2.0.pom; 包含翻译后的API文档...

    javax.servlet-api-4.0.1-API文档-中英对照版.zip

    赠送jar包:javax.servlet-api-4.0.1.jar; 赠送原API文档:javax.servlet-api-4.0.1-javadoc.jar; 赠送源代码:javax.servlet-api-4.0.1-sources.jar; 赠送Maven依赖信息文件:javax.servlet-api-4.0.1.pom; ...

    javax.annotation-api-1.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...

    javax.annotation-api-1.3.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-...

    javax.transaction-api-1.2-API文档-中文版.zip

    赠送jar包:javax.transaction-api-1.2.jar; 赠送原API文档:javax.transaction-api-1.2-javadoc.jar; 赠送源代码:javax.transaction-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.transaction-api-1.2....

    javax.websocket-api-1.1-API文档-中文版.zip

    赠送jar包:javax.websocket-api-1.1.jar; 赠送原API文档:javax.websocket-api-1.1-javadoc.jar; 赠送源代码:javax.websocket-api-1.1-sources.jar; 赠送Maven依赖信息文件:javax.websocket-api-1.1.pom; ...

    javax.servlet-3.0.0.v201112011016-API文档-中文版.zip

    赠送jar包:javax.servlet-3.0.0.v201112011016.jar; 赠送原API文档:javax.servlet-3.0.0.v201112011016-javadoc.jar; 赠送源代码:javax.servlet-3.0.0.v201112011016-sources.jar; 赠送Maven依赖信息文件:...

    javax.servlet.jsp.jstl-api-1.2.1.jar

    javax.servlet.jsp.jstl-api-1.2.1.jar

    javax.activation-api-1.2.0-API文档-中文版.zip

    赠送jar包:javax.activation-api-1.2.0.jar; 赠送原API文档:javax.activation-api-1.2.0-javadoc.jar; 赠送源代码:javax.activation-api-1.2.0-sources.jar; 赠送Maven依赖信息文件:javax.activation-api-...

    javax.activation-1.2.0-API文档-中文版.zip

    赠送jar包:javax.activation-1.2.0.jar; 赠送原API文档:javax.activation-1.2.0-javadoc.jar; 赠送源代码:javax.activation-1.2.0-sources.jar; 赠送Maven依赖信息文件:javax.activation-1.2.0.pom; 包含...

    javax.mail-1.6.2-API文档-中英对照版.zip

    赠送jar包:javax.mail-1.6.2.jar; 赠送原API文档:javax.mail-1.6.2-javadoc.jar; 赠送源代码:javax.mail-1.6.2-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.6.2.pom; 包含翻译后的API文档:javax.mail...

    javax.mail-1.5.6-API文档-中文版.zip

    赠送jar包:javax.mail-1.5.6.jar; 赠送原API文档:javax.mail-1.5.6-javadoc.jar; 赠送源代码:javax.mail-1.5.6-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.5.6.pom; 包含翻译后的API文档:javax.mail...

    javax.servlet-api-4.0.0-API文档-中文版.zip

    赠送jar包:javax.servlet-api-4.0.0.jar; 赠送原API文档:javax.servlet-api-4.0.0-javadoc.jar; 赠送源代码:javax.servlet-api-4.0.0-sources.jar; 赠送Maven依赖信息文件:javax.servlet-api-4.0.0.pom; ...

    javax.json-1.0.4-API文档-中文版.zip

    赠送jar包:javax.json-1.0.4.jar; 赠送原API文档:javax.json-1.0.4-javadoc.jar; 赠送源代码:javax.json-1.0.4-sources.jar; 赠送Maven依赖信息文件:javax.json-1.0.4.pom; 包含翻译后的API文档:javax.json...

    javax.servlet-api-3.1.0.jar中文文档.zip

    api-***.jar中文文档.zip,java,javax.servlet-api-***.jar,javax.servlet,javax.servlet-api,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,javax,servlet,api,中文API文档,手册,开发手册,使用手册,...

    javax.servlet-3.0.0.v201112011016-API文档-中英对照版.zip

    赠送jar包:javax.servlet-3.0.0.v201112011016.jar; 赠送原API文档:javax.servlet-3.0.0.v201112011016-javadoc.jar; 赠送源代码:javax.servlet-3.0.0.v201112011016-sources.jar; 赠送Maven依赖信息文件:...

    javax.ws.rs-api-2.0.1-API文档-中英对照版.zip

    赠送jar包:javax.ws.rs-api-2.0.1.jar; 赠送原API文档:javax.ws.rs-api-2.0.1-javadoc.jar; 赠送源代码:javax.ws.rs-api-2.0.1-sources.jar; 赠送Maven依赖信息文件:javax.ws.rs-api-2.0.1.pom; 包含翻译后...

Global site tag (gtag.js) - Google Analytics