Java没有使用正确的大小创建JFrame。

huangapple 未分类评论48阅读模式
英文:

Java not creating JFrame with correct size

问题

我正在使用尺寸为 800 x 600 创建一个 JFrame,在我的 MacBook Pro 2018 上,根据“关于本机”选项卡的显示,其分辨率为 2560 x 1600。然而,当我显示窗口时,它的大小比预期要大得多,宽度维度超过屏幕尺寸的 50%(下面是测试代码)。

##由 AT 编辑。

原始代码已经无法达到我的预期,但我需要排除一些可能性并获取更多信息,因此将其编辑为以下代码。

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class TestFrameSize {
    
    // 添加属性以在其他方法中引用frame
    JFrame frame; 

    public TestFrameSize() {
        Dimension size = new Dimension(800,600);
        frame = new JFrame();
        frame.setPreferredSize(size);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        System.out.println("Frame size: " + frame.getSize());
        printProperty("java.version");
        printProperty("os.name");
        getImage();
    }
    
    private void printProperty(String name) {
        System.out.println(name + ": " + System.getProperty(name));
    }
    
    private BufferedImage getImage() {
        Dimension d = frame.getSize();
        BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        
        Graphics2D g = bi.createGraphics();
        Window w = frame;
        w.paint(g);
        g.dispose();
        JOptionPane.showMessageDialog(frame, new ImageIcon(bi));
        
        return bi;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                TestFrameSize test = new TestFrameSize();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

结果如下图所示:
Java没有使用正确的大小创建JFrame。

对于 2560x1600 的显示器,此窗口显然比 800x600 要大!这似乎很奇怪,我尝试查看 Java 认为我的屏幕尺寸是多少:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

结果显示是 1440 x 900,这与屏幕应有的尺寸不符。难道是 Java 误解了我的显示器尺寸吗?

我有点困惑,经过搜索,我没有找到明显的答案,我不确定这是 Mac 的问题、Java 的问题,还是“我不理解屏幕如何工作”的问题。

英文:

I'm creating a JFrame with size 800 x 600 on a MacBook Pro 2018 that should according to the "About my Mac" tab has a resolution of 2560 x 1600, however when I display the window it appears much larger than it should be with the width dimension being over 50% of the screen size (test code below).

##Edit by AT.

The original code was already not behaving to my expectation, but I needed to dispel some possibilities & get more info so edited it to this code.

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class TestFrameSize {
    
    // made attibute to refer to frame in other methods
    JFrame frame; 

    public TestFrameSize() {
        Dimension size = new Dimension(800,600);
        frame = new JFrame();
        frame.setPreferredSize(size);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        System.out.println("Frame size: " + frame.getSize());
        printProperty("java.version");
        printProperty("os.name");
        getImage();
    }
    
    private void printProperty(String name) {
        System.out.println(name + ": " + System.getProperty(name));
    }
    
    private BufferedImage getImage() {
        Dimension d = frame.getSize();
        BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        
        Graphics2D g = bi.createGraphics();
        Window w = frame;
        w.paint(g);
        g.dispose();
        JOptionPane.showMessageDialog(frame, new ImageIcon(bi));
        
        return bi;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                TestFrameSize test = new TestFrameSize();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

And the result:
Java没有使用正确的大小创建JFrame。

For a display of 2560x1600 this window is certainly larger than 800x600 should be! This seemed strange and I tried to look up what Java thinks my screen size is with

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

and this gave the answer 1440 x 900 which is not what the screen should be. Is it that Java is misinterpreting the size of my display?

I'm at a bit of a loss here, googling around I couldn't see any obvious answers and I'm not sure whether this is a Mac thing, a Java thing, or a "I'm not understanding how the screen works" thing.

huangapple
  • 本文由 发表于 2020年7月23日 13:37:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/63047544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定