简体   繁体   English

JButton图标透明制品

[英]JButton icon transparent artefacts

some may think I'm making a double post because I already asked a question on this subject here : JButton alpha background change with mouseover 有人可能会认为我正在发表双重看法,因为我已经在这里问过这个问题: JButton alpha背景随鼠标悬停而变化

But I have some problems, not concerning button with alpha background using transparency color but with button using transparent icons (.png). 但是我有一些问题,不是与使用透明色的Alpha背景按钮有关,而是与使用透明图标(.png)的按钮有关。 I already tried lot's of things but I still have artefacts and I really don't understand why. 我已经尝试了很多东西,但是我仍然有人工制品,我真的不明白为什么。

Here is my code : 这是我的代码:

 public class PanelPosition extends JPanel
{
    private JButton leftArrow, rightArrow;

    public PanelPosition (int width, int height)
    {
        this.setPreferredSize(new Dimension(width,height));
        this.setBackground(new Color(0,0,0,0));

        JLabel position = new JLabel("support", JLabel.CENTER);
        position.setForeground(Color.white);

        leftArrow = new JButton(new AlphaImageIcon(new ImageIcon("pictures\\left arrow.png"),1));
        rightArrow = new JButton(new AlphaImageIcon(new ImageIcon("pictures\\right arrow.png"),1));

        leftArrow.setBackground(new Color (0,0,0,0));
        rightArrow.setBackground(new Color (0,0,0,0));

        leftArrow.setPreferredSize(new Dimension(leftArrow.getIcon().getIconWidth(), leftArrow.getIcon().getIconHeight()));
        rightArrow.setPreferredSize(new Dimension(rightArrow.getIcon().getIconWidth(), rightArrow.getIcon().getIconHeight()));

        leftArrow.setOpaque(false);
        leftArrow.setFocusPainted(false);
        leftArrow.setBorderPainted(false);
        leftArrow.setContentAreaFilled(false);
        leftArrow.addMouseListener(new MouseAdapter()
        {
             @Override
            public void mouseEntered(MouseEvent evt)
            {
                leftArrow.setIcon(new AlphaImageIcon(new ImageIcon("pictures\\left arrow.png"), (float)0.7));
            }
            @Override
            public void mouseExited(MouseEvent evt)
            {
               leftArrow.setIcon(new AlphaImageIcon(new ImageIcon("pictures\\left arrow.png"), (float)0.3));
            }
        });

        rightArrow.setOpaque(false);
        rightArrow.setFocusPainted(false);
        rightArrow.setBorderPainted(false);
        rightArrow.setContentAreaFilled(false);
        rightArrow.addMouseListener(new MouseAdapter()
        {
             @Override
            public void mouseEntered(MouseEvent evt)
            {
                rightArrow.setIcon(new AlphaImageIcon(new ImageIcon("pictures\\right arrow.png"), (float)0.7));
            }
            @Override
            public void mouseExited(MouseEvent evt)
            {
               rightArrow.setIcon(new AlphaImageIcon(new ImageIcon("pictures\\right arrow.png"), (float)0.3));
            }
        });

        add(leftArrow);
        add(position);
        add(rightArrow);

    }

}

And for those that the AlphaImageIcon raise questions, I found this class here and I simply take it : https://github.com/griffon/griffon-javatips-plugin/blob/master/src/main/com/wordpress/tipsforjava/swing/AlphaImageIcon.java 对于那些AlphaImageIcon提出问题的人,我在这里找到了这个类,我就简单地接受它: https : //github.com/griffon/griffon-javatips-plugin/blob/master/src/main/com/wordpress/tipsforjava/ swing / AlphaImageIcon.java

Thank's for help and sorry. 感谢您的帮助,对不起。

some may think I'm making a double post 有些人可能会认为我正在发表双重看法

Yes, slightly different question, but same problem. 是的,问题略有不同,但问题相同。

But I have some problems, not concerning button with alpha background using transparency color but with button using transparent icons 但我有一些问题,与使用透明色的Alpha背景按钮无关,而与使用透明图标的按钮有关

Yes, the problem is still with alpha background. 是的,问题仍然出在Alpha背景上。 You are using an alpha background on your panel: 您正在面板上使用Alpha背景:

this.setBackground(new Color(0,0,0,0));

Get rid of the above statement or use the AlphaContainer when you add that panel to your frame. 将该面板添加到框架中时,请摆脱上述声明或使用AlphaContainer

leftArrow.setBackground(new Color (0,0,0,0));
rightArrow.setBackground(new Color (0,0,0,0));

Also, get rid of the above two statements because they do nothing since later you use: 另外,摆脱上面的两个语句,因为它们在以后使用时不起作用:

leftArrow.setOpaque(false);
rightArrow.setOpaque(false);

which means don't paint the background. 这意味着不要绘制背景。 So painting the background any color will have not effect. 因此,将背景涂成任何颜色都不会起作用。 Reread my answer and follow the blog link in your last question! 重读我的答案,并按照您上一个问题的博客链接!

Again the summary from my blog link is that anytime you use alpha on any component you will have a painting problem unless you use one of the two solutions (there may be others) that I suggested. 同样,我博客链接中的摘要是,只要您在我建议的两种解决方案之一(可能还有其他)中使用,则在任何组件上使用alpha时,都会出现绘画问题。

Also, not sure why you have 3 states for your button. 同样,不确定为什么您的按钮有3种状态。 Once you move the mouse over the button it will never go back to the fully opaque state. 一旦将鼠标移到按钮上,它将永远不会回到完全不透明的状态。 If you are just trying to make a rollover effect then you should probably just be using: 如果您只是想产生过渡效果,则可能应该使用:

leftArrow.setRolloverIcon(...);

instead of trying to manage the MouseListener by yourself. 而不是尝试自己管理MouseListener。 Then you will have two states, one the rollover and the other for the default state. 然后,您将有两个状态,一个为过渡状态,另一个为默认状态。 Using this approach you would also only be reading the icons once, not every time the mouse enters/exits the button. 使用这种方法,您还将只读取一次图标,而不是每次鼠标进入/退出按钮时都读取一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM