【发布时间】:2012-03-23 19:59:03
【问题描述】:
我有一个 JPanel,它实现了自定义绘图来绘制背景。在此之上,应用程序可以放置 JButton 来检测对 JPanel 某些区域的点击。但是,当用鼠标突出显示(非透明)按钮时,底层 JPanel 的图形变得全都出现故障。
这些是 9 个带有自定义绘图的 JPanel,每个 JPanel 都有 2 个填充 JButton(R 和 L)。右上角的块和看起来像它的块是“新鲜的”。右下角的两个按钮都突出显示,中间的只有“R”等。
我这样创建按钮:
rotatePanel = new JPanel();
rotatePanel.setOpaque(false);
GridLayout rotateLayout = new GridLayout(1, 2);
rotatePanel.setLayout(rotateLayout);
rotateRight = new JButton("R");
rotateRight.addActionListener(this);
rotateRight.setOpaque(false);
rotateRight.setContentAreaFilled(false);
rotateRight.setBorderPainted(false);
rotatePanel.add(rotateRight);
rotateLeft = new JButton("L");
rotateLeft.addActionListener(this);
rotateLeft.setOpaque(false);
rotateLeft.setContentAreaFilled(false);
rotateLeft.setBorderPainted(false);
rotatePanel.add(rotateLeft);
这是我的绘图代码:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle clipRect = g.getClipBounds();
clipRect.grow(-4, -4);
int thirdWidth = clipRect.width/3;
int thirdHeight = clipRect.height/3;
for (int x = 0; x < Board.DIM; x++) {
//Draw the columns
for (int y = 0; y < Board.DIM; y++) {
//Draw the rows
g.drawRect(thirdWidth * x, thirdHeight * y, thirdWidth, thirdHeight);
}
}
g.setColor(Color.BLACK);
g.drawRect(0, 0, clipRect.width, clipRect.height);
}
【问题讨论】: