JLabel在全屏独占模式下消失

时间:2015-03-27 13:44:37

标签: java swing netbeans fullscreen screensaver

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

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

以下是代码:

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                   

}

1 个答案:

答案 0 :(得分:0)

查看documentation of setFullScreenWindow

  

独家模式意味着:

     
      
  • [...]所有其他应用程序窗口将始终显示在Z顺序的全屏窗口下方。
  •   
     

...

在您的代码中,您创建一个新的Window并将其设为全屏窗口:

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

因此,这个新的Window将位于最顶层,AdFrame将位于其下方,因此不可见,就像文档指定的那样。

如果你想让你的AdFrame实例成为全屏窗口,那么你应该做到这一点而不是创建另一个窗口:

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)次调用。