zoukankan      html  css  js  c++  java
  • Java基础 awt Frame 窗体在屏幕的中间显示

    •     JDK :OpenJDK-11
    •      OS :CentOS 7.6.1810
    •      IDE :Eclipse 2019‑03
    • typesetting :Markdown

    code

    package per.jizuiku.gui;
    
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.Toolkit;
    
    /**
     * @author 给最苦
     * @date 2019/06/30
     * @blog www.cnblogs.com/jizuiku
     */
    public class Demo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Frame f = new Frame();
    
            // 设置大小
            int width = 400;
            int height = 500;
            f.setSize(width, height);
    
            // 屏幕中间显示窗体
            center(f);
    
            // 可见
            f.setVisible(true);
    
        }
    
        /**
         * 
         * @param c
         */
        public static void center(Component c) {
            Toolkit kit = Toolkit.getDefaultToolkit();
            int x = (kit.getScreenSize().width - c.getWidth()) / 2;
            int y = (kit.getScreenSize().height - c.getHeight()) / 2;
            c.setLocation(x, y);
        }
    
    }
    
    

    sourceCode

    /**
        * Gets the size of the screen.  On systems with multiple displays, the
        * primary display is used.  Multi-screen aware display dimensions are
        * available from {@code GraphicsConfiguration} and
        * {@code GraphicsDevice}.
        * @return    the size of this toolkit's screen, in pixels.
        * @exception HeadlessException if GraphicsEnvironment.isHeadless()
        * returns true
        * @see       java.awt.GraphicsConfiguration#getBounds
        * @see       java.awt.GraphicsDevice#getDisplayMode
        * @see       java.awt.GraphicsEnvironment#isHeadless
        */
    public abstract Dimension getScreenSize()
        throws HeadlessException;
    
    /**
        * Returns the current width of this component.
        * This method is preferable to writing
        * {@code component.getBounds().width},
        * or {@code component.getSize().width} because it
        * doesn't cause any heap allocations.
        *
        * @return the current width of this component
        * @since 1.2
        */
    public int getWidth() {
        return width;
    }
    

    resource

    • [ JDK ] openjdk.java.net
    • [ doc - 参考 ] docs.oracle.com/en/java/javase/11
    • [ 规范 - 推荐 ] yq.aliyun.com/articles/69327
    • [ 规范 - 推荐 ] google.github.io/styleguide
    • [ 源码 ] hg.openjdk.java.net
    • [ OS ] www.centos.org
    • [ IDE ] www.eclipse.org/downloads/packages
    • [ 平台 ] www.cnblogs.com


    感谢帮助过 给最苦 的人们。
    Java、Groovy和Scala等基于JVM的语言,优秀,值得学习。
    规范的命名和代码格式等,有助于沟通和理解。
    JVM的配置、监控与优化,比较实用,值得学习。

  • 相关阅读:
    14 break
    13 for循环
    Python 3.7 将引入 dataclass 装饰器
    工程师如何在面试中脱颖而出
    如何避免 async/await 地狱
    命令行里打 cd 简直是浪费生命
    GitHub 十大 CI 工具
    GitHub CEO:GitHub 十年,感谢有你
    如何在 2 分钟内入睡(二战时期美国飞行员训练法)
    一分钟了解 TCP/IP 模型
  • 原文地址:https://www.cnblogs.com/jizuiku/p/11110219.html
Copyright © 2011-2022 走看看