【发布时间】:2010-05-02 06:00:09
【问题描述】:
我正在扩展一个 JPanel 来显示一个游戏板,并在底部添加一个 JEditorPane 来保存一些状态文本。不幸的是,游戏板渲染得很好,但是 JEditorPane 只是一个空白的灰色区域,直到我突出显示其中的文本,那时它将渲染突出显示的任何文本(但不是其余的)。如果我正确理解 Swing,它应该可以工作,因为 super.paintComponent(g) 应该渲染其他子项(即 JEditorPane)。告诉我,伟大的 stackoverflow,我犯了什么愚蠢的错误?
public GameMap extends JPanel {
public GameMap() {
JEditorPane statusLines = new JEditorPane("text/plain","Stuff");
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
this.add(new Box.Filler(/*enough room to draw my game board*/));
this.add(statusLines);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for ( all rows ){
for (all columns){
//paint one tile
}
}
}
}
【问题讨论】:
-
对我来说没问题。你用的是什么 JDK 和 L&F?
-
啊哈!我是个皮洛克! (Pillock 是一个非常有趣的词。)棋盘的每个图块都有自己的绘制方法,问题是我正在翻译 Graphics 对象,以便每个图块可以从 (0,0) 作为左上角进行绘制.而且,愚蠢的我,在绘制其余组件之前,我忘了翻译回全局原点。
标签: java swing jpanel paintcomponent