我试图有一个图像背景,所以我在 JFrame 中创建了以下代码:
@Override
public void paint(Graphics g) {
super.paint(g);
try {
final Image image = ImageIO.read(getClass().getResource("/images/login/gentlenoise100.png"));
int iw = 256;
int ih = 256;
for (int x = 0; x < getWidth(); x += iw) {
for (int y = 0; y < getHeight(); y += ih) {
g.drawImage(image, x, y, iw, ih, this);
}
}
} catch (IOException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
for(Component componente:getComponents()){
componente.repaint();
}
}
我看到背景颜色有某种偏好,我决定将其设置为不可见:
setBackground(new java.awt.Color(0,0,0,0));
它在 Mac OS X (java 1.6) 中运行良好,我不得不在 Windows 中对其进行探测,如果我删除 setBackground 调用,它不会显示我的背景,如果我保持背景颜色不可见,它会引发异常并说框架装饰!
我尝试使用setUndecorate(true)
,但在 macOS 中它丢失了标题栏(当然),在 Windows 中它给了我一个透明窗口。
我该如何解决?