如何在 JPanel 中使内容半透明? [英] How contents are made translucent in a JPanel?

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

问题描述

我对 JPanel 中组件的显示感到困惑.

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

假设我创建一个半透明度为 0.8f 的自定义 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);
        }
    };

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

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)然后在输出中为什么我得到半透明按钮?由于 JPanel 是单层的,当我覆盖它的 paint 方法然后添加按钮时,我首先绘制了半透明图像,按钮不能半透明,因为他们应该过来.

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)那4个按钮中也只有2个是半透明的.为什么会有这样的偏袒?

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

3)如果我在添加这 4 个按钮之前还添加了一个表格,那么一切都会变得半透明.为什么?

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)如果我为JPanel使用paintComponent(Graphics g)那么没有任何东西是半透明的,无论我是否添加表格或添加了多少按钮.为什么?

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?

(我问的是应用程序运行时的即时输出.我知道当鼠标悬停在这些按钮上或单击表中的任何行时,它会变得不透明,这是由于 Swing 的绘制机制.)

(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) 那么在输出中为什么我会得到半透明的按钮?由于 JPanel 是单身分层,当我覆盖它时,我首先绘制了半透明图像绘制方法然后添加按钮,按钮不能半透明,因为他们应该过来.

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.

实际上,您在按钮上绘制了半透明效果.paint 调用

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

  • paintComponent
  • paintBorder
  • paintChildren

然后你在已经画好的东西(孩子们)的顶部画了你的半透明效果.添加组件时没有区别,Swing 将在多种情况下绘制组件,首先是在组件首次实现时(在屏幕上可见)和响应脏区域(以及大量其他人)......不要欺骗你自己,你无法控制这个......

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...

有用;)

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

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