Want more features on Pastebin? Sign Up, it's FREE!
Guest

PositionLayout in Java

By: fedarius on Aug 6th, 2011  |  syntax: Java  |  size: 1.96 KB  |  views: 56  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.Container;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7.  
  8. import org.tobiasbelch.java.javui.layout.AbsoluteConstraint;
  9. import org.tobiasbelch.java.javui.layout.Constraint;
  10. import org.tobiasbelch.java.javui.layout.PositionLayout;
  11. import org.tobiasbelch.java.javui.layout.Property;
  12. import org.tobiasbelch.java.javui.layout.RelativeConstraint;
  13.  
  14. public class PLTest {
  15.     public static void addComponentsToPane(Container pane) {        
  16.         List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
  17.         constraints.add(new AbsoluteConstraint (Property.Left, 10));
  18.         constraints.add(new RelativeConstraint (Property.Right, 0.50f));
  19.         constraints.add(new AbsoluteConstraint (Property.Top, 10));
  20.         constraints.add(new AbsoluteConstraint (Property.Bottom, 10));
  21.        
  22.         pane.add(new JButton("Button"), constraints);
  23.        
  24.         constraints = new ArrayList<Constraint<?>>();
  25.         constraints.add(new RelativeConstraint (Property.HCenter, .125f));
  26.         constraints.add(new AbsoluteConstraint (Property.Top, 10));
  27.         constraints.add(new RelativeConstraint (Property.Width, .25f));
  28.         constraints.add(new RelativeConstraint (Property.Height, .25f));
  29.        
  30.        
  31.         pane.add(new JButton("Button"), constraints);
  32.     }
  33.    
  34.     private static void createAndShowGUI() {
  35.        
  36.         JFrame frame = new JFrame("Demo");
  37.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.         Container c = frame.getContentPane();
  39.         c.setLayout(new PositionLayout());
  40.         addComponentsToPane(c);
  41.         frame.pack();
  42.         frame.setVisible(true);
  43.        
  44.         System.out.println(c);
  45.     }
  46.    
  47.     public static void main(String[] args) {
  48.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  49.             public void run() {
  50.                 createAndShowGUI();
  51.             }
  52.         });
  53.     }
  54. }
clone this paste RAW Paste Data