JLabel 在全屏独占模式下消失

JLabel disappears in fullscreen exclusive mode

我正在使用 NetBeans GUI 生成器编写 java.swing 屏幕保护程序。我最近尝试使用 FullScreen Exclusive 模式以使其看起来更好,但现在我的图像根本不显示。

我正在使用 jLabel 和 setIcon 方法显示图像,并使用摆动计时器循环浏览它们。

下面是代码:

public class AdFrame extends javax.swing.JFrame {
    ActionListener changeImage;
    Timer timer;
    GraphicsDevice thispc;
    Window myWindow;
/**
 * Creates new form FifthFrame
 */
    public AdFrame() {
        initComponents();
    }

/**
 * 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);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setOpaque(false);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/btc_gui/newpackage/btc-zg.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(30, 30, 30)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(41, 41, 41)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    getContentPane().add(jPanel1, new java.awt.GridBagConstraints());

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

/**
 * @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(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            AdFrame ff1 = new AdFrame();
            ff1.setExtendedState(JFrame.MAXIMIZED_BOTH);
            ff1.setVisible(true);
            ff1.thispc = ff1.getGraphicsConfiguration().getDevice();
            ff1.myWindow = new Window(ff1);
            ff1.thispc.setFullScreenWindow(ff1.myWindow);
            ff1.repaint();
            String[] filearray = new String[2];
            filearray[0] = "/btc_gui/newpackage/btc-zg.jpg";
            filearray[1] = "/btc_gui/newpackage/pic2.jpeg";
            ff1.changeImage = new ChangeImageListener(ff1.jLabel1,filearray);
            ff1.timer = new Timer(5000,ff1.changeImage);
            ff1.timer.start();
        }

    });
}

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

}

看看 documentation of setFullScreenWindow:

Exclusive mode implies:

  • […] All other application windows will always appear beneath the full-screen window in the Z-order.

在您的代码中创建一个新的 Window 并使其成为全屏 window:

AdFrame ff1 = new AdFrame();
…
ff1.myWindow = new Window(ff1);
ff1.thispc.setFullScreenWindow(ff1.myWindow);

所以这个新的 Window 将在最上面,而您的 AdFrame 将在最上面,因此不可见,正如文档指定的那样。

如果你想让你的 AdFrame 实例成为全屏 window,你应该这样做而不是创建另一个 window:

AdFrame ff1 = new AdFrame();
ff1.setUndecorated(true);
ff1.setExtendedState(JFrame.MAXIMIZED_BOTH);
ff1.setVisible(true);
ff1.thispc = ff1.getGraphicsConfiguration().getDevice();
ff1.thispc.setFullScreenWindow(ff1);

请注意,我还按照文档的建议添加了一个 setUndecorated(true) 调用。