【发布时间】:2014-01-03 02:42:41
【问题描述】:
我有一个由 jPanel 组成的网格布局。每个 JPanel 都包含一个 JLabel,它们是 9x9。因为这是一个国际象棋游戏,我需要能够在某些事件(例如可能的移动选项或濒临灭绝的领域)上突出显示每个 jPanel 或其 jLabel。但是,我只能突出显示设置了图标(数字)的字段,这意味着空 jLabel 的维度可能为零。
这就是我在 jLabel 中放置数字的方式(it sucks,但它有点工作):
public void drawFigureAt(Figure fg, int x, int y) {
//retrieve the Jlabel from the field array
JLabel pole = policka_l[y*8+x];
//Set icon from file
pole.setIcon(new ImageIcon(fg.imageName()+(fg.color==1?"_white":"_black")+".png"));
//force redraw or whatever it is
pole.revalidate();
}
一些字段现在以非常透明的黑色突出显示,因此我可以看到 JLabels 与其图标一样大。
这就是我强调的方式:
public void highlightField(int x, int y, Color color) {
//Get the JLabel obejct
JLabel pole = policka_l[y*8+x];
//Set the color as the background
pole.setBackground(color);
//What is this?
pole.setOpaque(true);
//Another magic
pole.revalidate();
repaint();
}
但是,正如我所说,如果 JLabel 没有图标,则无法突出显示它,这对我来说非常难过。更糟糕的是这是一个今天早上必须工作的学校项目,所以使用不同的渲染系统是没有问题的。
我找到了一些如何处理它的线索,但是,它们不起作用。我对此寄予厚望:
JLabelinstance.setMinimumSize(new Dimension(70, 70));
不幸的是没有效果。
所以我现在想要的是使上述功能正常工作,甚至更好 - 配置 JLabels 以填充其父 JPanel,以便突出显示看起来不错,而不是愚蠢。
此外,我的图形根本不会随窗口调整大小,因此它们往往会溢出。
【问题讨论】: