Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何保持JButtons(java)的透明度_Java_Swing_Jbutton_Transparency - Fatal编程技术网

如何保持JButtons(java)的透明度

如何保持JButtons(java)的透明度,java,swing,jbutton,transparency,Java,Swing,Jbutton,Transparency,我在做坦克游戏。在我的菜单中,我想使用图片作为按钮,它们部分是透明的,当它们出现在屏幕上时,透明部分变成白色 我试着使用.setOpaque,但这不起作用。我想不出任何其他方法来去除白色部分。我一直在寻找堆栈溢出,但没有一个方法似乎有帮助。谁有主意 谢谢 package menu; import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("seri

我在做坦克游戏。在我的菜单中,我想使用图片作为按钮,它们部分是透明的,当它们出现在屏幕上时,透明部分变成白色

我试着使用
.setOpaque
,但这不起作用。我想不出任何其他方法来去除白色部分。我一直在寻找堆栈溢出,但没有一个方法似乎有帮助。谁有主意

谢谢

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
的一些默认呈现属性,包括

  • contentAreaFilled
  • borderPainted
  • focuspaint
这会把按钮减少到,嗯,什么都没有
JButton
已经支持图标的绘制(并且可以为许多状态这样做),因此不需要重写它的
paintComponent
方法

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
,并且在加载图像之前不会返回(如果你做得对,它支持进度反馈)


查看更多详细信息

试试这个。。按钮。setContentAreaFilled(假);