代码之家  ›  专栏  ›  技术社区  ›  Juan Francisco Garcia

将JComponent扩展对象数组添加到JFrame

  •  1
  • Juan Francisco Garcia  · 技术社区  · 8 年前

    我从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]);
            }       
        }
    }
    

    问题是它只打印一颗星,我认为这是最后一颗星,coords的工作方式就像他们应该做的那样,所以我认为错误在于JComponent或JFrame实现,我在自学,所以我的代码可能不是使用swing的正确方式。

    谢谢你,对我的英语很抱歉,我已经尽我所能写了。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sergiy Medvynskyy    8 年前

    在您的情况下,无法使用布局管理器,需要将其重置为null。请参见下面的“我的代码”

    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());
            }
        }
    }