我正在使用扩展 JPanel 的 RefreshablePanel 类
public class RefreshablePanel extends JPanel {
static String description="";
protected void paintComponent(Graphics g){
g.drawString(description, 10, 11);
}
void updateDescription(String dataToAppend){
description = description.concat("\n").concat(dataToAppend);
}
}
JPanel descriptionPanel = new JPanel();
scrollPane_2.setViewportView(descriptionPanel);
descriptionPanel.setBackground(Color.WHITE);
descriptionPanel.setLayout(null);
现在当我这样做时
RefreshablePanel descriptionPanel = new RefreshablePanel();
scrollPane_2.setViewportView(descriptionPanel);
descriptionPanel.setBackground(Color.WHITE);
descriptionPanel.setLayout(null);
最佳答案
这种情况发生变化的原因是,当您重写 paintComponent
时,必须始终调用 super.paintComponent(g)
作为第一行:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(description, 10, 11);
}
JPanel
父类(super class)中的 paintComponent
方法绘制背景,因此如果插入 super.paintComponent(g)
,背景将是在绘制任何自定义内容之前先绘制。
关于java - JPanel 的扩展类不显示 JPanel 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144928/