JViewport: Move and View : 滚动面板 « 图形用户界面 « 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 » 图形用户界面 » 滚动面板屏幕截图 
JViewport: Move and View
JViewport: Move and View
 
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JViewport;
import javax.swing.KeyStroke;

public class MoveViewSample {

  public static final int INCREASE = 0// direction

  public static final int DECREASE = 1// direction

  public static final int X_AXIS = 0// axis

  public static final int Y_AXIS = 1// axis

  public static final int UNIT = 0// type

  public static final int BLOCK = 1// type

  static class MoveAction extends AbstractAction {
    JViewport viewport;

    int direction;

    int axis;

    int type;

    public MoveAction(JViewport viewport, int direction, int axis, int type) {
      if (viewport == null) {
        throw new IllegalArgumentException(
            "null viewport not permitted");
      }
      this.viewport = viewport;
      this.direction = direction;
      this.axis = axis;
      this.type = type;
    }

    public void actionPerformed(ActionEvent actionEvent) {
      Dimension extentSize = viewport.getExtentSize();
      int horizontalMoveSize = 0;
      int verticalMoveSize = 0;
      if (axis == X_AXIS) {
        if (type == UNIT) {
          horizontalMoveSize = 1;
        else // type == BLOCK
          horizontalMoveSize = extentSize.width;
        }
      else // axis == Y_AXIS
        if (type == UNIT) {
          verticalMoveSize = 1;
        else // type == BLOCK
          verticalMoveSize = extentSize.height;
        }
      }
      if (direction == DECREASE) {
        horizontalMoveSize = -horizontalMoveSize;
        verticalMoveSize = -verticalMoveSize;
      }
      // Translate origin by some amount
      Point origin = viewport.getViewPosition();
      origin.x += horizontalMoveSize;
      origin.y += verticalMoveSize;
      // set new viewing origin
      viewport.setViewPosition(origin);
    }
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("JViewport Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Icon icon = new ImageIcon("dog.jpg");
    JLabel dogLabel = new JLabel(icon);
    JViewport viewport = new JViewport();
    viewport.setView(dogLabel);

    InputMap inputMap = viewport
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = viewport.getActionMap();

    // Up key moves view up unit
    Action upKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, UNIT);
    KeyStroke upKey = KeyStroke.getKeyStroke("UP");
    inputMap.put(upKey, "up");
    actionMap.put("up", upKeyAction);

    // Down key moves view down unit
    Action downKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, UNIT);
    KeyStroke downKey = KeyStroke.getKeyStroke("DOWN");
    inputMap.put(downKey, "down");
    actionMap.put("down", downKeyAction);

    // Left key moves view left unit
    Action leftKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, UNIT);
    KeyStroke leftKey = KeyStroke.getKeyStroke("LEFT");
    inputMap.put(leftKey, "left");
    actionMap.put("left", leftKeyAction);

    // Right key moves view right unit
    Action rightKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, UNIT);
    KeyStroke rightKey = KeyStroke.getKeyStroke("RIGHT");
    inputMap.put(rightKey, "right");
    actionMap.put("right", rightKeyAction);

    // PgUp key moves view up block
    Action pgUpKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, BLOCK);
    KeyStroke pgUpKey = KeyStroke.getKeyStroke("PAGE_UP");
    inputMap.put(pgUpKey, "pgUp");
    actionMap.put("pgUp", pgUpKeyAction);

    // PgDn key moves view down block
    Action pgDnKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, BLOCK);
    KeyStroke pgDnKey = KeyStroke.getKeyStroke("PAGE_DOWN");
    inputMap.put(pgDnKey, "pgDn");
    actionMap.put("pgDn", pgDnKeyAction);

    // Shift-PgUp key moves view left block
    Action shiftPgUpKeyAction = new MoveAction(viewport, DECREASE, X_AXIS,
        BLOCK);
    KeyStroke shiftPgUpKey = KeyStroke.getKeyStroke("shift PAGE_UP");
    inputMap.put(shiftPgUpKey, "shiftPgUp");
    actionMap.put("shiftPgUp", shiftPgUpKeyAction);

    // Shift-PgDn key moves view right block
    Action shiftPgDnKeyAction = new MoveAction(viewport, INCREASE, X_AXIS,
        BLOCK);
    KeyStroke shiftPgDnKey = KeyStroke.getKeyStroke("shift PAGE_DOWN");
    inputMap.put(shiftPgDnKey, "shiftPgDn");
    actionMap.put("shiftPgDn", shiftPgDnKeyAction);

    Container contentPane = frame.getContentPane();
    contentPane.add(viewport, BorderLayout.CENTER);
    frame.setSize(300200);
    frame.setVisible(true);
  }
}

           
         
  
Related examples in the same category
1. 创建一个JScrollPane容器
2. 创建一个滚动列表
3. 在JScrollPane滚动条控制在JScrollPane滚动条控制
4. ScrollPane Sample
5. 滚动窗格尺子滚动窗格尺子
6. 定制滚动控件定制滚动控件
7. 滚动控件与形象滚动控件与形象
8. 使用程序设置滚动值使用程序设置滚动值
9. JScrollPane和JList组件JScrollPane和JList组件
10. JScrollPane行和列标题JScrollPane行和列标题
11. 一个简单的JScrollPane示范一个简单的JScrollPane示范
12. 水印JScrollPane
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.