【问题标题】:How to set a button background to transparent?如何将按钮背景设置为透明?
【发布时间】:2014-06-20 21:52:36
【问题描述】:

我在 JLabel 中有一个按钮列表。按钮是图像并且该图像的背景是透明的,但是按钮本身比图像大并且它覆盖了背景图像,这就是我的意思:

这是我的代码:

JPanel buttons = new JPanel(new GridLayout(0, 1));
        JButton butoMapa = null;
        try {
            butoMapa = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Mapa.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        butoMapa.setOpaque(false);
        butoMapa.setContentAreaFilled(false);
        butoMapa.setBorderPainted(false);
        butoMapa.addActionListener(this);

        JButton butoEtnies = null;
        try {
            butoEtnies = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Etnies.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        butoEtnies.addActionListener(this);

        JButton butoComandes = null;
        try {
            butoComandes = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Comandes.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        butoComandes.addActionListener(this);

        JButton butoSurtir = null;
        try {
            butoSurtir = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Surtir.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        butoSurtir.addActionListener(this);

        SomePanel label2 = new SomePanel();
        label2.add(buttons);

        frame.add(label2, BorderLayout.EAST);
        buttons.add(butoMapa);
        buttons.add(butoEtnies);
        buttons.add(butoComandes);
        buttons.add(butoSurtir);

        //JPanel right = new JPanel(new BorderLayout());
       // right
        //right.add(buttons, BorderLayout.NORTH);


        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

SomePanel 代码:

class SomePanel extends JPanel {
    private BufferedImage image;

    public SomePanel() {
        try {
            image = ImageIO.read(getClass().getResource("imatges/costat.png"));
        } catch (IOException ex) {}
        //add(new JButton("Hello"));
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
}

请注意,我使用第一张地图测试了这些命令,但它仍然没有显示右侧标签的背景。我错过了什么?

【问题讨论】:

  • Transparent JButton的可能重复
  • 我怎样才能检测到这个?
  • 只需搜索StakOverflow。对你有帮助吗?

标签: java jbutton jlabel


【解决方案1】:

按钮有一个默认的边框。如果你不想要边框,你可以使用:

button.setBorderPainted( false );

您可能还想使用:

button.setFocusPainted( false );
button.setContentAreaPainted( false );

编辑:

糟糕,我刚刚注意到您确实为您的第一个按钮使用了上面的代码(如果您的其他按钮当然需要重复)。

我猜问题出在按钮面板上。您还需要使面板透明:

JPanel buttons = new JPanel(new GridLayout(0, 1));
buttons.setOpaque( false );

【讨论】:

  • 天哪,你成功了!我已经花了 3 个多小时来寻找这样的东西,我所要做的就是让面板透明......非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2019-03-18
  • 2016-09-12
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
相关资源
最近更新 更多