我有我的JPanel:
private int status = 0;
public serietv() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 848, 566);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel labelpanel = new JPanel(); //labelpanel is my problem
labelpanel.setBounds(92, 329, 625, 128);
contentPane.add(labelpanel);
labelpanel.setLayout(null);
//here my label go transparent
Color c=new Color(200,0,0,20);
labelpanel.setBackground(c);
labelpanel.setOpaque(true);
JLabel[] labelapp = new JLabel[1000]; //create an arrayJLabel for my images
但是当我点击JLabel“succ”时:
JLabel succ = new JLabel("");
succ.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
succ.setBounds(727, 350, 82, 79);
contentPane.add(succ);
succ.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent f) {
int x = 0;
int i = 0;
labelpanel.removeAll();
try {
for (i = status; i < status + 5; i++) {
RidimIcon locand = new RidimIcon();
labelapp[i].setBounds(30 + x, 15, 90, 100);
Border border = BorderFactory.createLineBorder(Color.BLACK, 3);
labelapp[i].setBorder(border);
labelapp[i].setIcon(locand.newicona(pathicon[i],labelapp[i]));
labelpanel.add(labelapp[i]);
x = x + 120;
}
status = i;
labelpanel.revalidate();
labelpanel.repaint();
} catch (NullPointerException e) {
labelpanel.revalidate();
labelpanel.repaint();
appoggio = i - status;
status = i - appoggio;
}
}
});
这是我的ridimIcon: 公共课RidimIcon {
ImageIcon image;
Image im;
Image myImg;
ImageIcon newImage;
int i=0;
public ImageIcon newicona (String img, JLabel lb){
image = new ImageIcon(img);
im = image.getImage();
myImg = im.getScaledInstance(lb.getWidth(), lb.getHeight(), Image.SCALE_SMOOTH);
newImage = new ImageIcon(myImg);
return newImage;
}
答案 0 :(得分:2)
Swing不知道如何绘制透明背景,因此您需要自己管理背景的绘画。
查看Backgrounds With Transparency了解更多信息和两个解决方案: