如何设置JFrame背景透明但JPanel或JLabel背景不透明?

sab*_*AVA 8 java swing image image-viewer jframe

根据任务,我们必须像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);  
Run Code Online (Sandbox Code Playgroud)

我试过了

si.setOpaque();   
si.setBackground(Color.black);
si.setForeground(Color.red);
Run Code Online (Sandbox Code Playgroud)

没人工作

当我采取布尔和测试

si.isDisplayable();
si.isVisible();
si.isShowing();
Run Code Online (Sandbox Code Playgroud)

只有可见返回true,休息是假的,这些是否有任何影响因素?

Mad*_*mer 20

问题是,JFrame默认Layout管理器是a BorderLayout,这意味着您的ShowImage窗格正在填充框架的整个区域(黑色).我敢打赌如果你改变了ShowPane红色的背景.它会显示完全填充红色

现在,您可以查看布局管理器的可视指南或更改ShowPane的工作方式

UPDATE

道歉,我不熟悉Java 7中新的Transparency API(仍然使用Java 6 hack;))

透明框架

你可以向我核实这是你正在寻找的那种效果吗?读取方块将是图像(黑色背景是帧 - nb,我只捕获帧)

UPDATE

图像预览

首先你要读Window.isOpaqueWindow.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);
Run Code Online (Sandbox Code Playgroud)

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();

    }

}
Run Code Online (Sandbox Code Playgroud)

我很抱歉我之前的回答,但我希望这能弥补它;)

用按钮更新

这就是我修改它的方式

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);
Run Code Online (Sandbox Code Playgroud)

带按钮的样品