gpt4 book ai didi

java jLabel.setVisible 不工作

转载 作者:行者123 更新时间:2023-12-01 09:59:39 24 4
gpt4 key购买 nike

我想检测监视器的数量,并显示相同数量的标签。但 setvisible 不起作用。 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>`

最佳答案

所有 UI 操作都需要通过事件调度程序线程 (EDT) 来完成。您需要删除此 block :

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 上运行所有内容,这可能会对应用程序的响应能力产生负面影响,具体取决于您正在进行的操作的持续时间。

关于java jLabel.setVisible 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36907117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com