如何保持JButton的透明度(java) [英] how to maintain transparency for JButtons (java)

查看:108
本文介绍了如何保持JButton的透明度(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做坦克游戏.在我的菜单中,我想将图片用作jbutton,它们是部分透明的,当它们出现在屏幕上时,透明部分会变成白色.

i'm making a tank game. in my menu i want to use pictures as jbuttons, they are partly transparent and when they appear on screen the transparent parts become white.

我尝试使用.setOpaque,但是这是行不通的.我想不出任何其他方法来摆脱白色部分.我一直在寻找整个堆栈溢出,但没有一种方法似乎有帮助.有想法的人吗?

i tried using .setOpaque but this doesn't work. i can't think of any other method to get rid of the white parts. i've been looking all over stack overflow but none of the methods seem to help. anyone who has an idea?

谢谢!

package menu;

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    @SuppressWarnings("serial")
    public class MenuPanel extends JPanel implements ActionListener
    {

        private Button playKnop, highScoreKnop, quitKnop, HTPKnop;
        private JTextField naam;
        private Image achtergrond;
        private Tanks mainVenster;
        public static String naam_input;

        int x = 95, width = 200, height = 50;



        public MenuPanel(Tanks mainVenster) 
        {
            this.mainVenster = mainVenster;
            this.setLayout(null); 

            playKnop = new Button("/buttons/PLAY.png", 350, this);      
            highScoreKnop = new Button("/buttons/HS.png", 460, this);
            HTPKnop = new Button("/buttons/HTP.png", 515, this);
            quitKnop = new Button("/buttons/QUIT.png", 570, this);



            this.add(playKnop);
            this.add(quitKnop);
            this.add(HTPKnop);
            this.add(highScoreKnop);

            validate();

        }

        public class Button extends JButton
        {
            JButton button;
            ImageIcon buttonImage;

            String backgroundPath;
            int y;


            public Button(String backgroundPath, int y, MenuPanel menuPanel)
            {
                super();
                this.backgroundPath = backgroundPath;
                this.y = y;

                buttonImage = new                 
                       ImageIcon(PlayPanel.class.getResource(backgroundPath));
                this.setIcon(buttonImage); 
                this.setBounds(x, y, width, height);;
                this.addActionListener(menuPanel); 
            }

        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(achtergrond, 0, 0, this.getWidth(), this.getHeight(), 
                this);
        }

        }

推荐答案

删除JButton的某些默认呈现属性,包括

Remove some of the default rendering properties of the JButton including

  • contentAreaFilled
  • borderPainted
  • focusPainted
  • contentAreaFilled
  • borderPainted
  • focusPainted

哪个按钮将减少到什么都没有. JButton已经支持图标的绘制(并且可以用于状态验证),因此无需覆盖它的paintComponent方法...

Which will reduce the button to, well, nothing. JButton already supports the painting of icons (and can do so for a verity of states), so there should be no need to override it's paintComponent method...

public class TestPane extends JPanel {

    public TestPane() {
        setBackground(Color.RED);
        setLayout(new GridBagLayout());
        try {
            BufferedImage img = ImageIO.read(getClass().getResource("/1xaN3.png"));
            JButton btn = new JButton(new ImageIcon(img));
            btn.setOpaque(false);
            btn.setContentAreaFilled(false);
            btn.setBorderPainted(false);
            btn.setFocusPainted(false);

            add(btn);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

个人而言,我更喜欢使用ImageIO而不是ImageIcon(或其他方法)加载图像,主要是因为如果出现问题(而不是默默地失败),它将抛出IOException并且不会返回直到图片加载完毕(如果操作正确,它就会支持进度反馈)

Personally, I prefer to load my images using ImageIO instead of ImageIcon (or other methods), mostly because it will throw an IOException if something goes wrong (rather the failing silently) and won't return until the image is loaded (and supports progress feedback if you do it right)

有关更多详细信息,请参见读取/加载图像

Have a look at Reading/Loading an Image for more details

这篇关于如何保持JButton的透明度(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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