4

这是这篇文章的延续

我有一组随机大小的图形添加和绘制到 JPanel 组件上。我有一个按钮,它正在向同一个 JPanel 添加一个新的绘图对象,但在我重新调整窗口大小之前不会显示。我已经添加了这篇文章中提到的 EDT 信息,并且还在组件上调用了 repaint() 方法。正如Hovercraft所建议的那样,我还没有使用 ArrayList ,但我会的。我的大脑需要慢慢地理解事物。

谢谢你。

代码分为两个类。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ZombieDance extends JComponent {

JFrame canvas = new JFrame();
JPanel actionPanel = new JPanel();
JButton newZombie = new JButton("Add new Zombie");

ZombieDance(){
//create a couple default zombies
    buildGUI();
    Random rand = new Random();
    int i,x,y,w,h;

    //add zombies to my canvas
    for (i=1;i<8;i++) {
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
    }   
}

//prep the canvas
void buildGUI(){
    actionPanel.add(newZombie);
    canvas.add(actionPanel);
    canvas.setLayout(new GridLayout(3,3));
    canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.setSize(400,400);
    canvas.setBackground(Color.WHITE);
    canvas.setVisible(true);

    newZombie.addActionListener(new NewZombieClickHandler());
}

public class NewZombieClickHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Random rand = new Random();
        int x,y,w,h;
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
        canvas.repaint();
    }
}   

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          new ZombieDance();
        }
    });
  }

}

二等舱

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Zombie extends JPanel{

private int x,y,w,h;
private float r,g,b;

Zombie(int argx, int argy, int argw, int argh, float argr, float argg, float argb){
    x = argx;
    y = argy;
    w = argw;
    h = argh;
    r = argr;
    g = argg;
    b = argb;
}

public Dimension getPreferredSize() {
    return new Dimension(20,20);
}

protected void paintComponent(Graphics gr) {
    super.paintComponent(gr);       
    //g.drawString("Drawing canvas...",10,20);
    gr.setColor(new Color(r,g,b));
    gr.fillRect(x,y,h,w);
} 
}
4

2 回答 2

7

我有一个按钮,它正在向同一个 JPanel 添加一个新的绘图对象,但在我重新调整窗口大小之前不显示

当您将组件添加到可见 GUI 时,代码应为:

canvas.add(...);
canvas.validate();
//canvas.repaint(); // sometimes needed

(编辑:更改为validate

于 2011-09-26T19:59:54.323 回答
-1

我自己试了一下,发现paintChildren() 方法解决了这个问题。

于 2014-08-13T01:27:18.607 回答