我现在要实现这样的功能
Container cc = getContentPane();
cc.setLayout(new BorderLayout());
我在面板cc的North区域放置一些按钮
现在想在“Center”再放置2个面板,a和b,其中a面板加载图片,作为背景图使用的,b面板用来进行实际的鼠标操作来用的,可以在b面板上进行画矩形,圆形,等操作。因此必须要让b面板浮在a面板表面,而且,b面板应该是透明的,这样才不会把a面板给挡住,背景图一直可以看见,请问下,有没有办法实现哦,最好能给一个简单的代码哦

解决方案 »

  1.   

    就用一个panel不行么?
    貌似两个的很难透明完全没注释,随便看看吧public class Test4Trans extends JFrame {
    private static final long serialVersionUID = 1L; public static void main(String[] args) {
    new Test4Trans();
    }

    public Test4Trans() {
    setSize(500, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final MyPanel mp = new MyPanel();
    getContentPane().add(mp);

    mp.addMouseListener(new MouseAdapter() {

    @Override
    public void mousePressed(MouseEvent mouseevent) {
    mp.lbq.add(new MyShape(MyShape.OVAL, mouseevent.getX() - 50, mouseevent.getY() - 50, 100, 100));
    mp.getGraphics().drawOval(mouseevent.getX() - 50, mouseevent.getY() - 50, 100, 100);
    }

    });


    setVisible(true);
    }

    class MyPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    Image backG = null;
    LinkedBlockingQueue<MyShape> lbq = new LinkedBlockingQueue<MyShape>(); public MyPanel() {
    super();
    setOpaque(false);
    try {
    backG = ImageIO.read(new File("c:\\1.jpg"));
    } catch (IOException e) {
    e.printStackTrace();
    }
    } @Override
    public void paintComponent(Graphics g) {
    if (null != backG)
    g.drawImage(backG, 0, 0, getWidth(), getHeight(), null);
    for (MyShape ms:lbq) {
    if (ms.type == MyShape.OVAL)
    g.drawOval(ms.x, ms.y, ms.width, ms.height);
    }
    super.paintComponent(g);

    }

    } class MyShape {
    final static int OVAL = 0;
    final static int RECT = 1;
    int x, y, width, height;
    int type = OVAL;

    public MyShape(int type, int x, int y, int width, int height) {
    this.type = type;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    }
    }
    }