如何设置JPanel的透明背景 [英] How to set a Transparent Background of JPanel

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

问题描述

我需要知道,如果一个JPanel`s bacground可以设置为透明?

I need to know if a JPanel`s bacground can be set to Transparent?

我的框架有两个Jpanels
图像面板和功能面板,功能面板重叠图像面板,图像面板工作作为背景,它是从远程URL加载的形象,现在我希望借助功能面板shaps,但现在图像面板不能因为看到功能面板的背景色。我需要功能面板背景透明,同时还绘制其形状,我想图像面板,因为它是做图像拼接和缓存功能是可见的。我需要单独的图像拉丝和形状为什么我真的使用两个jPanels绘制这就是!反正是有重叠的JPanel有一个透明的背景是什么?

My frame is has two Jpanels Image Panel and Feature Panel, Feature Panel is overlapping Image Panel, the Image Panel is working as a background and it is loading image from a remote Url, now I want to draw shaps on Feature Panel , but now Image Panel cannot be seen due to Feature Panel's background color. I need to make Feature Panel background transparent while still drawing its shapes and i want Image Panel to be visible since it is doing tiling and cache function of images. I need to seperate the image drawing and shape drawing thats why I`m using two jPanels! is there anyway the overlapping Jpanel have a transparent background?

感谢

推荐答案

此外,考虑的窗格玻璃,本文所讨论的 如何使用根窗格。你可以画在玻璃面板的的paintComponent()方法你的功能的内容。

Alternatively, consider The Glass Pane, discussed in the article How to Use Root Panes. You could draw your "Feature" content in the glass pane's paintComponent() method.

附录:用<工作href=\"http://docs.oracle.com/javase/tutorial/uiswing/examples/components/GlassPaneDemoProject/src/components/GlassPaneDemo.java\"相对=nofollow> GlassPaneDemo ,我添加了一个形象:

Addendum: Working with the GlassPaneDemo, I added an image:

/* Set up the content pane, where the "main GUI" lives. */
frame.add(changeButton, BorderLayout.SOUTH);
frame.add(new JLabel(new ImageIcon("img.jpg")), BorderLayout.CENTER);

和改变了玻璃面板的的paintComponent()方法:

and altered the glass pane's paintComponent() method:

protected void paintComponent(Graphics g) {
    if (point != null) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, 0.3f));
        g2d.setColor(Color.yellow);
        g2d.fillOval(point.x, point.y, 120, 60);
    }
}

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

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