根据作业,我们必须创建一个图像查看器,就像 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);
没有成功
当我取一个 boolean 值并进行测试时
si.isDisplayable();
si.isVisible();
si.isShowing();
只有 visible 返回 true,其余为 false,这些是任何影响因素吗?
最佳答案
问题是,JFrame
的默认 Layout
管理器是 BorderLayout
,这意味着您的 ShowImage
Pane 填充了框架的整个区域(黑色)。我敢打赌,如果您将 ShowPane
的背景更改为红色。它会显示为完全填充红色
现在你可以看看A Visual Guide to Layout Managers或更改 ShowPane 的工作方式
更新
抱歉,我不熟悉 Java 7 中新的透明 API(仍在使用 Java 6 hack ;))
你能证实这就是你正在寻找的那种效果吗?读取方 block 是图像(黑色背景是框架 - 注意,我只捕获了框架)
更新
首先你要阅读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);
关于java - 如何设置 JFrame 背景透明但 JPanel 或 JLabel 背景不透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11703794/