swing-在drawingPanel中刷新图片扩展了JPanel

我必须在软件的底部加载一个小图标.只是有一个加载/确定/错误图标.如“ http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html”上所述,我创建了一个扩展的面板,扩展了JPanel.

class drawingPanel extends JPanel
{
    Image img;

    drawingPanel (Image img){
        this.img = img;
    }

    public void paintComponent (Graphics g) {
        super.paintComponent (g);

        // Use the image width & height to find the starting point
        int imgX = getSize ().width/2 - img.getWidth (this);
        int imgY = getSize ().height/2 - img.getHeight (this);

        //Draw image centered in the middle of the panel    
        g.drawImage (img, imgX, imgY, this);
    } // paintComponent

}

我通过以下方式初始化组件:

// Grab the image.
Image img = new ImageIcon(iconPath+"ok.png").getImage();
// Create an instance of DrawingPanel
iconPanel = new drawingPanel(img);

一切正常,但在运行时我希望能够更改面板中的图标.我尝试了所有操作;但是,没有一个能够查看新图片:

Image img = new ImageIcon(iconPath+"loading.gif").getImage();
// Create a new  instance of DrawingPanel
this.iconPanel = new drawingPanel(img);
this.iconPanel.repaint();
this.iconPanel.revalidate();
this.iconPanel.repaint();
this.repaint();
this.revalidate();

(我之所以这样做,是因为我正在编写代码的类是JPanel的另一个扩展,其中包含IconPanel.关于为什么我无法更改图片的任何想法?

谢谢,
斯特凡诺

最佳答案
首先不要以小名字开头类名.将drawingPanel重命名为DrawingPanel.

我已经尝试根据您的描述制作一个简单的演示,并且效果很好.面板中的图像变化完美.

public class Demo {

    public Demo() {
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        // Grab the image.
        Image img = new ImageIcon("1.png").getImage();
        // Create an instance of DrawingPanel
        final DrawingPanel iconPanel = new DrawingPanel(img);

        frame.add(iconPanel, BorderLayout.CENTER);

        JButton button = new JButton("Change image..");
        frame.add(button, BorderLayout.NORTH);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                iconPanel.setImg(new ImageIcon("2.png").getImage());
                iconPanel.repaint();
            }
        });

        frame.setVisible(true);
    }

    public static void main(String[] args){
        new Demo();
    }
}

class DrawingPanel extends JPanel {
    Image img;

    DrawingPanel(Image img) {
        this.img = img;
    }

    public void setImg(Image img) {
        this.img = img;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Use the image width & height to find the starting point
        int imgX = getSize().width / 2 - img.getWidth(this);
        int imgY = getSize().height / 2 - img.getHeight(this);

        // Draw image centered in the middle of the panel
        g.drawImage(img, imgX, imgY, this);
    } // paintComponent

}

我所做的更改是在DrawingPanel类中添加img的setter方法.因此,不必创建新的DrawingPanel,而只需用新的Image调用setImg(),然后调用reapint绘制新的图像即可.

转载注明原文:swing-在drawingPanel中刷新图片扩展了JPanel - 代码日志