buttontomove.java

来自「Java 入门书的源码」· Java 代码 · 共 33 行

JAVA
33
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Moves a ball when the user presses a
 * button. Modifying the actionPerformed method
 * to put the code in a loop will cause the applet 
 * to display only the ball's final position.
 */

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class ButtonToMove extends Applet 
                     implements ActionListener {
  private int x = 50, y = 50;
  private Button move;

  public void init() {
    move = new Button("Move");
    add(move);
    move.addActionListener(this);
  }    
  public void actionPerformed(ActionEvent event) {
      
      x+=9;
      y+=9;
      repaint();
  }
  public void paint(Graphics g) {
    g.fillOval(x,y,40,40);
  }
}

⌨️ 快捷键说明

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