如何使用RemoveAll()具有trasparance面板

时间:2016-05-02 13:07:44

标签: java swing jpanel transparency

我有我的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

使用此代码,我的面板变为透明,红色透明背景如下: when i DONT click "Succ"

但是当我点击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;

  }

} 我的面板恢复正常,不透明..像这样: when i click succ 我需要删除所有组件,但不要删除透明度.. 我该如何解决?

1 个答案:

答案 0 :(得分:2)

Swing不知道如何绘制透明背景,因此您需要自己管理背景的绘画。

查看Backgrounds With Transparency了解更多信息和两个解决方案:

  1. 您自定义组件的绘画以绘制背景
  2. 使用包装器组件绘制背景。