1 19 20 package com.sslexplorer.upgrade; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.GraphicsConfiguration ; 25 import java.awt.GraphicsDevice ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.awt.Rectangle ; 29 import java.awt.Toolkit ; 30 31 import javax.swing.JComponent ; 32 import javax.swing.SwingConstants ; 33 34 39 public class UIUtil implements SwingConstants { 40 41 42 43 55 public static void jGridBagAdd(JComponent parent, Component componentToAdd, GridBagConstraints constraints, int pos) { 56 if (!(parent.getLayout() instanceof GridBagLayout )) { 57 throw new IllegalArgumentException ("parent must have a GridBagLayout"); 58 } 59 60 GridBagLayout layout = (GridBagLayout ) parent.getLayout(); 62 63 constraints.gridwidth = pos; 65 layout.setConstraints(componentToAdd, constraints); 66 parent.add(componentToAdd); 67 } 68 69 76 public static void positionComponent(int p, Component c) { 77 78 positionComponent(p, c, c); 79 80 } 81 82 public static void positionComponent(int p, Component c, Component o) { 83 Rectangle d = null; 84 88 try { 89 90 94 95 GraphicsConfiguration config = o.getGraphicsConfiguration(); 97 GraphicsDevice dev = config.getDevice(); 98 d = config.getBounds(); 99 100 } catch (Throwable t) { 102 } 103 positionComponent(p, c, d); 104 105 } 106 107 public static void positionComponent(int p, Component c, Rectangle d) { 108 if (d == null) { 109 Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); 110 d = new Rectangle (0, 0, s != null ? s.width : 800, s != null ? s.height : 600); 111 System.out.println("Could not get metrics from graphics config, using default " + d); 112 } 113 114 switch (p) { 115 case NORTH_WEST: 116 c.setLocation(d.x, d.y); 117 break; 118 case NORTH: 119 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y); 120 break; 121 case NORTH_EAST: 122 c.setLocation(d.x + (d.width - c.getSize().width), d.y); 123 break; 124 case WEST: 125 c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2); 126 break; 127 case SOUTH_WEST: 128 c.setLocation(d.x, d.y + (d.height - c.getSize().height)); 129 break; 130 case EAST: 131 c.setLocation(d.x + d.width - c.getSize().width, d.y + (d.height - c.getSize().height) / 2); 132 break; 133 case SOUTH_EAST: 134 c.setLocation(d.x + (d.width - c.getSize().width), d.y + (d.height - c.getSize().height) - 30); 135 break; 136 case CENTER: 137 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y + (d.height - c.getSize().height) / 2); 138 break; 139 } 140 } 141 } 142 | Popular Tags |