driver.java

来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 50 行

JAVA
50
字号
import javax.swing.*;import java.awt.*;/** Program to animate a message moving side to side in a window  (Figure 8.38) *  Author: David Riley *  Date: January, 2005  */public class Driver  {	private JFrame window;	private Label fishyMsg;	private MsgCallbackTimer clock;	private int velocity;	/**	post:	window != null 	 *				and fishyMsg is placed at the left edge of window	 *				and velocity = 2	 *				and clock is scheduled for 1/50 sec. repeated events 	 */	public Driver()   {		window = new JFrame("Animated Message");		window.setBounds(10, 10, 400, 200);		window.setVisible(true);		window.setLayout(null);		fishyMsg = new Label("FISH");		fishyMsg.setBounds(0, 70, 50, 30);		fishyMsg.setForeground(Color.black);		fishyMsg.setBackground(Color.blue);		window.add(fishyMsg, 0); 		window.repaint();		velocity = 1;		clock = new MsgCallbackTimer(20, this);		clock.start();	}	/**	pre:	fishyMsg != null  <br>	 *		post:	(either the left or right edge of fishyMsg@pre lies	 *				outside the boundaries of pane)  	 *  						implies velocity == - velocity@pre	 *				and  fishyMsg.getX() == fishyMsg.getX()@pre + velocity 	 */	public void moveMsg( )   {		if (fishyMsg.getX() < 0			|| fishyMsg.getX()+fishyMsg.getWidth() > window.getWidth())		{			velocity = -velocity;		}		fishyMsg.setLocation( fishyMsg.getX()+velocity, fishyMsg.getY() );	}}

⌨️ 快捷键说明

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