如何检查您是使用Mac还是Windows来调整Java中的GUI大小?

lil*_*oka 2 java windows macos swing layout-manager

我编写了一个需要在Mac和Windows上运行的程序.就GUI而言,它在Windows上看起来很好,但JFrame在Mac上太小了.我已经使用了GridBag布局,没有使用绝对值,这在类似于这个问题的答案中已经提出过.我尝试过使用pack(),但它无法正常使用此GUI.它甚至没有调整框架大小以适应菜单栏.我正在使用setSize(X,Y),但有没有办法检查用户是否在Mac上,然后相应地更改大小?我也试过使用setMinimumSize()然后打包()但是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);
Run Code Online (Sandbox Code Playgroud)

提前致谢!

nul*_*ntr 5

您可以找出系统属性使用的操作系统.

例如:

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
Run Code Online (Sandbox Code Playgroud)

检查条件:

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";
    }
Run Code Online (Sandbox Code Playgroud)

  • Linux操作系统.有关更完整的列表,请访问http://mindprod.com/jgloss/properties.html#OSNAME(谷歌再次帮助) (2认同)