1 18 19 package org.apache.jorphan.gui; 20 21 import java.awt.Component ; 22 import java.awt.Dimension ; 23 24 31 public final class ComponentUtil 32 { 33 43 public static void centerComponentInWindow( 44 Component component, 45 int percentOfScreen) 46 { 47 if (percentOfScreen < 0) 48 { 49 centerComponentInWindow(component, -percentOfScreen); 50 return; 51 } 52 if (percentOfScreen > 100) 53 { 54 centerComponentInWindow(component, 100); 55 return; 56 } 57 double percent = percentOfScreen / 100.d; 58 Dimension dimension = component.getToolkit().getScreenSize(); 59 component.setSize( 60 (int) (dimension.getWidth() * percent), 61 (int) (dimension.getHeight() * percent)); 62 centerComponentInWindow(component); 63 } 64 65 70 public static void centerComponentInWindow(Component component) 71 { 72 Dimension dimension = component.getToolkit().getScreenSize(); 73 74 component.setLocation( 75 (int) ((dimension.getWidth() - component.getWidth()) / 2), 76 (int) ((dimension.getHeight() - component.getHeight()) / 2)); 77 component.validate(); 78 component.repaint(); 79 } 80 81 88 public static void centerComponentInComponent( 89 Component parent, 90 Component toBeCentered) 91 { 92 toBeCentered.setLocation( 93 (int) parent.getX() 94 + (int) ((parent.getWidth() - toBeCentered.getWidth()) / 2), 95 (int) parent.getY() 96 + (int) ((parent.getHeight() - toBeCentered.getHeight()) / 2)); 97 98 toBeCentered.validate(); 99 toBeCentered.repaint(); 100 } 101 102 105 private ComponentUtil() 106 { 107 } 108 } 109 | Popular Tags |