如何内容一个JPanel是用半透明? [英] How contents are made translucent in a JPanel?

查看:171
本文介绍了如何内容一个JPanel是用半透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑关于组件的一个JPanel显示。

假设,如果我创建0.8f的半透明定制的JPanel如下: -

 的JP​​anel面板=新JPanel(){
        @覆盖
        公共无效漆(图形G)
        {
            super.paint(G);
            IMG的BufferedImage =(BufferedImage的)的createImage(的getWidth()的getHeight());
            Graphics2D的G2 =(Graphics2D的)g.create();
            g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
            g2.drawImage(IMG,0,0,NULL);
        }
        @覆盖
        公共尺寸的get preferredSize()
        {
            返回新的Dimension(300,300);
        }
    };

现在我将其设置为框架的contentPane。

  frame.setContentPane(面板);

现在我一些按钮添加到它。

  frame.add(的新的JButton(点击这里));
    frame.add(的新的JButton(点击这里));
    frame.add(的新的JButton(点击这里));
    frame.add(的新的JButton(点击这里));

1),然后在输出为什么我得到半透明按钮?由于JPanel的是单层和我第一次画的半透明图像时我overrided其油漆方法,然后按键分别为补充说,该按钮不得半透明的,因为他们要过来了。

2)还掉那些4个按键只有2个是半透明的。为什么会出现这种偏袒?

3)如果我也加入这些4个按钮,然后一切就变得translucent.Why前添加一个表?

 对象[] =名新的对象[] {
             标题,艺术家,专辑
         };
         的String [] []数据=新的String [] [] {
             {洛杉矶,Sugarcult,熄灯},
             {独自做到这一点,Sugarcult,熄灯},
             {犯了错误,Sugarcult,熄灯},
             {吻你更好,马克西莫公园,某个触发因素},
             {遍铺,马克西莫公园,某个触发因素},
             {失踪,马克西莫公园,某个触发因素}
         };
         JTable的表=新的JTable(数据名称);
         frame.add(表);

4)如果我使用的paintComponent(图形G)的JP​​anel中再没有什么是半透明的,我是否添加表或不作为,或许多按钮都added.Why?

(我问什么时候应用是run.I知道立即输出,当鼠标滑过那些按钮,或者如果表中的任何行点击它时会变成不透明,这是由于Swing的绘画机制。)


解决方案

  

1),然后在输出为什么我得到半透明按钮?由于JPanel的是单
  分层和我第一次画的半透明图像时我overrided其
  漆的方法,然后加入按钮,按钮必须不
  半透明的,因为他们要过来了。


其实,你画了按钮的半透明效果。 油漆通话


  • 的paintComponent

  • 的paintBorder

  • paintChildren

然后你画了什么已经绘(孩子)的顶部的半透明效果。它会使没有区别,当您添加组件时,Swing会画下许多情况下分量,首先被当组件第一次实现(在屏幕上可以看到),并响应改变脏区(和许多其他)...骗不了你自己,你有没有控制此...

想想烤漆工艺作为一种分层的方法。首先绘制背景,那么你画中期地面,最后,脱颖而出地上,然后你去了,泼了哪些...

 公共类TestTranslucentControls {    公共静态无效的主要(字串[] args){
        新TestTranslucentControls();
    }    公共TestTranslucentControls(){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }赶上(ClassNotFoundException的| InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException前){
                }                JFrame的帧=新的JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(新的BorderLayout());
                frame.add(新TestPane());
                frame.pack();
                frame.setLocationRelativeTo(NULL);
                frame.setVisible(真);
            }
        });
    }    公共类TestPane继承JPanel {        私人的BufferedImage背景;        公共TestPane(){
            的setLayout(新的GridBagLayout());
            尝试{
                背景= ImageIO.read(新文件(C:/Users/shane/Dropbox/MegaTokyo/poster.jpg));
            }赶上(IOException异常前){
                ex.printStackTrace();
            }
            添加(新的JButton(1));
            添加(新的JButton(2));
            添加(新的JButton(3));
            添加(新的JButton(4));
            添加(新的JButton(5));
        }        @覆盖
        保护无效paintComponent(图形G){
            super.paintComponent方法(G);
            如果(背景!= NULL){
                Graphics2D的G2 =(Graphics2D的)g.create();
                g2.setComposite(AlphaComposite.SrcOver.derive(0.25f));
                INT X =(的getWidth() - background.getWidth())/ 2;
                INT Y =(的getHeight() - background.getHeight())/ 2;
                g2.drawImage(背景,X,Y,这一点);
                g2.dispose();
            }
        }        @覆盖
        公共尺寸的get preferredSize(){
            返回后台== NULL?新尺寸(300,300):新的Dimension(background.getWidth(),background.getHeight());
        }
    }
}

我想你可能会发现...

有用)

I am confused regarding the display of components in a JPanel.

Suppose if I create a custom JPanel with translucency of 0.8f as follows :-

JPanel panel=new JPanel(){
        @Override
        public void paint(Graphics g)
        {
            super.paint(g);
            BufferedImage img=(BufferedImage)createImage(getWidth(),getHeight());
            Graphics2D g2=(Graphics2D) g.create();
            g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
            g2.drawImage(img,0,0,null);
        }
        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(300,300);
        }
    };

Now I set it as the contentPane of the frame.

frame.setContentPane(panel);

Now I add some buttons to it.

    frame.add(new JButton("Click Here"));
    frame.add(new JButton("Click Here"));
    frame.add(new JButton("Click Here"));
    frame.add(new JButton("Click Here"));

1)Then in the output why I get translucent buttons?As JPanel is single layered and I painted the translucent image first when I overrided its paintmethod and then buttons were added,the buttons must not be translucent as they should come over it.

2)Also out of those 4 buttons only 2 are translucent .Why is there such partiality?

3)If I also add a table before adding these 4 buttons then everything becomes translucent.Why?

Object[] names = new Object[] {
             "Title", "Artist", "Album"
         };
         String[][] data = new String[][] {
             { "Los Angeles", "Sugarcult", "Lights Out" },
             { "Do It Alone", "Sugarcult", "Lights Out" },
             { "Made a Mistake", "Sugarcult", "Lights Out" },
             { "Kiss You Better", "Maximo Park", "A Certain Trigger" },
             { "All Over the Shop", "Maximo Park", "A Certain Trigger" },
             { "Going Missing", "Maximo Park", "A Certain Trigger" }
         };
         JTable table = new JTable(data, names);
         frame.add(table); 

4)If I use paintComponent(Graphics g) for JPanel then nothing is translucent,whether I add table or not or as many buttons are added.Why?

(I am asking about the immediate output when application is run.I know that when mouse is rolled over those buttons or if any row in table is clicked it will become opaque,which is due to Swing's painting mechanism.)

解决方案

1) Then in the output why I get translucent buttons? As JPanel is single layered and I painted the translucent image first when I overrided its paint method and then buttons were added, the buttons must not be translucent as they should come over it.

Actually, you painted the translucent effect OVER the buttons. paint calls

  • paintComponent
  • paintBorder
  • paintChildren

Then you painted your translucent effect OVER the top of what has already been painted (the children). It will make no difference when you add the components, Swing will paint the component under a number of circumstances, the first been when the component is first realised (made visible on the screen) and in response to changes to the dirty regions (and lots of others)...don't fool your self, you have no control over this...

Think of the paint process as a layering approach. First you paint the background, then you paint the mid ground and then finally, the fore ground, which then you went and splashed over...

public class TestTranslucentControls {

    public static void main(String[] args) {
        new TestTranslucentControls();
    }

    public TestTranslucentControls() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage background;

        public TestPane() {
            setLayout(new GridBagLayout());
            try {
                background = ImageIO.read(new File("C:/Users/shane/Dropbox/MegaTokyo/poster.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            add(new JButton("1"));
            add(new JButton("2"));
            add(new JButton("3"));
            add(new JButton("4"));
            add(new JButton("5"));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setComposite(AlphaComposite.SrcOver.derive(0.25f));
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g2.drawImage(background, x, y, this);
                g2.dispose();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(300, 300) : new Dimension(background.getWidth(), background.getHeight());
        }
    }
}

I think you may find...

useful ;)

这篇关于如何内容一个JPanel是用半透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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