将一组 JComponent 扩展对象添加到 JFrame
Adding an array of JComponent extended objects to a JFrame
我从 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 的正确方法。
谢谢,抱歉我的英语不好,我已经尽力把它写出来了。
在您的情况下,您不能使用布局管理器,需要将其重置为空。请参阅下面的我的代码
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
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);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// usually you should use a normal layout manager, but for your task we need null
getContentPane().setLayout(null);
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]);
}
}
public class Star extends JComponent {
private final Color color = Color.YELLOW;
public Star(int x, int y) {
// need to set the correct coordinates
setBounds(x, y, 8, 8);
}
@Override
public void paintComponent(Graphics g) {
g.setColor(color);
g.fillOval(0, 0, getWidth(), getHeight());
}
}
}
我从 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 的正确方法。
谢谢,抱歉我的英语不好,我已经尽力把它写出来了。
在您的情况下,您不能使用布局管理器,需要将其重置为空。请参阅下面的我的代码
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
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);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// usually you should use a normal layout manager, but for your task we need null
getContentPane().setLayout(null);
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]);
}
}
public class Star extends JComponent {
private final Color color = Color.YELLOW;
public Star(int x, int y) {
// need to set the correct coordinates
setBounds(x, y, 8, 8);
}
@Override
public void paintComponent(Graphics g) {
g.setColor(color);
g.fillOval(0, 0, getWidth(), getHeight());
}
}
}