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

Lol*_*ola 3 java swing transparency jbutton

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

按钮看起来像这样,所以只有外面必须再次变得透明我尝试使用,.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);
        }

        }
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 6

删除某些默认渲染的性能JButton,包括

  • contentAreaFilled
  • borderPainted
  • focusPainted

哪个会减少按钮,没什么. 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();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

就个人而言,我更喜欢使用ImageIO而不是ImageIcon(或其他方法)来加载我的图像,主要是因为IOException如果出现问题它会抛出(而不是静默失败)并且在图像加载之前不会返回(并且如果你支持进度反馈)做对了)

有关详细信息,请查看读取/加载图像