【发布时间】:2018-04-11 09:39:56
【问题描述】:
我从 JFrame 开始,我正在尝试制作一个 StarField,目前我正在将 Star JComponent 添加到 Starfield JFrame:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
public class Star extends JComponent{
public int x;
public int y;
private final Color color = Color.YELLOW;
public Star(int x, int y) {
this.x = x;
this.y = y;
}
public void paintComponent(Graphics g) {
g.setColor(color);
g.fillOval(x, y, 8, 8);
}
}
和 StarField 代码:
import javax.swing.*;
public class StarField extends JFrame{
public int size = 400;
public Star[] stars = new Star[50];
public static void main(String[] args) {
StarField field = new StarField();
field.setVisible(true);
}
public StarField() {
this.setSize(size, size);
for (int i= 0; i< stars.length; i++) {
int x = (int)(Math.random()*size);
int y = (int)(Math.random()*size);
stars[i] = new Star(x,y);
this.add(stars[i]);
}
}
}
问题在于它只打印一颗星,我认为它是最后一颗星,坐标工作正常,所以我认为错误出在 JComponent 或 JFrame 实现中,我自己-学习,所以也许我的代码不是使用swing的正确方法。
谢谢你,对不起我的英语,我已经尽力写了我所知道的。
【问题讨论】:
-
最好看看
LayoutManager。如果我没记错的话,默认的Layout是BorderLayout,它允许在 5 个不同的位置总共有 5 个组件。this.add(stars[i]);覆盖之前添加的每个组件,因为您只能在BorderLayout.CENTER处拥有一个组件(这是默认设置,因此会神奇地附加到您的add调用中,因此它实际上是this.add(stars[i], BorderLayout.CENTER);) -
对 OP 改进自定义组件的建议:1) 不要对颜色进行硬编码。相反,您可以使用 setForeground(...) 方法为椭圆设置颜色,然后在绘制时使用 getForeground()。然后每个椭圆可以是不同的颜色。 2)不要硬编码大小。将此作为参数。然后每个椭圆可以是不同的大小。 3) 实现
getPreferredSize()方法。这将基于 2 中建议的参数。布局管理器使用它来确定组件的大小。
标签: java swing jframe jcomponent