我正在学习使用 java 线程,所以我决定制作一个简单的弹跳球程序。但是,程序显示了多个线程,但只有一个利用了窗口大小,其他球被限制在一个区域。
我尝试为每个球的 JPanel 和不同的布局设置大小,但这些布局不起作用。
弹跳球.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.*;
public class BouncingBall extends JFrame {
ArrayList<Ball> balls = new ArrayList<Ball>();
//GUI Elements
JLabel lblCount;
JButton btn= new JButton("Stop");
BouncingBall() {
// setDefaultLookAndFeelDecorated(true);
setTitle("BouncingBall");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
for (int i = 0; i < 5; i++) {
balls.add(new Ball());
}
setLayout(new FlowLayout());
setContentPane(balls.get(0));
balls.get(0).init();
for (Ball b : balls
) {
System.out.println(b.getHeight());
if (b != balls.get(0)) {
b.init();
balls.get(0).add(b);
}
}
this.add(btn,BorderLayout.SOUTH);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (Ball b :balls
) {
b.stopMoving();
}
}
});
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
for (Ball b :balls
) {
b.startMoving();
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new BouncingBall();
}
}
Ball.java
import javax.swing.*;
import java.awt.*;
public class Ball extends JPanel implements Runnable {
// Box height and width
int width;
int height;
// Ball Size
float radius = 5;
float diameter = radius * 2;
// Center of Call
float X = radius + 50;
float Y = radius + 20;
// Direction
float dx;
float dy;
//Vars
int count = 0;
float[] colorHSB = new float[3];
boolean moving = false;
//Thread
Thread t;
Ball() {
dx = (float) Math.random() * 10;
dy = (float) Math.random() * 10;
width = getWidth();
height = getHeight();
for (int i = 0; i < 3; i++) {
colorHSB[i] = (float) Math.random() * 255;
}
t = new Thread(this);
}
void init() {
t.start();
}
public void run() {
while (true) {
width = getWidth();
height = getHeight();
if (moving){
X = X + dx;
Y = Y + dy;
}
if (X - radius < 0) {
dx = -dx;
X = radius;
addCount();
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
addCount();
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
addCount();
} else if ((Y + radius) > height) {
dy = -dy;
Y = height - radius;
addCount();
}
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
}
public void startMoving() {
moving = true;
}
public void stopMoving(){
moving=false;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.getHSBColor(colorHSB[0], colorHSB[1], colorHSB[2]));
g.fillOval((int) (X - radius), (int) (Y - radius), (int) diameter, (int) diameter);
}
public void addCount() {
count++;
System.out.println(count);
}
}
程序运行的照片
它应该利用整个窗口显示所有在框架周围弹跳的球。