简体   繁体   English

JFrame.setExtendedState()在Mac上无法正常工作

[英]JFrame.setExtendedState() not working properly on Macs

I am using a javax.swing.JFrame with java 1.8.0_211 and I want to have my JFrame maximized over the whole screen. 我正在使用带有java 1.8.0_211的javax.swing.JFrame,我希望在整个屏幕上最大化我的JFrame。

I am using the following code: 我使用以下代码:

public class MitarbeiterUI extends javax.swing.JFrame {


    /**
     * Creates new form MitarbeiterUI
     */
    public MitarbeiterUI(Mitarbeiter mitarbt) {

        initComponents();

        this.setExtendedState(MAXIMIZED_BOTH);
        this.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        this.setVisible(true);
    }

    [...]
}

This however only causes the window to align to the screen horizontally, but not vertically. 但是这只会使窗口水平对齐屏幕,而不是垂直对齐。 It does work with both Windows And Linux systems, so it has to be a Mac specific issue. 它适用于Windows和Linux系统,因此它必须是Mac特定的问题。 Is there any way around this mighty annoying bug? 围绕这个强大的烦人的bug有什么办法吗?

Edit: Executable code sample 编辑:可执行代码示例

import java.awt.Toolkit;

public class SecondFrame extends javax.swing.JFrame {

/**
 * Creates new form SecondFrame
 */
public SecondFrame() {
    initComponents();

    this.setExtendedState(MAXIMIZED_BOTH);
    this.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
    this.setVisible(true);
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    addComponentListener(new java.awt.event.ComponentAdapter() {
        public void componentResized(java.awt.event.ComponentEvent evt) {
            formComponentResized(evt);
        }
    });

    jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 563, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 877, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(134, 134, 134)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)))
            .addContainerGap(25, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addGap(19, 19, 19)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(241, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void formComponentResized(java.awt.event.ComponentEvent evt) {                                      
    jLabel1.setText("W: " + this.getWidth() + ", H: " + this.getHeight());
}                                     

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new SecondFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}

Edit2: It seems the issue lies with the screen sizes the Macs we used have. 编辑2:似乎问题在于我们使用的Mac的屏幕尺寸。 They both sport a resolution greater than fullHD, but both are automatically scaling down to 1440 to 900p, which makes the JPanel we use in our class bigger than the resized screen and in turn has it cut off at the bottom. 它们的分辨率都高于全高清,但两者都会自动缩小到1440到900p,这使得我们在我们班级使用的JPanel比调整大小的屏幕大,反过来又在底部切断。 So I guess the question is now how can I either tell those Macs to not resize their screens, or how to add scrollbars to the JFrame without having to redo the whole class. 所以我想问题是现在我怎么能告诉那些Mac不调整屏幕大小,或者如何将滚动条添加到JFrame而不必重做整个类。

this.setExtendedState(MAXIMIZED_BOTH);
this.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
this.setVisible(true);

The point of using setExtendedState(...) is that you don't need to use the setSize(...) method. 使用setExtendedState(...)是你不需要使用setSize(...)方法。

So the code should simply be: 所以代码应该只是:

this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);

The setSize() statement is overriding the size calculated by the setExtendedState(...) method. setSize()语句将覆盖setExtendedState(...)方法计算的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM