📄 game.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -