【问题标题】:How to check if you're on a Mac or Windows to resize GUI in Java?如何检查您是在 Mac 还是 Windows 上调整 Java 中的 GUI 大小?
【发布时间】:2012-06-01 20:51:25
【问题描述】:

我编写了一个需要在 Mac 和 Windows 上运行的程序。就 GUI 而言,它在 Windows 上看起来不错,但 JFrame 在 Mac 上太小了。 我使用了 GridBag 布局,但没有使用绝对布局,这在与此问题类似的答案中已提出。 我尝试过使用 pack() 但它不适用于这个 GUI。它甚至不会调整框架的大小以适应菜单栏。我正在使用 setSize(X, Y) 但有没有办法检查用户是否在 Mac 上,然后相应地更改大小? 我也尝试过使用 setMinimumSize() 然后 pack() 但 pack 无论如何都没有做任何事情。

这是我的帧代码位;以防由于 pack() 无法正常工作而出现任何问题。

try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) { }

    try {
        timeCodeMask = new MaskFormatter(" ## : ## : ## : ## ");
    } catch(ParseException e) {
        errorMessage("Warning!", "Formatted text field hasn't worked, text fields will not be formatted.");
    }

    try {
        activePanel = new JPanelSwapper("src/bg.png");
    } catch(IOException e) {
        errorMessage("Warning!", "Background image has not loaded, continuing without one.");
    }

    FPS = 24;

    calculatorPanel = calculatorPanel();
    converterPanel = converterPanel();

    activePanel.setPanel(calculatorPanel());
    previousTimes = new TimeStore();
    resultTimes = new TimeStore();
    previousConversions = new TimeStore();

    frame = new JFrame("TimeCode Calculator & Converter");
    ImageIcon frameIcon = new ImageIcon("src/frame icon.png");
    frame.setIconImage(frameIcon.getImage());
    frame.setExtendedState(JFrame.NORMAL);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setSize(WIDTH, HEIGHT);
        //frame.pack();
        frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));
        frame.pack();
        frame.setResizable(false);

    frame.setJMenuBar(menuBar());
    frame.getContentPane().add(activePanel);
    frame.setBackground(Color.WHITE);
    frame.setVisible(true);

    screen = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((screen.width - WIDTH) / 2, (screen.height - HEIGHT) / 2);

提前致谢!

【问题讨论】:

  • “我试过了..” 尝试发布SSCCE
  • 你在做什么需要这些信息。
  • 我的问题是它在不同操作系统上的外观,所以它取决于你运行它的操作系统来查看问题。

标签: java windows macos swing layout-manager


【解决方案1】:

您可以通过系统属性找出所使用的操作系统。

例如:

System.getProperty("os.name"); //returns name of os as string
System.getProperty("os.version"); //returns version of os as string
System.getProperty("os.arch"); //returns architecture of os as string

根据条件检查:

public String getOS() {
    String os = System.getProperty("os.name").toLowerCase();

    if(os.indexOf("mac") >= 0){
       return "MAC";
    }
    else if(os.indexOf("win") >= 0){
       return "WIN";
    }
    else if(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0){
       return "LINUX/UNIX";
    }
    else if(os.indexOf("sunos") >= 0){
       return "SOLARIS";
    }

【讨论】:

  • 准确返回什么以及如何使用它来检查条件?
  • 我添加了第二个代码块,您可以根据条件检查它。
  • 仅供参考,如果我要检查 Linux 的索引是什么?
  • 我在第一篇文章中进一步添加了 Linux/Unix 和 Solaris ;)
  • Linux。请参阅mindprod.com/jgloss/properties.html#OSNAME 以获得更完整的列表(谷歌再次提供帮助)
猜你喜欢
  • 1970-01-01
  • 2019-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
相关资源
最近更新 更多