如何设置JFrame背景透明但JPanel或JLabel背景不透明? [英] how to set JFrame background transparent but JPanel or JLabel Background opaque?

查看:176
本文介绍了如何设置JFrame背景透明但JPanel或JLabel背景不透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据作业,我们必须像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 的默认布局 manager是 BorderLayout ,这意味着您的 ShowImage 窗格正在填充整个区域框架(黑色)。我敢打赌,如果你将 ShowPane 的背景更改为红色。它会显示完全填充红色而不是



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



接下来,不要使用 Window.setOpacity ,它不会去实现你想要的。主要原因是不透明度值应用于父级及其子级(最初通过我)。



所以,帧代码:

  Dimension dim = Toolkit.getDefaultToolkit()。getScreenSize(); 

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//确保在左上角的位置,请查找
//如何找到屏幕插图;)
setLocation(0,0);
setSize(dim);
//设置未修饰的
setUndecorated(true);
//为背景应用透明色
//这一切都很重要,没有它,它将无效!
setBackground(new Color(0,255,0,0));

//这是我们偷偷摸摸的地方,基本上去
//提供我们自己的内容窗格,为我们做一些特别的绘画
//
setContentPane (new ContentPane());
getContentPane()。setBackground(Color.BLACK);
setLayout(new BorderLayout());

//添加图像窗格...
ShowImage panel = new ShowImage();
add(panel);

setVisible(true);

ContentPane 。基本上,我们需要欺骗油漆引擎思考透明(不透明)的地方,然后绘制我们自己的不透明度

  public class ContentPane扩展JPanel {

public ContentPane(){

setOpaque(false);

}

@Override
protected void paintComponent(Graphics g){

//允许super绘制
super .paintComponent(克);

//应用我们自己的绘画效果
Graphics2D g2d =(Graphics2D)g.create();
// 50%透明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(>>));

//好吧,理论上,getContentPane()是必需的,但更好的安全抱歉
getContentPane()。add(pnlButtons,BorderLayout.SOUTH);

setVisible(true);


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

这篇关于如何设置JFrame背景透明但JPanel或JLabel背景不透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆