Placing component on Glass Pane

前端 未结 4 2175
时光说笑
时光说笑 2020-11-22 03:45

I have a subclass of JLabel that forms a component of my GUI. I have implemented the ability to drag and drop the component from one container to another, but without any v

4条回答
  •  野性不改
    2020-11-22 04:49

    Besides the pointers to the LayerPane examples already provided, the issue with your original code centers around the setting of the preferred size of your label. You set it before the JLabel has been sized, so your:

    l.setPreferredSize(l.getSize());
    

    is ineffectual. If, on the other hand, you make that call after you make your call to setBounds, you will see your desired results. With that in mind, reorder this:

    l.setPreferredSize(l.getSize());
    l.setBounds(10, 10, 50, 20);
    

    to look like this:

    l.setBounds(10, 10, 50, 20);
    l.setPreferredSize(l.getSize());
    

提交回复
热议问题