简体   繁体   English

如何向JComponent添加多个对象?

[英]How to add multiple objects to JComponent?

This is my tester class: 这是我的测试类:

public void start() {
    // We do our drawing here       
    JFrame frame = new JFrame("Animation");
    frame.setSize(800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));


    frame.setVisible(true);
}

Shape1 class: Shape1类:

public class Shape1 extends JComponent{
    protected double x, y, r;
    protected double height, width;
    protected Color col;
    protected int counter;

    public Shape1(double x, double y, double r) {
        this.x = x - 2*r;
        this.y = y - r;
        this.r = r;
        this.width = 4*r;
        this.height = 2*r;

        this.col = new Color((int)(Math.random() * 0x1000000));
    }

    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        draw(g2);
    }

    public void draw(Graphics2D g2){
        Ellipse2D.Double face = new Ellipse2D.Double(this.x, this.y, this.width, this.height);

        g2.setColor(this.col);
        g2.fill(face);
    }
}

I'm instantiating the Shape1 class 3 times and adding them to the frame. 我将Shape1类实例化3次并将它们添加到框架中。 But the shape is drawn only once, how can I draw it 3 times? 但是形状只绘制一次,我怎么画3次呢?

You can try to use a loop: 您可以尝试使用循环:

List<Shape1> shapes = new ArrayList<>();

@Override
protected void paintComponent(Graphics g) {
    super.paintCompoent(g);
    for (Shape1 s : shapes) {
        s.draw(g);
    }
}

JFrame is using a BorderLayout by default, which means only the last component is placed in the default/ CENTER position. JFrame默认使用BorderLayout ,这意味着只有最后一个组件位于默认/ CENTER位置。

Start by changing the layout manager. 首先更改布局管理器。

public void start() {
    // We do our drawing here       
    JFrame frame = new JFrame("Animation");
    frame.setSize(800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(1, 3));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));
    frame.add(new Shape1(getRandom(WIDTH), getRandom(HEIGHT), objRadius));


    frame.setVisible(true);
}

Take a look at Laying Out Components Within a Container for more details 有关更多详细信息,请查看在容器布置组件

This assumes, of course, that you want to keep each instance of your shape in it's own component. 当然,这假设您希望将形状的每个实例保留在其自己的组件中。 If you want the shapes to interact in some way or overlap, you'd be better of creating a JComponent which could paint different shapes itself. 如果您希望形状以某种方式交互或重叠,您最好创建一个可以自己绘制不同形状的JComponent

Have a look at 2D Graphics for more ideas 查看2D Graphics以获得更多创意

Also, you should be calling super.paintComponent(g) before you do any custom painting 此外,在进行任何自定义绘制之前,您应该调用super.paintComponent(g)

Have a look at Painting in AWT and Swing and Performing Custom Painting for more details 有关更多详细信息,请参阅AWT和Swing中的 绘画以及执行自定义绘画

I'm instantiating the Shape1 class 3 times and adding them to the frame. 我将Shape1类实例化3次并将它们添加到框架中。 But the shape is drawn only once, how can I draw it 3 times? 但是形状只绘制一次,我怎么画3次呢?

If you want to position components randomly in a panel (ie the content pane of the JFrame in your example), then you need to set the layout of the panel to null. 如果要在面板中随机定位组件(即示例中JFrame的内容窗格),则需要将面板的布局设置为null。 This means that you need to manually determine the size/location of the component. 这意味着您需要手动确定组件的大小/位置。

So you would need to add: 所以你需要添加:

frame.setLayout( null );

before you start adding the shapes to the frame. 在开始将形状添加到框架之前。

However, in general it is never a good idea to use a null layout. 但是,一般来说,使用null布局绝不是一个好主意。 So instead I suggest you use the Drag Layout as it is designed to replace a null layout in a situation like this. 所以我建议你使用Drag Layout,因为它被设计为在这种情况下替换null布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM