[swing][布局管理器][layout]怎样实现层叠的组件 JPanel没有setOpaque(false)这个方法但你可以设置它的Visible为true或者False 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那你可能要实现自己的轻量级组件了,只有轻量级组件才可能是透明的swing是轻量级组件,但它是不透明的,自己写一个透明的Panel,很简单,直接继承java.awt.Container就可以了. 解决了大部分问题。但是还有个问题,三个层不能改变先后顺序。也就是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(); }} java 能不能一次性读取串口内所有数据 求助,swing的问题,在一个jpanel上加了图片就把其他按钮给挡住了,怎么办 关于double的问题。 请教类的问题!帮我解读类的问题. 抛出异常的实现问题 有谁用netbeans开发GUI程序 java图形设计 怎么控制Gif动画的速度? 麻烦大家帮我看看,这个代码哪有错误!多谢了! 简单问题 如何把树形结构的连线显示出来(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(); }
}