我试图在多个面板上绘制一条线(图中的红线),但似乎无法实现。有什么建议吗?
在玻璃面板上绘制。
1)
Rob's http://tips4java.wordpress.com/2009/07/26/overlap-layout/,`2)` J(X)Layer,3)
JViewport,4)
GlassPane。 - mKorbel JComponent comp = Box.createVerticalBox();
final JComponent upper = new JPanel();
final JButton upperChild = new JButton("happy in upper");
upper.add(upperChild);
final JComponent lower = new JPanel();
final JButton lowerChild = new JButton("unhappy in lower");
lower.add(lowerChild);
comp.add(upper);
comp.add(lower);
LayerUI<JComponent> ui = new LayerUI<JComponent>() {
@Override
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
Rectangle u = SwingUtilities.convertRectangle(upper, upperChild.getBounds(), c);
Rectangle l = SwingUtilities.convertRectangle(lower, lowerChild.getBounds(), c);
g.setColor(Color.RED);
g.drawLine(u.x, u.y + u.height, l.x, l.y);
}
};
JLayer<JComponent> layer = new JLayer<JComponent>(comp, ui);