Java 多个移动球:添加球有效,但删除球无效
我正在自学Java,在图形方面遇到了一些问题。我从StackOverflow中学到了很多!如果不是早期的帖子,我会发布得更早 以下是基本场景。我有Java 多个移动球:添加球有效,但删除球无效,java,swing,Java,Swing,我正在自学Java,在图形方面遇到了一些问题。我从StackOverflow中学到了很多!如果不是早期的帖子,我会发布得更早 以下是基本场景。我有JFrame,我被添加到Balls(这是另一个扩展JPanel的类)。球随机移动,一个球在矩形上移动。面板上有三个按钮:暂停、添加随机球和删除随机球。我在移除球时遇到问题。它提供了对ArrayList的并发访问的例外,我不知道如何解决这个问题 我还想将pause按钮改为pause/play,但不确定如何重新启动run方法,因为它不是静态方法 由于我是J
JFrame
,我被添加到Balls
(这是另一个扩展JPanel
的类)。球随机移动,一个球在矩形上移动。面板上有三个按钮:暂停、添加随机球和删除随机球。我在移除球时遇到问题。它提供了对ArrayList
的并发访问的例外,我不知道如何解决这个问题
我还想将pause按钮改为pause/play,但不确定如何重新启动run方法,因为它不是静态方法
由于我是Java新手,我可能犯了一些基本的错误,或者我的方法可能不正确。如有任何建议,将不胜感激。谢谢
代码如下:
球类
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JPanel;
public class Balls extends JPanel {
public static ArrayList<Balls> BallList = new ArrayList<Balls>();
public Balls(String n, Color col, String nm) {
mode = n;
ballcolor = col;
name = nm;
if (name != "dummy") {
BallList.add(this);
}
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
for (Balls ball : BallList) {
g.setColor(ball.ballcolor);
g.fillOval(ball.x, ball.y, 20, 20);
}
}
private int x, y;
private String name;
private static Boolean run = true;
public String mode;
public Color ballcolor;
private String DirectionR = "R";
private void getposition() {
if (mode.equals("Circle")) {
}
if (mode.equals("Rectangle")) {
if (x == 0) {
x = 150;
}
if (y == 0) {
y = 200;
}
if (x == 150 && y == 200) {
DirectionR = "R";
}
if (x == 350 && y == 200) {
DirectionR = "D";
}
if (x == 350 && y == 300) {
DirectionR = "L";
}
if (x == 150 && y == 300) {
DirectionR = "U";
}
if (DirectionR.equals("R")) {
x++;
}
if (DirectionR.equals("L")) {
x--;
}
if (DirectionR.equals("U")) {
y--;
}
if (DirectionR.equals("D")) {
y++;
}
}
if (mode.equals("Random")) {
x = (int) (Math.random() * 400);
y = (int) (Math.random() * 400);
}
}
public void run() {
while (run == true) {
for (Balls ball : BallList) {
ball.getposition();
}
try {
Thread.sleep(20);
} catch (InterruptedException e2) {
// TODO: handle exception
e2.printStackTrace();
}
repaint();
}
}
static class pauseframe implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (run.equals(true)) {
run = false;
}
else {
run = true;
}
}
}
static class addball implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Balls Randn = new Balls("Random", Color.GREEN, "NEW ADDED");
for (Balls ball : BallList) {
System.out.println(ball.name);
}
}
}
static class Delball implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for (Balls ball : BallList) {
System.out.println(ball.mode);
if (ball.mode.equals("Random")) {
BallList.remove(ball);
}
}
}
}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class Balls_test {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
Balls dummy = new Balls("Dummy", Color.BLACK, "dummy");
frame.setSize(500, 500);
frame.getContentPane().add(dummy);
frame.setBackground(Color.WHITE);
dummy.setBackground(Color.WHITE);
Balls Rect = new Balls("Rectangle", Color.RED, "Rect");
Balls Rand = new Balls("Random", Color.BLUE, "Rand");
Button pause = new Button("Pause");
ActionListener pauseall = new Balls.pauseframe();
pause.addActionListener(pauseall);
frame.getContentPane().add(BorderLayout.NORTH, pause);
Button addRandom = new Button("Add Random Ball");
ActionListener NewBall = new Balls.addball();
addRandom.addActionListener(NewBall);
frame.getContentPane().add(BorderLayout.SOUTH, addRandom);
Button DelRandom = new Button("Remove Random Ball");
ActionListener DelBall = new Balls.Delball();
DelRandom.addActionListener(DelBall);
frame.getContentPane().add(BorderLayout.WEST, DelRandom);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dummy.run();
}
}
在迭代代码中使用的
列表时,无法删除该项:
public static ArrayList<Balls> BallList = new ArrayList<Balls>();
for (Balls ball : BallList) {
System.out.println(ball.mode);
if (ball.mode.equals("Random")) {
BallList.remove(ball);
}
}
添加操作从不将球添加到球列表中,因此列表中没有“随机”球
if(name!=“dummy”){
不是String
比较在Java中的工作方式
迭代时不能从列表
中删除元素。可以使用同步
块来确保一次只有一个线程访问列表
,但使用迭代器
迭代列表
可能更容易
遵循Java命名约定。变量名不应以大写字符开头。有些名称区域正确,有些不正确!保持一致!+1我很难理解OP在没有“Random”时如何获得异常将球添加到列表中:P@MadProgrammer这是我的荣幸。谢谢。谢谢程序员。我通过得到“Random”的索引修复了代码ball,从循环中中断,然后在循环后删除它。这解决了问题。迭代器听起来也不错。还修复了字符串比较。顺便说一句,我在主方法执行时添加了random ball,所以当我单击random ball removing按钮时,random ball将出现在那里。
Iterator<Balls> it = BallList.iterator();
while(it.hasNext()) {
Balls ball = it.next();
System.out.println(ball.mode);
if (ball.mode.equals("Random")) {
it.remove();
}
}