game.java

来自「学习java语言」· Java 代码 · 共 45 行

JAVA
45
字号
import java.awt.*;
import java.awt.event.*;
class Mywin extends Frame implements ActionListener {
  Button button;
  Label label;
  Mywin() {
    setLayout(null);
    Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    setCursor(c);
    button = new Button("横向走动");
    button.setBackground(Color.pink);
    button.addActionListener(this);
    label = new Label("我可以被碰掉",Label.CENTER);
    label.setBackground(Color.yellow);
    button.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    add(button);
    add(label);
    button.setBounds(20,80,80,20);
    label.setBounds(150,80,80,20);
    setVisible(true);
    setBounds(100,120,300,300);
    validate();
  }
  public void actionPerformed(ActionEvent e) {
    Rectangle rect = button.getBounds();
    int x = (int)rect.getX();
    int y = (int)rect.getY();
    if(rect.intersects(label.getBounds()))
      label.setVisible(false);
    if(label.isVisible()) {
       x = x + 2;
       button.setLocation(x,y);
    }
    else {
       y = y + 3;
       button.setLocation(x,y);
       button.setLabel("纵向走动");
    }
  }
}
public class Game {
  public static void main(String[] args) {
    new Mywin();
  }
}                 

⌨️ 快捷键说明

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