开发者

Dynamic JFrame in java

开发者 https://www.devze.com 2023-03-05 21:18 出处:网络
I have a project and I need to know how to adding a component in a JFrame when pressing a JButton. I have added a panel into panel by this code:

I have a project and I need to know how to adding a component in a JFrame when pressing a JButton. I have added a panel into panel by this code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       开发者_StackOverflow  

    colorPanel color0=new colorPanel();
    color0.setBounds(5,150+nb,300,120);
    color0.setLayout(new FlowLayout());

    this.getContentPane().add(color0);
    this.validate();

    System.out.println("add couleur:"+nb);
    nb+=50;
}

It works but the problem is that the component is deleted when I maximize or minimize the JFrame. Thanks.


When you resize your Jframe, a repaint() & paint() methods are called which probably erases your component.

0

精彩评论

暂无评论...
验证码 换一张
取 消