[英]how to set JFrame background transparent but JPanel or JLabel Background opaque?
根据任务,我们必须像Picasas一样创建一个图像查看器。 中间的图片,半透明的黑色背景和左/右按钮改变图像。
我可以显示一个图像设置为底涂,设置为半透明框架,但随着框架,图片变得半透明。 我究竟做错了什么。
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
JFrame f1 = new JFrame("ShowImage");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(dim);
f1.setUndecorated(true);
f1.setOpacity(0.5f);
ShowImage panel = new ShowImage();
panel.setBackground(Color.black);
f1.setContentPane(panel);
f1.setVisible(true);
我试过了
si.setOpaque();
si.setBackground(Color.black);
si.setForeground(Color.red);
没人工作
当我采取布尔和测试
si.isDisplayable();
si.isVisible();
si.isShowing();
只有可见返回true,休息是假的,这些是否有任何影响因素?
问题是, JFrame
的默认Layout
管理器是BorderLayout
,这意味着您的ShowImage
窗格正在填充框架的整个区域(黑色)。 我敢打赌,如果你将ShowPane
的背景改为红色。 它会显示完全填充红色
现在,您可以查看布局管理器的可视指南或更改ShowPane的工作方式
UPDATE
道歉,我不熟悉Java 7中新的Transparency API(仍然使用Java 6 hack;))
你可以向我核实这是你正在寻找的那种效果吗? 读取方块将是图像(黑色背景是帧 - nb,我只捕获帧)
UPDATE
首先你要读Window.isOpaque和Window.setBackground了解这个解决方案是如何工作的。
接下来,不要使用Window.setOpacity ,它不会实现你想要的。 主要原因是不透明度值应用于父级及其子级(这首先通过我)。
所以,框架代码:
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make sure where in the top left corner, please lookup how
// to find the screen insets ;)
setLocation(0, 0);
setSize(dim);
// Set undecorated
setUndecorated(true);
// Apply a transparent color to the background
// This is ALL important, without this, it won't work!
setBackground(new Color(0, 255, 0, 0));
// This is where we get sneaky, basically where going to
// supply our own content pane that does some special painting
// for us
setContentPane(new ContentPane());
getContentPane().setBackground(Color.BLACK);
setLayout(new BorderLayout());
// Add out image pane...
ShowImage panel = new ShowImage();
add(panel);
setVisible(true);
ContentPane
。 基本上,我们需要“欺骗”绘制引擎思考透明(不透明)的地方然后绘制我们自己的不透明度
public class ContentPane extends JPanel {
public ContentPane() {
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
// Allow super to paint
super.paintComponent(g);
// Apply our own painting effect
Graphics2D g2d = (Graphics2D) g.create();
// 50% transparent Alpha
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2d.setColor(getBackground());
g2d.fill(getBounds());
g2d.dispose();
}
}
我很抱歉我之前的回答,但我希望这能弥补它;)
用按钮更新
这就是我修改它的方式
ShowImage panel = new ShowImage();
panel.setBackground(Color.RED);
setContentPane(new ContentPane());
getContentPane().setBackground(Color.BLACK);
setLayout(new BorderLayout());
add(panel);
JPanel pnlButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
pnlButtons.setOpaque(false);
pnlButtons.add(new JButton("<<"));
pnlButtons.add(new JButton("<"));
pnlButtons.add(new JButton(">"));
pnlButtons.add(new JButton(">>"));
// Okay, in theory, getContentPane() is required, but better safe the sorry
getContentPane().add(pnlButtons, BorderLayout.SOUTH);
setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.