撤消范例1 : 撤消重做 « 图形用户界面 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 图形用户界面 » 撤消重做屏幕截图 
撤消范例1
撤消范例1
 
/*
Core SWING Advanced Programming 
By Kim Topley
ISBN: 0 13 083292 8       
Publisher: Prentice Hall  
*/


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

public class UndoExample1 extends JFrame {
  public UndoExample1() {
    super("Undo/Redo Example 1");
  
    pane = new JTextPane();
    pane.setEditable(true);    // Editable
    getContentPane().add(new JScrollPane(pane)"Center");

    // Add a menu bar
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    // Populate the menu bar
    createMenuBar();
  }

  public void createMenuBar() {
    // Remove the existing menu items
    int count = menuBar.getMenuCount();
    for (int i = 0; i < count; i++) {
      menuBar.remove(menuBar.getMenu(0));
    }

    // Build the new menu.
    Action[] actions = pane.getActions();
    Hashtable actionHash = new Hashtable();
    count = actions.length;
    for (int i = 0; i < count; i++) {
      actionHash.put(actions[i].getValue(Action.NAME), actions[i]);
    }

    // Add the font menu
    JMenu menu = MenuBuilder.buildMenu("Font", fontSpec, actionHash);
    if (menu != null) {
      menuBar.add(menu);
    }

    // Add the alignment menu
    menu = MenuBuilder.buildMenu("Align", alignSpec, actionHash);
    if (menu != null) {
      menuBar.add(menu);
    }
  }

  public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    catch (Exception evt) {}
  
    JFrame f = new UndoExample1();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) {
        System.exit(0);
      }
    });
    f.setSize(250300);
    f.setVisible(true);

    // Create and show a frame monitoring undoable edits
    JFrame undoMonitor = new JFrame("Undo Monitor");
    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    undoMonitor.getContentPane().add(new JScrollPane(textArea));
    undoMonitor.setBounds(f.getLocation().x + f.getSize().width, 
              f.getLocation().y, 400200);
    undoMonitor.setVisible(true);

    pane.getDocument().addUndoableEditListener(new UndoableEditListener() {
      public void undoableEditHappened(UndoableEditEvent evt) {
        UndoableEdit edit = evt.getEdit();
        textArea.append(edit.getPresentationName() "(" +
                edit.toString() ")\n");
      }
    });
    
    // Create and show a frame monitoring document edits
    JFrame editMonitor = new JFrame("Edit Monitor");
    final JTextArea textArea2 = new JTextArea();
    textArea2.setEditable(false);
    editMonitor.getContentPane().add(new JScrollPane(textArea2));
    editMonitor.setBounds(undoMonitor.getLocation().x, 
          undoMonitor.getLocation().y + undoMonitor.getSize().height,
          400200);
    editMonitor.setVisible(true);

    pane.getDocument().addDocumentListener(new DocumentListener() {
      public void changedUpdate(DocumentEvent evt) {
        textArea2.append("Attribute change\n");
      }
      public void insertUpdate(DocumentEvent evt) {
        textArea2.append("Text insertion\n");
      }
      public void removeUpdate(DocumentEvent evt) {
        textArea2.append("Text removal\n");
      }
    });
  }

  private static JTextPane pane;
  private static JMenuBar menuBar;

  private static MenuSpec[] sizeSpec = new MenuSpec[] {
      new MenuSpec("Size 8",  "font-size-8"),
      new MenuSpec("Size 10""font-size-10"),
      new MenuSpec("Size 12""font-size-12"),
      new MenuSpec("Size 14""font-size-14"),
      new MenuSpec("Size 16""font-size-16"),
      new MenuSpec("Size 18""font-size-18"),
      new MenuSpec("Size 24""font-size-24"),
      new MenuSpec("Size 36""font-size-36"),
      new MenuSpec("Size 48""font-size-48")          
  };

  private static MenuSpec[] familySpec = new MenuSpec[] {
      new MenuSpec("Sans Serif""font-family-SansSerif"),
      new MenuSpec("Monospaced""font-family-Monospaced"),
      new MenuSpec("Serif""font-family-Serif")
  };

  private static MenuSpec[] styleSpec = new MenuSpec[] {
      new MenuSpec("Bold""font-bold"),
      new MenuSpec("Italics""font-italic"),
      new MenuSpec("Underline""font-underline")
  };

  // Menu definitions for fonts
  private static MenuSpec[] fontSpec = new MenuSpec[] {
      new MenuSpec("Size", sizeSpec),
      new MenuSpec("Family", familySpec),
      new MenuSpec("Style", styleSpec)
  };

  // Alignment
  private static MenuSpec[] alignSpec = new MenuSpec[] {
      new MenuSpec("Left""left-justify"),
      new MenuSpec("Center""center-justify"),
      new MenuSpec("Right""right-justify")
  };
}


class MenuSpec {
  public MenuSpec(String name, MenuSpec[] subMenus) {
    this.name = name;
    this.subMenus = subMenus;
  }

  public MenuSpec(String name, String actionName) {
    this.name = name;
    this.actionName = actionName;
  }

  public MenuSpec(String name, Action action) {
    this.name = name;
    this.action = action;
  }

  public boolean isSubMenu() {
    return subMenus != null;
  }

  public boolean isAction() {
    return action != null;
  }

  public String getName() {
    return name;
  }

  public MenuSpec[] getSubMenus() {
    return subMenus;
  }

  public String getActionName() {
    return actionName;
  }

  public Action getAction() {
    return action;
  }

  private String name;

  private String actionName;

  private Action action;

  private MenuSpec[] subMenus;
}


class MenuBuilder {
  public static JMenu buildMenu(String name, MenuSpec[] menuSpecs,
      Hashtable actions) {
    int count = menuSpecs.length;

    JMenu menu = new JMenu(name);
    for (int i = 0; i < count; i++) {
      MenuSpec spec = menuSpecs[i];
      if (spec.isSubMenu()) {
        // Recurse to handle a sub menu
        JMenu subMenu = buildMenu(spec.getName(), spec.getSubMenus(),
            actions);
        if (subMenu != null) {
          menu.add(subMenu);
        }
      else if (spec.isAction()) {
        // It's an Action - add it directly to the menu
        menu.add(spec.getAction());
      else {
        // It's an action name - add it if possible
        String actionName = spec.getActionName();
        Action targetAction = (Actionactions.get(actionName);

        // Create the menu item
        JMenuItem menuItem = menu.add(spec.getName());
        if (targetAction != null) {
          // The editor kit knows the action
          menuItem.addActionListener(targetAction);
        else {
          // Action not known - disable the menu item
          menuItem.setEnabled(false);
        }
      }
    }

    // Return null if nothing was added to the menu.
    if (menu.getMenuComponentCount() == 0) {
      menu = null;
    }

    return menu;
  }
}



           
         
  
Related examples in the same category
1. 使用UndoableToggleEdit使用UndoableToggleEdit
2. 使用StateEdit使用StateEdit
3. 使用UndoManager使用UndoManager
4. A sample app showing the use of UndoableToggleEdit and CompoundEditA sample app showing the use of UndoableToggleEdit and CompoundEdit
5. UndoManager细节UndoManager细节
6. 文本控件撤消重做文本控件撤消重做
7. 撤消重载管理撤消重载管理
8. 简单图形用户界面演示UndoManager简单图形用户界面演示UndoManager
9. 撤消示例2撤消示例2
10. 撤消示例3撤消示例3
11. 撤消范例4撤消范例4
12. 撤消例5撤消例5
13. 撤消范例6撤消范例6
14. Undoable绘制Undoable绘制
15. 撤消制图撤消制图
16. 撤消范例7撤消范例7
17. 创建文本与撤消,重做功能
18. 撤消和重做,添加到文本组成部分
19. 新增撤消支持StyleFrame新增撤消支持StyleFrame
20. 撤消和重做一个文本组件
21. Create a redo action and add it to the text component (JTextComponent)
22. 聆听撤消和重做事件
23. 创建一个撤消的行动,并将其添加到文本控件
24. Bind the undo action to ctl-Z
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.