JPanel没有setOpaque(false)这个方法但你可以设置它的Visible为true或者False
解决方案 »
- 线程同步之synchronized关键字
- 初学JAVA JDK问题
- 关于General Output 和 Dos 界面输出的问题
- java 动态数据库连接
- jdk中的JAVA程序运行问题?请教!!
- 请教一个关于将String转化为Point的方法?,不知道有没有?
- 我在一个包中有两个类,可是一个类中无法引用另一个,请问为什么?
- 我的Java学习心得(2)
- JBuilder高手请进,JTree中删除了一个结点然后聚焦在它的父结点上该怎么办?最好还能触发valueChanged事件,在线等待...?
- SQL Server 2000 有专门的JDBC驱动程序吗?not odbc-jdbc bridge ?
- 如何把树形结构的连线显示出来(windows观感)?
- 求bytecode编辑器,能改import语句的
swing是轻量级组件,但它是不透明的,自己写一个透明的Panel,很简单,直接继承java.awt.Container就可以了.
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(); }
}