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

2022-01-24 00:00:00 image java swing jframe image-viewer

根据任务,我们必须创建一个像 Picasa 一样的图像查看器.中间的图片,半透明的黑色背景和左/右按钮改变图像.

我可以显示将其设置为底涂的图像,将其设置为半透明框架,但随着框架,图片变得半透明.我做错了什么.

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 面板 = new ShowImage();panel.setBackground(Color.black);f1.setContentPane(面板);f1.setVisible(true);

我试过了

si.setOpaque();si.setBackground(Color.black);si.setForeground(Color.red);

没有用

当我取一个布尔值并进行测试时

si.isDisplayable();si.isVisible();si.isShowing();

只有可见的返回真,其余的都是假,这些是任何影响因素吗?

解决方案

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

现在您可以查看 和 Window.setBackground 了解此解决方案的工作原理.

接下来,不要使用 Window.setOpacity,它不会达到你想要的.主要原因是 opacity 值应用于父级及其子级(最初是通过我).

所以,框架代码:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();设置默认关闭操作(JFrame.EXIT_ON_CLOSE);//确保在左上角的位置,请查找如何//查找屏幕插图 ;)设置位置(0, 0);设置尺寸(暗淡);//设置未装饰设置未装饰(真);//给背景应用透明色//这很重要,没有这个,它就行不通!setBackground(新颜色(0, 255, 0, 0));//这是我们偷偷摸摸的地方,基本上是要去哪里//提供我们自己的内容窗格,它会做一些特殊的绘画//为了我们设置内容窗格(新内容窗格());getContentPane().setBackground(Color.BLACK);设置布局(新边框布局());//添加图像窗格...ShowImage 面板 = new ShowImage();添加(面板);设置可见(真);

ContentPane.基本上,我们需要欺骗"绘制引擎,让其思考透明(不透明)的位置,然后绘制我们自己的不透明度

公共类 ContentPane 扩展 JPanel {公共内容窗格(){设置不透明(假);}@覆盖受保护的无效paintComponent(图形g){//允许 super 绘制super.paintComponent(g);//应用我们自己的绘画效果Graphics2D g2d = (Graphics2D) g.create();//50% 透明 Alphag2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));g2d.setColor(getBackground());g2d.fill(getBounds());g2d.dispose();}}

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

使用按钮更新

我就是这样修改的

ShowImage 面板 = new ShowImage();panel.setBackground(Color.RED);设置内容窗格(新内容窗格());getContentPane().setBackground(Color.BLACK);设置布局(新边框布局());添加(面板);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(">>"));//好的,理论上,getContentPane() 是必需的,但更安全的是抱歉getContentPane().add(pnlButtons, BorderLayout.SOUTH);设置可见(真);

As per assignment, we have to create a image viewer just like Picasas one. picture in the middle, translucent black background and changing images with left/right buttons.

i can display an image set it to undercoated, set it to translucent frame but along with frame the the picture becomes translucent. what am i doing wrong.

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

i tried

si.setOpaque();   
si.setBackground(Color.black);
si.setForeground(Color.red);

none worked

when i took a boolean and tested

si.isDisplayable();
si.isVisible();
si.isShowing();

only is visible returns true, rest are false, are these any contributing factor?

解决方案

The problem is, the JFrame's default Layout manager is a BorderLayout, this means that your ShowImage pane is filling the entire area of the frame (in black). I bet if you changed the background of the ShowPane to red. It would show up completely filled in red instead

Now you can have a look at A Visual Guide to Layout Managers or change the way your ShowPane works

UPDATE

Apologies, I'm not familiar with the new Transparency API in Java 7 (still using the Java 6 hack ;))

Can you verify for me that this is the kind of effect you are looking for? Where the read square would be the image (and the black background is the frame - nb, I only captured the frame)

UPDATE

First you want to read Window.isOpaque and Window.setBackground to understand how this solution works.

Next, don't use Window.setOpacity, it isn't going to achieve what you want. The main reason is that the opacity value is applied to the parent and it's children (this through me at first).

So, the frame code:

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

The ContentPane. Basically, we need to "trick" the paint engine into thinking where transparent (not opaque) and then paint our own opacity

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

    }

}

I'm sorry for my earlier answer, but I hope this makes up for it ;)

UPDATE with Buttons

This is how I modified it

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

相关文章