【发布时间】:2012-10-02 14:22:58
【问题描述】:
您好,我用 google 搜索过,不知道为什么我的 paintComp 方法没有被调用
我有以下代码 包 com.vf.zepto.view;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import com.vf.zepto.view.interfaces.ProcessorPanel;
public class CountryDetailsPanel extends JPanel implements ProcessorPanel, Runnable {
private GridBagConstraints c = new GridBagConstraints();
private String countryName;
private Properties prop = new Properties();
private BufferedImage image;
public CountryDetailsPanel() {
try {
prop.load(new FileInputStream("country.props"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//this.setLayout(new GridBagLayout());
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
this.setPreferredSize(new Dimension(200, 200));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
try {
if(countryName != null) {
String asset = prop.getProperty(countryName+".flag");
if(!asset.equals(null)) {
image = ImageIO.read(new File(asset));
g.drawImage(image, 0, 0, null);
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void updateDetails(Object o) {
countryName = (String)o;
SwingUtilities.invokeLater(this);
}
@Override
public void run() {
this.repaint();
}
}
当调用 this.repaint() 时,期望调用 paintComponent 方法,但不是为了爱和金钱。
已尝试强制它使用 EDT,以防出现问题但不是。
有什么想法吗?
【问题讨论】:
-
+1 表示 ** 是为了爱,也不是为了金钱。虽然有问题的图像的扩展究竟是什么?
-
看起来paint应该调用paintComponent。 leepoint.net/notes-java/GUI-lowlevel/graphics/… 可以帮忙。
标签: java swing repaint paintcomponent event-dispatch-thread