Java 如何正确地绘制图像而不仅仅是主窗口?

Java 如何正确地绘制图像而不仅仅是主窗口?,java,swing,java-2d,Java,Swing,Java 2d,如果我从netbeans运行它,有时它会按预期绘制图像。。但有时我只会看到主窗口,以及移动鼠标后的“开始”按钮 public class my_gui extends JFrame { public my_gui() { setTitle("Broscute 1.0 :p"); setSize(954, 320); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(f

如果我从netbeans运行它,有时它会按预期绘制图像。。但有时我只会看到主窗口,以及移动鼠标后的“开始”按钮

public class my_gui extends JFrame {
    public my_gui() {
        setTitle("Broscute 1.0 :p");
        setSize(954, 320);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage("src/img/test.png"));
        setVisible(true);
        initUI();
    }
    public final void initUI() { //ui here
        setLayout(null);
        setLocationRelativeTo(null);
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 954, 320);
        getContentPane().add(panel);
        JButton button = new JButton("Start!");
        button.setBounds(0, 0, 954, 40);
        final ImagePanel[] label = new ImagePanel[4];
        int i, j;
        for(i=40, j=0;i<=220;i+=60, j++){
            label[j] = new ImagePanel(0, i);
            label[j].setBounds(0, 0, 954, 320);
            panel.add(label[j]);
        }
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                label[3].X += 100;

            }
        });
        panel.add(button);
    }
}
class ImagePanel extends JComponent{
    public int X, Y;
    float v = 10;
    private BufferedImage image;
    ImagePanel(){}
    public ImagePanel(int x, int y) {
        X = x; Y = y;
       try {                
          image = ImageIO.read(new File("src/img/broasca.png"));
       } catch (IOException ex) {
            // handle exception...
       }
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintChildren(g);  //a friend told me I should put it here
        g.drawImage(image, X, Y, this); // see javadoc for more info on the parameters
        repaint(); //I think this should go here
    }
}
公共类my_gui扩展JFrame{
公共my_gui(){
setTitle(“Broscute 1.0:p”);
设置大小(954320);
setDefaultCloseOperation(关闭时退出);
可设置大小(假);
setIconImage(Toolkit.getDefaultToolkit().getImage(“src/img/test.png”);
setVisible(真);
initUI();
}
public final void initUI(){//ui在此
setLayout(空);
setLocationRelativeTo(空);
JPanel面板=新的JPanel();
panel.setLayout(空);
面板立根(0,0,954,320);
getContentPane().add(面板);
JButton按钮=新JButton(“开始!”);
按钮.立根(0,0,954,40);
最终ImagePanel[]标签=新ImagePanel[4];
int i,j;

对于(i=40,j=0;i您通常在
paintComponent
中做的第一件事是通过
super.paintComponent()
清除屏幕外位图-我打赌这是您的问题。

您通常在
paintComponent
中做的第一件事是通过
super.paintComponent()清除屏幕外位图
-我打赌这是你的问题。

在所有初始化完成之前,不要显示对话框,即将集合移动到构造函数中的最后一个方法

为了更好地重用,根本不需要JFrame派生类调用setVisible(true),让客户机来执行


问题是,一旦显示窗口,对窗口所做的任何更改都必须在GUI线程上完成,否则将出现与您所看到的一样的虚假问题。

在所有初始化完成之前,不要显示对话框,即将setVisible移到构造函数中的最后一个方法

为了更好地重用,根本不需要JFrame派生类调用setVisible(true),让客户机来执行


问题是,一旦显示窗口,对窗口所做的任何更改都必须在GUI线程上完成,否则会出现与您所看到的一样的虚假问题。

仅使用super.paintComponent()而不是super.paintChildren()…结果相同,有时它会绘制图像,有时则不会。仅使用super.paintComponent()进行了尝试与super.paintChildren()不同的是,同样的结果,有时它会绘制图像,有时则不会。就像一个符咒,谢谢你。关于如何在将图像构建到.jar文件后加载图像,有什么想法吗?新的图像图标(my_gui.class.getResource(“/img/test.png”));非常感谢您,非常感谢您。关于如何在将图像构建到.jar文件后加载图像,有什么想法吗?新建图像图标(my_gui.class.getResource(“/img/test.png”);