gpt4 book ai didi

java - 如何强制调用paintComponent?

转载 作者:行者123 更新时间:2023-11-29 10:02:25 25 4
gpt4 key购买 nike

我有一个覆盖了 paintComponent() 的 JLabel。我希望它被强制调用,因为更新我的 Label UI 的代码就是这个事件。我如何强制调用和更新 UI? (顺便说一句,重绘不起作用!)

这是我的代码:

BufferedImage background;
String Uri;

public CustomClockLabel(String Uri){
init(Uri);
this.Uri = Uri;
}

public void init(String Uri){
try {
URL inp = CustomClockLabel.class.getResource(Uri);
background = ImageIO.read(inp);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
protected void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

if(background != null){
g2.drawImage(background, 0, 0,getWidth(),getHeight(), this);
}
g2.dispose();
super.paintComponent(g);
}

这是更新标签的代码,它被递归调用:

lblHour1 = new CustomClockLabel("/icons/noa_en/"+Hour.charAt(0)+".png");
lblHour1.repaint();
lblHour2 = new CustomClockLabel("/icons/noa_en/"+Hour.charAt(1)+".png");
lblHour2.repaint();
lblMin1 = new CustomClockLabel("/icons/noa_en/"+Minute.charAt(0)+".png");
lblMin1.repaint();
lblMin2 = new CustomClockLabel("/icons/noa_en/"+Minute.charAt(1)+".png");
lblMin1.repaint();

最佳答案

您可能误以为创建新标签会更新屏幕上的内容,这样做...

lblHour1 = new CustomClockLabel("/icons/noa_en/"+Hour.charAt(0)+".png");
lblHour2 = new CustomClockLabel("/icons/noa_en/"+Hour.charAt(1)+".png");
lblMin1 = new CustomClockLabel("/icons/noa_en/"+Minute.charAt(0)+".png");
lblMin2 = new CustomClockLabel("/icons/noa_en/"+Minute.charAt(1)+".png");

将更改变量的引用,因此它们将不再是您添加到屏幕的变量。

假设上面的变量已经被添加到屏幕上,你可以简单地通过使用类似...的东西来更新它们

lblHour1.init(new ImageIcon("/icons/noa_en/"+Hour.charAt(0)+".png"));
lblHour2.init(new ImageIcon("/icons/noa_en/"+Hour.charAt(1)+".png"));
lblMin1.init(new ImageIcon("/icons/noa_en/"+Minute.charAt(0)+".png"));
lblMin2.init(new ImageIcon("/icons/noa_en/"+Minute.charAt(1)+".png"));
revalidate();
repaint();

如果失败,您应该尝试设置一个标签边框的属性,以便您可以查看它是否真的被添加到屏幕上。

已更新

在对您提供的少量代码进行一些实验后,这里有一些更多的建议...

  1. 如前所述,请确保您首先调用了 super.paintComponent,因为此方法的工作之一是清除准备绘画的图形...
  2. 确保为组件提供合适的大小提示,以便布局管理器对您希望组件的大小有一些了解。这可确保组件的大小不会为 0x0

下面的示例非常简单,但是使用了您提供的(很少的)代码并从中构建了一个可运行的示例...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PaintComponentTest {

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

private int time = 0;

public PaintComponentTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

final CustomClockLabel counter = new CustomClockLabel("/icons/0.png");
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
time++;
if (time > 9) {
time = 0;
}
counter.init("/icons/" + Integer.toString(time) + ".png");
counter.repaint();
}
});
timer.start();

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(counter);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class CustomClockLabel extends JPanel {

BufferedImage background;
String Uri;

public CustomClockLabel(String Uri) {
init(Uri);
this.Uri = Uri;
}

public void init(String Uri) {
try {
URL inp = getClass().getResource(Uri);
background = ImageIO.read(inp);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

if (background != null) {
g2.drawImage(background, 0, 0, getWidth(), getHeight(), this);
}
g2.dispose();
}
}

}

关于java - 如何强制调用paintComponent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20201024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com