簡體   English   中英

更改后如何重新繪制圖像?

[英]How to repaint Image after changing?

我有一個JPanel ,其中有一個JLabel ,其中包含一個Image ,如下所示:

JLabel imageLabel = new JLabel(new ImageIcon(image));

之后,我設置imageLabelbounds ,如下所示:

//I want the Image to be in the middle of the screen!
imageLabel.setBounds((int) (screenSize.getWidth() / 2 - image.getWidth(null) / 2),
        (int) (screenSize.getHeight() / 2 - image.getHeight(null) / 2),
        image.getWidth(null), image.getHeight(null));

然后將imageLabel添加到JPanel

add(imageLabel);

現在,我想通過使用KeyEvent更改Image (該KeyEvent起作用)。 我認為,它會更改Image (通過使用image = any other Image ),但在屏幕上不會更改。
我該如何實現? 我試圖添加revalidate()repaint(); 到JPanel。

我認為,它會更改Image(通過使用image =任何其他Image),

那什么也沒做。 您要做的只是更新變量以指向其他圖像。 您實際上並沒有將圖像添加到標簽中。

您需要重置標簽的圖標:

label.setIcon( new ImageIcon(...) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM