compassscroller.java

来自「java swing 开发代码」· Java 代码 · 共 54 行

JAVA
54
字号
// CompassScroller.java// A simple ActionListener that can move the view of a viewport// north, south, east and west by specified units.//package	jswing.ch11;import java.awt.*;import java.awt.event.*;import javax.swing.JViewport;public class CompassScroller implements ActionListener {  public static final String NORTH = "North";  public static final String SOUTH = "South";  public static final String EAST  = "East";  public static final String WEST  = "West";  private JViewport viewport;  private Point p;  public CompassScroller(JViewport viewport) {    this.viewport = viewport;    p = new Point();  }  public void actionPerformed(ActionEvent ae) {    Dimension dv = viewport.getViewSize();    Dimension de = viewport.getExtentSize();    String command = ae.getActionCommand();    if (command == NORTH) {      if (p.y > 9) {	p.y -= 10;      }    }    else if (command == SOUTH) {      if (p.y + de.height < dv.height) {	p.y += 10;      }    }    else if (command == EAST) {      if (p.x + de.width < dv.width) {	p.x += 10;      }    }    else if (command == WEST) {      if (p.x > 9) {	p.x -= 10;      }    }    viewport.setViewPosition(p);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?