在GUI中怎样实现小球移动

来源:百度知道 编辑:UC知道 时间:2024/05/27 21:21:59
要求上下左右移动求个全代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestBall extends JFrame implements ActionListener,Runnable{
JButton jb1;
boolean flag;
public TestBall(){
this.dispose();
jb1 = new JButton("啊");

JButton jb2 = new JButton("开始");
JButton jb3 = new JButton("结束");

jb1.setBounds(0,0,60,20);
jb2.setBounds(50, 220, 60, 20);
jb3.setBounds(120, 220, 60, 20);

jb2.addActionListener(this);
jb3.addActionListener(this);

this.setLayout(null);
this.add(jb1);
this.add(jb2);
this.add(jb3);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setVisible(true);
}

/**
* @param args
*/
public static void main(Str

import java.awt.*;

import java.awt.event.*;

public class BallDemo extends Frame implements Runnable {
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
Image img = null;
public BallDemo() {
super("小球撞墙");
setSize(WIDTH, HEIGHT);
setResizable(false);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/*

* 4 右下 3 左下 2 左上 1 右上

*/

public void run() {
int direction = 4;
while (true) {
if (x <