2012-03-02 11 views
8

tôi loại nhầm lẫn về nơi để đặt này:Làm thế nào để thiết lập JFrame xem và cảm nhận

try { 
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
} catch(Exception e){ 

} 

tôi không mở rộng các lớp JFrame nhưng sử dụng JFrame f = new JFrame(); Thanks: D

+0

Hãy chắc chắn rằng Look'n'Feel được cấu hình ** trước ** initialising khung. –

+0

[Lập trình thiết lập giao diện] (http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#programmatic) – chicout

Trả lời

6

nơi phổ biến nhất để đặt cái này, nằm ngay bên trong phương thức void chính (String [] args). Giống như vậy:

public static void main(String[] args) 
{ 
    try 
    { 
     UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
    } 
    catch(Exception e){ 
    } 
    new YourClass(); //start your application 
} 

để biết thêm nhìn vào trang web này: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

+2

về cơ bản chính xác, nhưng không được đề xuất cho Nimbus :) nó bắt đầu cuộc sống của nó trong com.sun. * trong jdk6 với sự chắc chắn được chuyển vào javax.swing trong jdk7. Vì vậy, thay vì mã hóa cứng tên lớp, truy vấn UIManager cho giao diện cài đặtAndFeels và lặp lại chúng cho đến khi một lớp có chứa "Nimbus" được tìm thấy – kleopatra

+0

Thành thật mà nói, tôi không bao giờ sử dụng bất kỳ Giao diện nào cho các chương trình java của mình. Nhưng nếu tôi muốn, tôi sẽ sử dụng đoạn mã của bạn !! cảm ơn nhiều –

9

Lưu ý: đây không phải là một câu trả lời cho câu hỏi (mà là nơi để thiết lập LAF). Thay vào đó, nó trả lời câu hỏi cách thực hiện đặt LAF theo cách độc lập với tên gói của nó. Đơn giản hóa cuộc sống trong trường hợp lớp học được di chuyển, như f.i. Nimbus từ com.sun * đến javax.swing.

Cách tiếp cận cơ bản là truy vấn UIManager cho các LAF đã được cài đặt của nó, lặp qua chúng cho đến khi tìm thấy một kết quả phù hợp và thiết lập. Here'r phương pháp như thực hiện trong SwingX:

/** 
* Returns the class name of the installed LookAndFeel with a name 
* containing the name snippet or null if none found. 
* 
* @param nameSnippet a snippet contained in the Laf's name 
* @return the class name if installed, or null 
*/ 
public static String getLookAndFeelClassName(String nameSnippet) { 
    LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels(); 
    for (LookAndFeelInfo info : plafs) { 
     if (info.getName().contains(nameSnippet)) { 
      return info.getClassName(); 
     } 
    } 
    return null; 
} 

sử dụng (ở đây mà không xử lý ngoại lệ)

String className = getLookAndFeelClassName("Nimbus"); 
UIManager.setLookAndFeel(className); 
8

UIManager.setLookAndFeel() sẽ không hoạt động trên các thành phần đã được tạo ra. Đây là một cách hay để thiết lập Giao diện cho mọi cửa sổ trong ứng dụng của bạn. Điều này sẽ đặt nó trên tất cả các cửa sổ đang mở trong chương trình của bạn. Bất kỳ cửa sổ mới nào được tạo sẽ sử dụng những gì được thiết lập bởi UIManager.

UIManager.setLookAndFeel(lookModel.getLookAndFeels().get(getLookAndFeel())); 
    for(Window window : JFrame.getWindows()) { 
     SwingUtilities.updateComponentTreeUI(window); 
    } 
2

Bạn có thể đặt khối này trong phương thức chính sau khi đã tạo JFrame hoặc trong hàm tạo của lớp mở rộng JFrame.

 

    try 
    { 
     //Set the required look and feel 
     UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
     //Update the component tree - associate the look and feel with the given frame. 
     SwingUtilities.updateComponentTreeUI(frame); 
    }//end try 
    catch(Exception ex) 
    { 
     ex.printStackTrace(); 
    }//end catch 
 
0
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 | InstantiationException || javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger( Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
Các vấn đề liên quan