我有一個JLabel中的按鈕列表。這些按鈕的圖像和圖像的背景是透明的,但是按鈕iself比圖像更大,它涵蓋了背景圖片,這裏就是我的意思:如何將按鈕背景設置爲透明?
這是我的代碼:
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代碼:我測試這些命令與第一張地圖,但它仍然沒有SH
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);
}
}
注在右邊標籤的背景。我錯過了什麼?
的[透明的JButton(http://stackoverflow.com/questions/4585867/transparent-jbutton) – Braj
我怎樣才能檢測到這種可能重複? –
只需搜索'StakOverflow'。對你有幫助嗎? – Braj