List of usage examples for java.awt Component getToolkit
public Toolkit getToolkit()
From source file:Main.java
public static void center(Component c) { Dimension screenSize = c.getToolkit().getScreenSize(); Dimension componentSize = c.getSize(); int xPos = (screenSize.width - componentSize.width) / 2; xPos = Math.max(xPos, 0);//from w w w. j av a 2 s . c o m int yPos = (screenSize.height - componentSize.height) / 2; yPos = Math.max(yPos, 0); c.setLocation(new Point(xPos, yPos)); }
From source file:Main.java
public static void center(Component c) { Dimension screenSize = c.getToolkit().getScreenSize(); screenSize.width -= BORDER_SIZE;//from w ww . java 2 s. c o m screenSize.height -= BORDER_SIZE; Dimension componentSize = c.getSize(); int xPos = (screenSize.width - componentSize.width) / 2; xPos = Math.max(xPos, 0); int yPos = (screenSize.height - componentSize.height) / 2; yPos = Math.max(yPos, 0); c.setLocation(new Point(xPos, yPos)); }
From source file:Main.java
/** * Use this static method if you want to center a component in Window. * * @param component// ww w . j ava 2s . co m * the component you want to center in window */ public static void centerComponentInWindow(Component component) { Dimension dimension = component.getToolkit().getScreenSize(); component.setLocation((int) ((dimension.getWidth() - component.getWidth()) / 2), (int) ((dimension.getHeight() - component.getHeight()) / 2)); component.validate(); component.repaint(); }
From source file:BooksDemo.java
public static void centerOnScreen(Component component) { Dimension paneSize = component.getSize(); Dimension screenSize = component.getToolkit().getScreenSize(); component.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2); }
From source file:Main.java
/** * Use this static method if you want to center and set its position * compared to the size of the current users screen size. Valid percent is * between +-(0-100) minus is treated as plus, bigger than 100 is always set * to 100./*w ww. ja v a 2 s . c o m*/ * * @param component * the component you want to center and set size on * @param percentOfScreen * the percent of the current screensize you want the component * to be */ public static void centerComponentInWindow(Component component, int percentOfScreen) { if (percentOfScreen < 0) { centerComponentInWindow(component, -percentOfScreen); return; } if (percentOfScreen > 100) { centerComponentInWindow(component, 100); return; } double percent = percentOfScreen / 100.d; Dimension dimension = component.getToolkit().getScreenSize(); component.setSize((int) (dimension.getWidth() * percent), (int) (dimension.getHeight() * percent)); centerComponentInWindow(component); }