簡體   English   中英

為什么我的GUI看起來不對勁?

[英]Why does my GUI never look right?

使用Java中的Mac上的Netbeans GUI構建器構建。

這就是Netbeans中GUI的樣子:

Netbeans GUI

當我點擊預覽時,它看起來並不太糟糕,但有一些小變化:

GUI預覽

最后,當我運行它時,它看起來像這樣: - 太可怕了

外觀和感覺

我認為這與Java的“外觀和感覺”有關。 我試過刪除它,GUI變得一團糟。 那么我的選擇是什么? 正如你可以看到Netbeans中的所有內容一樣,當我運行它時,一切都很糟糕。

有任何想法嗎? 花了一天時間搞亂了這一點,我至少可以說

從它的外觀來看,Netbeans中的預覽使用的是與Java運行應用程序時使用的外觀不同的外觀
但是,您可以嘗試手動設置所需的外觀 Oracle提供了一個關於如何獲取和設置外觀教程

在你編碼的某些地方,Netbeans會創建一個main方法(這將在Netbeans執行的類中,並且可能是你的主框架 - 我認為這將是SPPMainGUI

它看起來像這樣(注意它可能是“折疊”的,所以你只會看到第一個注釋和第二個的decsLook and feel setting code (optional) ))

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

通過單擊邊距中的小+展開代碼塊,並用UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());替換for-loop UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 如下

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

當afsantos發現問題時,如果你能接受他的答案是正確的(並且反而投票給我),我會贊美它,輕推,輕推,眨眼,眨眼,不再說了)

正如你所提到的,它與外觀和感覺有關,所以真的,如果找到L&F Netbeans默認使用的唯一問題。 谷歌提出了這個頁面 我引用,

在NetBeans 7中,默認外觀自動設置為Nimbus外觀。

來自Oracle:

Nimbus是Java SE 6 Update 10(6u10)版本中引入的優質跨平台外觀。

要啟用Nimbus外觀,只需在某處添加這些不言自明的線條:

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    // If Nimbus is not available, you can set the GUI to another look and feel.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM