布局管理器实际上执行3件事:
设置组件的位置。由于您需要能够拖动组件,因此您不希望布局管理器执行此 *** 作。
设置组件的大小。由于您需要能够调整组件的大小,因此您不希望这样做。但是,您可能要根据组件的首选大小为组件指定默认大小。这样,您无需在创建组件时指定大小。
根据添加到其上的组件确定父面板的首选大小。这将允许滚动窗格正常运行,因为可以根据需要添加/删除滚动条。因此,您需要确定拖动方式的行为。也就是说,允许您将组件拖动到面板的当前边界之外。如果是这样,面板的首选大小应自动增加。
是否有任何已实现的LayoutManager允许组件的绝对定位
我一直在与接近您需求的布局管理器一起玩耍。它被设计为可与
MovingWindows垃圾桶提供的链接中的ComponentMover类一起使用。
这是我针对此类的测试代码:
import java.awt.*;import javax.swing.*;import javax.swing.border.*;public class DragLayout implements LayoutManager, java.io.Serializable{ public DragLayout() { } @Override public void addLayoutComponent(String name, Component comp) {} @Override public void removeLayoutComponent(Component component) { } @Override public Dimension minimumLayoutSize(Container parent) { synchronized (parent.getTreeLock()) { return preferredLayoutSize(parent); } } @Override public Dimension preferredLayoutSize(Container parent) { synchronized (parent.getTreeLock()) { return getLayoutSize(parent); } } private Dimension getLayoutSize(Container parent) { Insets parentInsets = parent.getInsets(); int x = parentInsets.left; int y = parentInsets.top; int width = 0; int height = 0; // Get extreme values of the components on the container for (Component component: parent.getComponents()) { if (component.isVisible()) { Point p = component.getLocation(); Dimension d = component.getPreferredSize(); x = Math.min(x, p.x); y = Math.min(y, p.y); width = Math.max(width, p.x + d.width); height = Math.max(height, p.y + d.height); } } // Width/Height is adjusted if any component is outside left/top edge if (x < parentInsets.left) width += parentInsets.left - x; if (y < parentInsets.top) height += parentInsets.top - y; // Adjust for insets width += parentInsets.right; height += parentInsets.bottom; Dimension d = new Dimension(width, height); return d;// return new Dimension(width, height); } @Override public void layoutContainer(Container parent) { synchronized (parent.getTreeLock()) { Insets parentInsets = parent.getInsets(); int x = parentInsets.left; int y = parentInsets.top; // Get X/Y location outside the bounds of the panel for (Component component: parent.getComponents()) { if (component.isVisible()) { Point location = component.getLocation(); x = Math.min(x, location.x); y = Math.min(y, location.y); } } x = (x < parentInsets.left) ? parentInsets.left - x : 0; y = (y < parentInsets.top) ? parentInsets.top - y : 0; // Set bounds of each component for (Component component: parent.getComponents()) { if (component.isVisible()) { Point p = component.getLocation(); Dimension d = component.getPreferredSize(); component.setBounds(p.x + x, p.y + y, d.width, d.height); } } }} public String toString() { return "[" + getClass().getName() + "]"; } public static void main( String[] args ) { ComponentMover cm = new ComponentMover(); cm.setEdgeInsets( new Insets(-100, -100, -100, -100) );// cm.setEdgeInsets( new Insets(10, 10, 10, 10) ); cm.setAutoLayout(true); JPanel panel = new JPanel( new DragLayout() ); panel.setBorder( new MatteBorder(10, 10, 10, 10, Color.YELLOW) ); createLabel(cm, panel, "North", 150, 0); createLabel(cm, panel, "West", 0, 100); createLabel(cm, panel, "East", 300, 100); createLabel(cm, panel, "South", 150, 200); createLabel(cm, panel, "Center", 150, 100); Jframe frame = new Jframe(); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.add( new JScrollPane(panel) ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } public static void createLabel(ComponentMover cm, JPanel panel, String text, int x, int y) { JLabel label = new JLabel( text ); label.setOpaque(true); label.setBackground( Color.ORANGE ); label.setLocation(x, y); panel.add( label ); cm.registerComponent( label ); }}
对于此布局,始终假定大小为首选大小。您将需要更改此设置。大小为(0,0)时,可以将大小设置为首选大小。在确定父容器的首选大小时,您还需要使用组件的大小(而不是首选大小)。
可以配置ComponentMover类,以允许您将组件拖动到父容器的边界之外,或者将组件保持在边界之内。如果允许将组件移出边界,则将自动调整首选大小,以考虑组件的新位置。
如果将组件拖动到顶部或左侧边界之外,则所有组件都将移动(向右或向下),请确保没有组件位于负位置。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)