下面列出了java.awt.Toolkit#getScreenSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Centers the child component relative to it's parent component
*
* @param parent
* @param child
* @param bStayOnScreen
*/
public static final void centerChildToParent(final Component parent, final Component child,
final boolean bStayOnScreen) {
int x = (parent.getX() + (parent.getWidth() / 2)) - (child.getWidth() / 2);
int y = (parent.getY() + (parent.getHeight() / 2)) - (child.getHeight() / 2);
if (bStayOnScreen) {
final Toolkit tk = Toolkit.getDefaultToolkit();
final Dimension ss = new Dimension(tk.getScreenSize());
if ((x + child.getWidth()) > ss.getWidth()) {
x = (int) (ss.getWidth() - child.getWidth());
}
if ((y + child.getHeight()) > ss.getHeight()) {
y = (int) (ss.getHeight() - child.getHeight());
}
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
}
child.setLocation(x, y);
}
/**
*
* @param width
* @param height
*/
@Override
public void setSize(int width, int height) {
if (BrowserControl.isMacOS()) {
super.setSize(width, height);
}
if (BrowserControl.isWindowsPlatform()) {
super.setSize(width, height + 25);
}
//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
//Calculate the frame location
int x = (screenSize.width - getWidth()) / 2;
int y = (screenSize.height - getHeight()) / 2;
//Set the new frame location
setLocation(x, y);
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
public void displayAlert() throws InterruptedException {
final Toolkit aToolkit = Toolkit.getDefaultToolkit();
final Dimension screen = aToolkit.getScreenSize();
final int xPosition = screen.width - (this.WIDTH); // Right edge of the
// screen
for (int i = 0; i < this.HEIGHT_ADD; i++) {
final int yPosition = screen.height - (this.HEIGHT + i); // Bottom
// edge
// of
// the
// screen
setBounds(xPosition, yPosition, this.WIDTH, this.HEIGHT);
Thread.sleep(10);
}
Thread.sleep(3000);
removeAlert();
}
/**
*
* @param preferredWidth
* @param myPreferredHeight
*/
protected void setSizeAndCenter(int preferredWidth, int myPreferredHeight) {
int preferredHeight = myPreferredHeight;
preferredHeight += (BrowserControl.isMacOS() ? 0 : 25);
super.setSize(preferredWidth, preferredHeight);
super.setPreferredSize(new Dimension(preferredWidth, preferredHeight));
//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
//Calculate the frame location
int x = (screenSize.width - getWidth()) / 2;
int y = (screenSize.height - getHeight()) / 2;
//Set the new frame location centered
setLocation(x, y);
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice graphicsDevice =
graphicsEnvironment.getDefaultScreenDevice();
final Dimension screenSize = toolkit.getScreenSize();
final Insets screenInsets = toolkit.getScreenInsets(
graphicsDevice.getDefaultConfiguration());
final Rectangle availableScreenBounds = new Rectangle(screenSize);
availableScreenBounds.x += screenInsets.left;
availableScreenBounds.y += screenInsets.top;
availableScreenBounds.width -= (screenInsets.left + screenInsets.right);
availableScreenBounds.height -= (screenInsets.top + screenInsets.bottom);
frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
availableScreenBounds.width, availableScreenBounds.height);
frame.setVisible(true);
Rectangle frameBounds = frame.getBounds();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
((SunToolkit) toolkit).realSync();
Rectangle maximizedFrameBounds = frame.getBounds();
if (maximizedFrameBounds.width < frameBounds.width
|| maximizedFrameBounds.height < frameBounds.height) {
throw new RuntimeException("Maximized frame is smaller than non maximized");
}
}
/**
*
* @Title: setLocation
* @Description: set component's location
* @param @param c
* @return void
* @throws
*/
public static void setLocation(Component c)
{
int winWidth = c.getWidth();
int winHeight = c.getHeight();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
c.setLocation(screenWidth / 2 - winWidth / 2, screenHeight / 2 - winHeight / 2);
}
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice graphicsDevice =
graphicsEnvironment.getDefaultScreenDevice();
final Dimension screenSize = toolkit.getScreenSize();
final Insets screenInsets = toolkit.getScreenInsets(
graphicsDevice.getDefaultConfiguration());
final Rectangle availableScreenBounds = new Rectangle(screenSize);
availableScreenBounds.x += screenInsets.left;
availableScreenBounds.y += screenInsets.top;
availableScreenBounds.width -= (screenInsets.left + screenInsets.right);
availableScreenBounds.height -= (screenInsets.top + screenInsets.bottom);
frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
availableScreenBounds.width, availableScreenBounds.height);
frame.setVisible(true);
Rectangle frameBounds = frame.getBounds();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
((SunToolkit) toolkit).realSync();
Rectangle maximizedFrameBounds = frame.getBounds();
if (maximizedFrameBounds.width < frameBounds.width
|| maximizedFrameBounds.height < frameBounds.height) {
throw new RuntimeException("Maximized frame is smaller than non maximized");
}
}
/**
*
* frame������
*
* @param jf
*/
public static void setFrameCenter (JFrame jf) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screen = toolkit.getScreenSize();// �����ݶ���
int x = (screen.width - jf.getWidth()) / 2;
int y = (screen.height - jf.getHeight()) / 2 - 32;
jf.setLocation(x, y);
}
private void positionFrame()
{
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = defaultToolkit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);
}
private void initView() {
JPanel contentPanel = new JPanel();
contentPanel.setBorder(new LineBorder(Colors.LIGHT_GRAY));
contentPanel.setLayout(new GridBagLayout());
controlPanel.add(closeLabel);
if (OSUtil.getOsType() != OSUtil.Mac_OS) {
setUndecorated(true);
contentPanel.add(controlPanel, new GBC(0, 0).setFill(GBC.BOTH).setWeight(1, 1).setInsets(5, 0, 0, 0));
}
JPanel titlePanel = new JPanel();
titlePanel.add(titleLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
//buttonPanel.add(loginButton, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(2, 1).setInsets(10, 0, 0, 0));
buttonPanel.add(remberPsw, new GBC(0, 1).setFill(GBC.HORIZONTAL).setWeight(1, 1).setInsets(20, 10, 0, 0));
buttonPanel.add(downloadLabel, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(1, 1).setInsets(20, 10, 0, 0));
buttonPanel.add(offlineLogin, new GBC(0, 2).setFill(GBC.HORIZONTAL).setWeight(1, 1).setInsets(20, 10, 0, 0));
editPanel.add(usernameField);
editPanel.add(passwordField);
editPanel.add(loginButton);
editPanel.add(buttonPanel);
editPanel.add(statusLabel);
add(contentPanel);
contentPanel.add(titlePanel, new GBC(0, 1).setFill(GBC.BOTH).setWeight(2, 1).setInsets(10, 10, 0, 10));
contentPanel.add(editPanel, new GBC(0, 2).setFill(GBC.BOTH).setWeight(2, 10).setInsets(10, 10, 0, 10));
Toolkit tk = Toolkit.getDefaultToolkit();
Launcher.currentWindowWidth = tk.getScreenSize().width;
Launcher.currentWindowHeight = tk.getScreenSize().height;
}
/**
* 屏幕中间显示对话窗口
*
* @param i_JDialog
*/
public void showCreate(JDialog i_JDialog)
{
Toolkit v_Toolkit = Toolkit.getDefaultToolkit();
int v_X = (v_Toolkit.getScreenSize().height - i_JDialog.getHeight()) / 2;
int v_Y = (v_Toolkit.getScreenSize().width - i_JDialog.getWidth() ) / 2;
i_JDialog.setLocation(v_Y ,v_X);
i_JDialog.setVisible(true);
}
public static void center(Window win){
Toolkit tkit = Toolkit.getDefaultToolkit();
Dimension sSize = tkit.getScreenSize();
Dimension wSize = win.getSize();
if(wSize.height > sSize.height){
wSize.height = sSize.height;
}
if(wSize.width > sSize.width){
wSize.width = sSize.width;
}
win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2);
}
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice graphicsDevice =
graphicsEnvironment.getDefaultScreenDevice();
final Dimension screenSize = toolkit.getScreenSize();
final Insets screenInsets = toolkit.getScreenInsets(
graphicsDevice.getDefaultConfiguration());
final Rectangle availableScreenBounds = new Rectangle(screenSize);
availableScreenBounds.x += screenInsets.left;
availableScreenBounds.y += screenInsets.top;
availableScreenBounds.width -= (screenInsets.left + screenInsets.right);
availableScreenBounds.height -= (screenInsets.top + screenInsets.bottom);
frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
availableScreenBounds.width, availableScreenBounds.height);
frame.setVisible(true);
Rectangle frameBounds = frame.getBounds();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
((SunToolkit) toolkit).realSync();
Rectangle maximizedFrameBounds = frame.getBounds();
if (maximizedFrameBounds.width < frameBounds.width
|| maximizedFrameBounds.height < frameBounds.height) {
throw new RuntimeException("Maximized frame is smaller than non maximized");
}
}
/**
* Add a label and a widget to the panel.
*
* @param name
* The name of the entry.
* @param label
* The label.
* @param widget
* The interactive entry to the right of the label.
* @param entry
* The object that contains user data.
*/
protected void _addPair(String name, JLabel label, Component widget, Object entry) {
// Surely there is a better layout manager in swing...
// Note that Box and BoxLayout do not work because they do not
// support gridded layout.
_constraints.gridwidth = 1;
_constraints.insets = _leftPadding;
_grid.setConstraints(label, _constraints);
_entryPanel.add(label);
_constraints.insets = _noPadding;
if ((_columns > 1) && (((_entries.size() + 1) % _columns) != 0)) {
_constraints.gridwidth = 1;
} else {
_constraints.gridwidth = GridBagConstraints.REMAINDER;
}
_grid.setConstraints(widget, _constraints);
_entryPanel.add(widget);
_entries.put(name, entry);
_labels.put(name, label);
_previous.put(name, getStringValue(name));
Dimension preferredSize = _entryPanel.getPreferredSize();
// Add some slop to the width to take in to account
// the width of the vertical scrollbar.
preferredSize.width += 25;
// Applets seem to need this, see CT/SigmaDelta
_widgetsHeight += widget.getPreferredSize().height;
preferredSize.height = _widgetsHeight;
Toolkit tk = Toolkit.getDefaultToolkit();
if (preferredSize.height > tk.getScreenSize().height) {
// Fudge factor to keep this window smaller than the screen
// height. CGSUnitBase and the Code Generator are good tests.
preferredSize.height = (int) (tk.getScreenSize().height * 0.75);
_entryScrollPane.setPreferredSize(preferredSize);
}
_entryScrollPane.setPreferredSize(preferredSize);
// Call revalidate for the scrollbar.
_entryPanel.revalidate();
}