我正在用自己的绘画编写自定义 Swing 组件。我想查询当前外观的组件是否不透明,如果是,它们的背景颜色是什么,以便我的组件也可以使用它。我很难在 Google 上找到它。有人知道吗?谢谢!
最佳答案
这很简单:
public class MyComponent extends JComponent {
public void paintComponent(Graphics g) {
if (this.isOpaque()) {
// Paint background
g.setColor(this.getBackground());
g.fillRect(0,0,this.getWidth(), this.getHeight());
}
g.setColor(this.getForeground());
// Continue painting
}
}
关于java - 在 Swing 中获取当前外观组件背景颜色的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2710458/