zoukankan      html  css  js  c++  java
  • Java基础之创建窗口——使窗口在屏幕居中(TryWindow2/TryWindow3)

    控制台程序。

    1、使用ToolKit对象在屏幕的中心显示窗口,将窗口的宽度和高度设置为屏幕的一半:

     1 import javax.swing.JFrame;
     2 import javax.swing.SwingUtilities;
     3 import java.awt.Toolkit;
     4 import java.awt.Dimension;
     5 
     6 public class TryWindow2 {
     7   public static void createWindow(){
     8     JFrame aWindow = new JFrame("This is the Window Title");
     9     Toolkit theKit = aWindow.getToolkit();                             // Get the window toolkit
    10     Dimension wndSize = theKit.getScreenSize();                        // Get screen size
    11 
    12     // Set the position to screen center & size to half screen size
    13     aWindow.setBounds(wndSize.width/4, wndSize.height/4,               // Position
    14                       wndSize.width/2, wndSize.height/2);              // Size
    15     aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    16     aWindow.setVisible(true);                                          // Display the window
    17   }
    18 
    19   public static void main(String[] args) {
    20     SwingUtilities.invokeLater(new Runnable() {
    21             public void run() {
    22                 createWindow();
    23             }
    24         });
    25   }
    26 }

    组件拥有的由java.awt.Dimension对象定义的“首选”尺寸,Dimension对象封装了宽度和高度值。
    Component类中定义的另一个重要方法是getToolkit(),这个方法返回Toolkit类型的对象,其中包含了与运行应用程序有关的环境信息,包含屏幕尺寸(以像素为单位)。使用getToolkit()方法可以设置窗口在屏幕中的大小和位置。

    2、调用JFrame对象从Window类继承而来的setLocationRelativeTo()方法,这个方法使窗口在传送为参数的另一个组件中居中放置。如果参数为null,这个方法就使窗口在主显示器中居中放置。

     1 import javax.swing.JFrame;
     2 import javax.swing.SwingUtilities;
     3 
     4 public class TryWindow3 {
     5   public static void createWindow(){
     6     JFrame aWindow = new JFrame("This is the Window Title");
     7     int windowWidth = 400;                                             // Window width in pixels
     8     int windowHeight = 150;                                            // Window height in pixels
     9     aWindow.setSize(windowWidth, windowHeight);                        // Set window size
    10     aWindow.setLocationRelativeTo(null);                               // Center window
    11 
    12     aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    13     aWindow.setVisible(true);                                          // Display the window
    14   }
    15 
    16   public static void main(String[] args) {
    17     SwingUtilities.invokeLater(new Runnable() {
    18             public void run() {
    19                 createWindow();
    20             }
    21         });
    22   }
    23 }
  • 相关阅读:
    HDU4366 Successor 线段树+预处理
    POJ2823 Sliding Window 单调队列
    HDU寻找最大值 递推求连续区间
    UVA846 Steps 二分查找
    HDU3415 Max Sum of MaxKsubsequence 单调队列
    HDU时间挑战 树状数组
    UVA10168 Summation of Four Primes 哥德巴赫猜想
    UESTC我要长高 DP优化
    HDUChess 递推
    HDU4362 Dragon Ball DP+优化
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3463942.html
Copyright © 2011-2022 走看看