【问题标题】:JLabel disappears in fullscreen exclusive modeJLabel 在全屏独占模式下消失
【发布时间】:2015-03-28 02:19:33
【问题描述】:

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

我正在使用 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                   

}

【问题讨论】:

  • 什么是ChangeImageListener? NVM.. 1) 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。

标签: java swing netbeans fullscreen screensaver


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    相关资源
    最近更新 更多