java jLabel.setVisible无效

时间:2016-04-28 06:40:20

标签: java

我想检测多个显示器,并显示相同数量的标签。但设置不起作用。 InitialMonitorsDetection - 我想在DetectMonitors中隐藏每个标签 - 显示一些标签,但标签始终可见。我是java的新手,很抱歉愚蠢的问题。谢谢你的帮助。 `

   public MainFrame() {
        initComponents();
            }

public void InititalMonitorsDetection() { Monitor1.setVisible(false); Monitor2.setVisible(false); Monitor3.setVisible(false); Monitor4.setVisible(false); Monitor5.setVisible(false); Monitor6.setVisible(false); Monitor7.setVisible(false); Monitor8.setVisible(false); Monitor9.setVisible(false); Monitor10.setVisible(false); } public void DetectMonitors() { JLabel[] ArMonitors; ArMonitors = new JLabel[11]; ArMonitors[1] = Monitor1; ArMonitors[2] = Monitor2; ArMonitors[3] = Monitor3; ArMonitors[4] = Monitor4; ArMonitors[5] = Monitor5; ArMonitors[6] = Monitor6; ArMonitors[7] = Monitor7; ArMonitors[8] = Monitor8; ArMonitors[9] = Monitor9; ArMonitors[10] = Monitor10; GraphicsEnvironment ge = GraphicsEnvironment. getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); System.out.println(gs.length); for (int i=1; i <= gs.length; i++){ ArMonitors[i].setVisible(true); } } 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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainFrame.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 MainFrame().setVisible(true); } }); MainFrame mainfr = new MainFrame(); mainfr.InititalMonitorsDetection(); mainfr.DetectMonitors();<code>`

1 个答案:

答案 0 :(得分:1)

所有UI操作都需要通过Event Dispatcher Thread(EDT)完成。您需要删除此块:

MainFrame mainfr = new MainFrame();
mainfr.InititalMonitorsDetection();
mainfr.DetectMonitors();

它在这里的地方(至少):

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
         MainFrame mainfr = new MainFrame();
         mainfr.InititalMonitorsDetection();
         mainfr.DetectMonitors();
    }
});

理想情况下,您可以从UI操作中分离逻辑。上面的代码段将运行EDT上的所有内容,这取决于您正在进行的操作的持续时间,可能会对您的应用程序的响应性产生负面影响。