Guest User

Untitled

a guest
Aug 19th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. suitable LayoutManager for resizable components
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6. /**
  7. */
  8. public class DragLayout implements LayoutManager, java.io.Serializable
  9. {
  10. public DragLayout()
  11. {
  12. }
  13.  
  14. /**
  15. * Adds the specified component with the specified name to the layout.
  16. * @param name the name of the component
  17. * @param comp the component to be added
  18. */
  19. @Override
  20. public void addLayoutComponent(String name, Component comp) {}
  21.  
  22.  
  23. /**
  24. * Removes the specified component from the layout.
  25. *
  26. * @param comp the component to be removed
  27. */
  28. @Override
  29. public void removeLayoutComponent(Component component)
  30. {
  31. }
  32.  
  33. /**
  34. * Determine the minimum size on the Container
  35. *
  36. * @param target the container in which to do the layout
  37. * @return the minimum dimensions needed to lay out the
  38. * subcomponents of the specified container
  39. */
  40. @Override
  41. public Dimension minimumLayoutSize(Container parent)
  42. {
  43. synchronized (parent.getTreeLock())
  44. {
  45. return preferredLayoutSize(parent);
  46. }
  47. }
  48.  
  49. /**
  50. * Determine the preferred size on the Container
  51. *
  52. * @param parent the container in which to do the layout
  53. * @return the preferred dimensions to lay out the
  54. * subcomponents of the specified container
  55. */
  56. @Override
  57. public Dimension preferredLayoutSize(Container parent)
  58. {
  59. synchronized (parent.getTreeLock())
  60. {
  61. return getLayoutSize(parent);
  62. }
  63. }
  64.  
  65. /*
  66. * The calculation for minimum/preferred size it the same. The only
  67. * difference is the need to use the minimum or preferred size of the
  68. * component in the calculation.
  69. *
  70. * @param parent the container in which to do the layout
  71. */
  72. private Dimension getLayoutSize(Container parent)
  73. {
  74. Insets parentInsets = parent.getInsets();
  75. int x = parentInsets.left;
  76. int y = parentInsets.top;
  77. int width = 0;
  78. int height = 0;
  79.  
  80. // Get extreme values of the components on the container
  81.  
  82. for (Component component: parent.getComponents())
  83. {
  84. if (component.isVisible())
  85. {
  86. Point p = component.getLocation();
  87. Dimension d = component.getPreferredSize();
  88. x = Math.min(x, p.x);
  89. y = Math.min(y, p.y);
  90. width = Math.max(width, p.x + d.width);
  91. height = Math.max(height, p.y + d.height);
  92. }
  93. }
  94.  
  95. // Width/Height is adjusted if any component is outside left/top edge
  96.  
  97. if (x < parentInsets.left)
  98. width += parentInsets.left - x;
  99.  
  100. if (y < parentInsets.top)
  101. height += parentInsets.top - y;
  102.  
  103. // Adjust for insets
  104.  
  105. width += parentInsets.right;
  106. height += parentInsets.bottom;
  107. Dimension d = new Dimension(width, height);
  108.  
  109. return d;
  110. // return new Dimension(width, height);
  111. }
  112.  
  113. /**
  114. * Lays out the specified container using this layout.
  115. *
  116. * @param target the container in which to do the layout
  117. */
  118. @Override
  119. public void layoutContainer(Container parent)
  120. {
  121. synchronized (parent.getTreeLock())
  122. {
  123. Insets parentInsets = parent.getInsets();
  124.  
  125. int x = parentInsets.left;
  126. int y = parentInsets.top;
  127.  
  128. // Get X/Y location outside the bounds of the panel
  129.  
  130. for (Component component: parent.getComponents())
  131. {
  132. if (component.isVisible())
  133. {
  134. Point location = component.getLocation();
  135. x = Math.min(x, location.x);
  136. y = Math.min(y, location.y);
  137. }
  138. }
  139.  
  140. x = (x < parentInsets.left) ? parentInsets.left - x : 0;
  141. y = (y < parentInsets.top) ? parentInsets.top - y : 0;
  142.  
  143. // Set bounds of each component
  144.  
  145. for (Component component: parent.getComponents())
  146. {
  147. if (component.isVisible())
  148. {
  149. Point p = component.getLocation();
  150. Dimension d = component.getPreferredSize();
  151.  
  152. component.setBounds(p.x + x, p.y + y, d.width, d.height);
  153. }
  154. }
  155. }}
  156.  
  157. /**
  158. * Returns the string representation of this column layout's values.
  159. * @return a string representation of this layout
  160. */
  161. public String toString()
  162. {
  163. return "["
  164. + getClass().getName()
  165. + "]";
  166. }
  167.  
  168. public static void main( String[] args )
  169. {
  170. ComponentMover cm = new ComponentMover();
  171. cm.setEdgeInsets( new Insets(-100, -100, -100, -100) );
  172. // cm.setEdgeInsets( new Insets(10, 10, 10, 10) );
  173. cm.setAutoLayout(true);
  174.  
  175. JPanel panel = new JPanel( new DragLayout() );
  176. panel.setBorder( new MatteBorder(10, 10, 10, 10, Color.YELLOW) );
  177.  
  178. createLabel(cm, panel, "North", 150, 0);
  179. createLabel(cm, panel, "West", 0, 100);
  180. createLabel(cm, panel, "East", 300, 100);
  181. createLabel(cm, panel, "South", 150, 200);
  182. createLabel(cm, panel, "Center", 150, 100);
  183.  
  184. JFrame frame = new JFrame();
  185. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  186. frame.add( new JScrollPane(panel) );
  187. frame.pack();
  188. frame.setLocationRelativeTo( null );
  189. frame.setVisible( true );
  190. }
  191.  
  192. public static void createLabel(ComponentMover cm, JPanel panel, String text, int x, int y)
  193. {
  194. JLabel label = new JLabel( text );
  195. label.setOpaque(true);
  196. label.setBackground( Color.ORANGE );
  197. label.setLocation(x, y);
  198. panel.add( label );
  199. cm.registerComponent( label );
  200. }
  201.  
  202. }
Add Comment
Please, Sign In to add comment