在 Java 中获取 JFrame 窗口大小信息


若要获取 JFrame 窗口大小信息,你可以使用以下命令 −

environment.getMaximumWindowBounds();

对于屏幕大小 −

config.getBounds()

对于框架大小 −

frame.getSize());

以下是如何获取 JFrame 窗口大小信息的示例 −

示例

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class SwingDemo {
   public static void main(String[] args) {
      GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
      Rectangle bounds = environment.getMaximumWindowBounds();
      System.out.println("Screen Bounds = " + bounds);
      GraphicsDevice device = environment.getDefaultScreenDevice();
      GraphicsConfiguration config = device.getDefaultConfiguration();
      System.out.println("Screen Size = " + config.getBounds());
      JFrame frame = new JFrame("Frame Info");
      frame.setSize(500, 300); frame.setVisible(true);
      System.out.println("Frame Size = " + frame.getSize());
   }
}

输出

Screen Bounds = java.awt.Rectangle[x=0,y=0,width=1366,height=728]
Screen Size = java.awt.Rectangle[x=0,y=0,width=1366,height=768]
Frame Size = java.awt.Dimension[width=500,height=300]

更新于: 30-7-2019

2K+ 浏览量

开启你的职业生涯

通过完成课程获得认证

立即开始
广告