JPanel没有setOpaque(false)这个方法但你可以设置它的Visible为true或者False

解决方案 »

  1.   

    那你可能要实现自己的轻量级组件了,只有轻量级组件才可能是透明的
    swing是轻量级组件,但它是不透明的,自己写一个透明的Panel,很简单,直接继承java.awt.Container就可以了.
      

  2.   

    解决了大部分问题。但是还有个问题,三个层不能改变先后顺序。也就是Z轴顺序import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;class FloatPanel
        extends JPanel {
      private Rectangle rectangle; // Panel position
      private int lxPressed = 0; // x when pressed
      private int lyPressed = 0; // y when pressed
      private int pw = 0; // width of it's parent container(if exits)
      private int ph = 0; // height of it's parent container(if exits)
      private int lxDragged = 0; // x when pressed
      private int lyDragged = 0; // y when pressed
      private boolean draggable = false; // drag tag  public FloatPanel(Rectangle r) {
        rectangle = r;
        this.setBounds(r);
        this.addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            Object o;
            draggable = false;
            if (e.getButton() == e.BUTTON1
                && (o = e.getSource())instanceof FloatPanel) {
              draggable = true;
              lxPressed = e.getX();
              lyPressed = e.getY();
              pw = 0;
              ph = 0;
              if ( ( (FloatPanel) o).getParent() != null) {
                Object o1 = ( (FloatPanel) o).getParent();
                if (o1 instanceof Container) {
                  pw = ( (Container) o1).getWidth();
                  ph = ( (Container) o1).getHeight();
                }
              }
            }
          }
        });
        this.addMouseMotionListener(new MouseMotionAdapter() {
          public void mouseDragged(MouseEvent e) {
            if (draggable) {
              lxDragged = e.getX();
              lyDragged = e.getY();          rectangle.x = lxDragged - lxPressed + rectangle.x;
              rectangle.y = lyDragged - lyPressed + rectangle.y;
              // check if the float panel is out of it's container's right border
              if (rectangle.x + rectangle.width >= pw) {
                rectangle.x = pw - rectangle.width;
              }
              if (rectangle.y + rectangle.height >= ph) {
                rectangle.y = ph - rectangle.height;
              }
              // check if the float panel is out of it's container's left border
              //    BTW, it also check the extra returned offset by the steps above
              if (rectangle.x < 0) {
                rectangle.x = 0;
              }
              if (rectangle.y < 0) {
                rectangle.y = 0;
              }          rectangle.setLocation(rectangle.x, rectangle.y);
              ( (FloatPanel) e.getSource()).setBounds(rectangle);
            }
          }
        });
      }
    }public class TestFloatPanel
        extends JPanel {
      JPanel jPanel1;
      JPanel jPanel2;
      JPanel jPanel3;
      public TestFloatPanel() {
        this.setLayout(null);
        jPanel1 = new FloatPanel(new Rectangle(43, 72, 93, 112));
        jPanel1.setBackground(Color.lightGray);
        jPanel1.setLayout(new BorderLayout());
        jPanel1.add(new JLabel("Layer 1"), BorderLayout.CENTER);
        jPanel2 = new FloatPanel(new Rectangle(102, 111, 141, 121));
        jPanel2.setBackground(Color.gray);
        jPanel2.setLayout(new BorderLayout());
        jPanel2.add(new JLabel("Layer 2"), BorderLayout.CENTER);
        jPanel3 = new FloatPanel(new Rectangle(168, 88, 124, 57));
        jPanel3.setBackground(Color.WHITE);
        jPanel3.setLayout(new BorderLayout());
        jPanel3.add(new JLabel("Layer 3"), BorderLayout.CENTER);
        this.add(jPanel1, null);
        this.add(jPanel2, null);
        this.add(jPanel3, null);
      }
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        Container cp = frame.getContentPane();
        cp.setLayout(new BorderLayout());
    cp.add(new TestFloatPanel(), BorderLayout.CENTER);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.show();  }
    }