如何在Java中保持JButton的透明度

3
我正在制作一款坦克游戏。在我的菜单中,我希望使用图片作为JButton按钮,它们部分透明,当它们出现在屏幕上时,透明部分会变成白色。
我尝试使用.setOpaque方法,但这并不起作用。我想不到任何其他方法来消除白色部分。我一直在Stack Overflow上寻找,但似乎没有任何方法能够帮助我。有人有想法吗?
谢谢!
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);
        }

        }

尝试这个... button.setContentAreaFilled(false); - sethu
1个回答

7

移除JButton默认的渲染属性,包括:

  • contentAreaFilled
  • borderPainted
  • focusPainted

这将使按钮几乎看不到。 JButton 已经支持绘制图标(并且可以为各种状态绘制),因此无需覆盖其 paintComponent 方法...

Button

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异常(而不是悄悄失败),并且在图像加载完成之前不会返回结果(如果你使用正确的方式还支持进度反馈)

请参考读取/加载图像以获取更多细节信息


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接