嘿,大家我有一個按鈕,應該添加一個新的球到我設置的ArrayList。與其添加一個新球,它只是加速我已經有的球。 這CreateCircle創建球:使用ActionListener添加新元素
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class CreateCircle extends JPanel {
/* Ports JFrame width, height from BouncingDrawFrame */
static double c, d;
/* Ports desired size of Circle */
static int r = 20; // Initial Value 20
/* Ports timer delay from BouncingDrawFrame */
static int z = 10; // Initial Value 10
/* Timer to control speed */
static Timer t = new Timer(z, null);
/* X,Y points to start, speed of movement */
static double x, y, velX = 1, velY = 1;
/* Ports color choice from BouncingDrawFrame */
static Color myColor;
public CreateCircle(int a, int b) {
/* Height of Frame */
c = a;
/* Width of Frame */
d = b;
t.start();
t.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/* "Bounces" the Ball off the sides */
if (x < 0 || x > (d - (r + 2))) {
velX = -velX;
}
/* "Bounces" the Ball off the top and bottom */
if (y < 0 || y > (c - (r + 30))) {
velY = -velY;
}
/* Moves ball 2 pixels each timer action */
x += velX;
y += velY;
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x, y, r, r);
g2.setColor(myColor);
g2.fill(circle);
}
}
雖然這是處理按鈕類,如果點擊創建一個新的球:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class BouncingDrawFrame extends JFrame {
public BouncingDrawFrame() {
/*
* Create ArrayList to hold balls, remember ArrayList is not a component
* but the elements of ArrayList are
*/
final ArrayList<CreateCircle> ballList = (ArrayList<CreateCircle>) new ArrayList<CreateCircle>();
/* Create Main Ball Frame */
final JFrame main = new JFrame();
main.setTitle("Bouncing Balls!!");
main.setSize(350, 500);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setLocationRelativeTo(null);
main.setVisible(true);
/* Create Control Panel */
JFrame control = new JFrame();
control.setSize(300, 300);
control.setTitle("Change Settings!");
control.setLocationRelativeTo(null);
control.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
control.setVisible(true);
final JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(10, 2));
control.add(p1);
final JButton addBall = new JButton("Add A Ball");
p1.add(addBall);
/* Y Point */
final int a = main.getHeight();
/* X Point */
final int b = main.getWidth();
ballList.add(new CreateCircle(a, b));
main.add(ballList.get(0));
addBall.addActionListener(new ActionListener() {
private int click;
public void actionPerformed(ActionEvent arg0) {
click++;
ballList.add((click), new CreateCircle(a, b));
System.out.println(click);
System.out.println(ballList.size());
main.add(ballList.get(click));
repaint();
}
});
}
}
不是創建一個新球,點擊按鈕即可加速第一個球的運動。我正在嘗試向ArrayList的索引添加一個新球,該索引等於所點擊的數量。我有ArrayList的大小和點擊數量輸出到系統,所以我知道ArrayList的大小隨着點擊次數的增加而增加。我只是不知道爲什麼它不添加新的CreateCircle。
PS:這裏是主線程。
public class BouncingRun {
public static void main(String[] args) {
new BouncingDrawFrame();
}
}
你有一個重要的過度使用靜態修飾符。 **編輯** - 正如我現在看到的那樣,Jason C在回答中說。 1+給他的回答。 –