日期:2014-05-20  浏览次数:20909 次

设置JPanel为透明
我定义了一个JPanel,定义如下:
[code=Java][/code]
package ui.mainUi;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;

import java.awt.*;

class MainPanel extends JPanel {

private JButton playerButton;
private JButton nextone;
private JButton previousone;
private JButton vloumimage;
protected static Label labelfilepath = new Label("播放目录:");

private static final Image image = Toolkit.getDefaultToolkit().getImage(
"img/skin6.jpg");

// 加载播放器的背景
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics gBackground = g.create();
gBackground.drawImage(image, 0, 0, getWidth(), getHeight(), this);
gBackground.dispose();
}

MainPanel() {
nextone = new Button("img/rewind.png");
nextone.setBounds(new Rectangle(165, 60, 40, 35));
playerButton = new Button("img/play.png");
playerButton.setBounds(210, 60, 40, 35);
previousone = new Button("img/fastForward.png");
previousone.setBounds(255, 60, 40, 35);
vloumimage = new Button("img/sound.png");
vloumimage.setBounds(315, 65, 30, 25);
labelfilepath.setBounds(20, 100, 400, 35);
setLayout(null);
add(labelfilepath, null);
add(nextone, null);
add(previousone, null);
add(playerButton, null);
JSlider VolumControl = new JSlider();
VolumControl.setBounds(350, 60, 110, 35);
VolumControl.setPaintLabels(true);
add(VolumControl);
add(vloumimage);
this.setOpaque(false);
// setBackground(null);
setVisible(true);
}

}


我现在因为JPanle的背景是一张图片,所以当我在向JPane加新的控件的时候,就覆盖了。
向请问下要怎么样才能将JPanel设置成透明的 ,而不会被新加的控件覆盖呢?

------解决方案--------------------
setOpaque(true);