Java 如何将JButton转换为don';t在滚动或按下带有透明图像图标的按钮时绘制其背景

Java 如何将JButton转换为don';t在滚动或按下带有透明图像图标的按钮时绘制其背景,java,swing,jbutton,Java,Swing,Jbutton,我想让JButton有一个透明的背景图标,它可以响应三种模式并更改其相应的图标: 翻滚 压制 何时变为正常模式 我已经设置了这些图标,但是每次滚动时都会变成黑色,而且当我按下按钮时,背景会变得非常难看(绘制一些文本或其他按钮背景等) 如果你知道这个问题,请告诉我这里出了什么问题。 有关更多详细信息,我上传了一些图片: 正常时 按下或滚动时(在背景中打印JRadioButton) 按下或滚动时(在背景中打印另一个JButton) 多次翻滚时 按下或翻滚时 我只希望背景图标在没有滚动或按下时是正

我想让JButton有一个透明的背景图标,它可以响应三种模式并更改其相应的图标:

  • 翻滚
  • 压制
  • 何时变为正常模式
我已经设置了这些图标,但是每次滚动时都会变成黑色,而且当我按下按钮时,背景会变得非常难看(绘制一些文本或其他按钮背景等)

如果你知道这个问题,请告诉我这里出了什么问题。 有关更多详细信息,我上传了一些图片:

正常时

按下或滚动时(在背景中打印JRadioButton)

按下或滚动时(在背景中打印另一个JButton)

多次翻滚时

按下或翻滚时

我只希望背景图标在没有滚动或按下时是正常的,滚动时变成浅蓝色,按下时变成全蓝色,我已经设置了这些图标,我正在使用Intellij IDE GUI表单设计器

编辑添加的代码:

public class TransparentTest extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    TransparentTest frame = new TransparentTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TransparentTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        //----------------------------------------------------------
        JLayeredPane layeredPane = new JLayeredPane();
        contentPane.add(layeredPane, BorderLayout.CENTER);

        //----------------------------------------------------------
        JPanel mailJPanel = new JPanel();
        mailJPanel.setBackground(Color.RED);
        mailJPanel.setBounds(0, 0, 422, 243);
        layeredPane.add(mailJPanel);

        //----------------------------------------------------------
        JPanel bottomJPanel = new JPanel();
        //panel_1.setOpaque(false);
        bottomJPanel.setBounds(0, 195, 422, 48);
        layeredPane.setLayer(bottomJPanel, 1);
        layeredPane.add(bottomJPanel);
        // set background
        bottomJPanel.setBackground(Color.BLUE);
        Color color = bottomJPanel.getBackground();
        // set transparency
        bottomJPanel.setBackground(new Color(color.getRed(), color.getGreen(), color.getBlue(), 64));
        // set layout
        FlowLayout fl_bottomJPanel = new FlowLayout();
        fl_bottomJPanel.setHgap(0);
        fl_bottomJPanel.setVgap(0);
        bottomJPanel.setLayout(fl_bottomJPanel);

       //----------------------------------------------------------
       JButton button = new JButton("");
       button.setAlignmentX(Component.CENTER_ALIGNMENT);
       button.setPressedIcon(new ImageIcon(TransparentTest.class.getResource("/start-3.png")));
       button.setRolloverIcon(new ImageIcon(TransparentTest.class.getResource("/start-2.png")));
       button.setHorizontalTextPosition(SwingConstants.CENTER);
       //button.setOpaque(false);
       button.setContentAreaFilled(false);
       button.setBorderPainted(false);
       button.setFocusPainted(false);
       button.setIcon(new ImageIcon(TransparentTest.class.getResource("/start-1.png")));
       // add to bottom panel
       bottomJPanel.add(button);
    }
}

你能展示你的代码吗?我在问题中提到,我正在使用Intellij IDE GUI表单设计器,它不生成Java代码,它使用XML格式,你知道…@BahramdunAdil你仍然可以发布一个-一个可编译的示例,为了得到更好的帮助,你付出了一些努力。不过,请发布一个不是用XML制作的。只是纯java代码,所以我们很容易复制和粘贴。您使用了
新颜色(r、g、b、0)
或类似的设置透明背景,不是吗,不起作用。Swing组件是不透明或透明的,这是通过使用
setOpaque
控制的。简单地将背景设置为基于alpha的颜色并不能告诉Swing它也应该更新其下方的颜色。首先,需要将组件设置为透明,
setOpaque(false)
。现在,如果希望组件是半透明的,则需要通过覆盖组件
paintComponent
并填充基于alpha的colorFor,