java jbutton 透明,如何保持JButton的透明度(java)

i'm making a tank game. in my menu i want to use pictures as jbuttons, they are partly transparent and when they appear on screen the transparent parts become white.

1xaN3.pngi tried using .setOpaque but this doesn't work. i can't think of any other method to get rid of the white parts. i've been looking all over stack overflow but none of the methods seem to help. anyone who has an idea?

Thanks!

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);

}

}

解决方案

Remove some of the default rendering properties of the JButton including

contentAreaFilled

borderPainted

focusPainted

Which will reduce the button to, well, nothing. JButton already supports the painting of icons (and can do so for a verity of states), so there should be no need to override it's paintComponent method...

sU3W6.png

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();

}

}

}

Personally, I prefer to load my images using ImageIO instead of ImageIcon (or other methods), mostly because it will throw an IOException if something goes wrong (rather the failing silently) and won't return until the image is loaded (and supports progress feedback if you do it right)

Have a look at Reading/Loading an Image for more details

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值