2013-03-12 104 views
1

請看看下面的代碼餐巾紙外觀和感覺錯誤

Form.java

import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 

public class Form extends JFrame 
{ 
    private JButton[]buttonHolder; 

    public Form() 
    { 
     //Intializing instance variables 
     buttonHolder = new JButton[9]; 

     this.add(createCenterPanel()); 
     this.setSize(300,300); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private JPanel createCenterPanel() 
    { 
     JPanel centerPanel = new JPanel(); 
     centerPanel.setLayout(new GridLayout(3,3,0,0)); 

     for(int i=0;i<9;i++) 
     { 
      buttonHolder[i] = new JButton(); 
      centerPanel.add(buttonHolder[i]); 
     } 

     return centerPanel; 
    } 
} 

Main.java

import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import napkin.NapkinLookAndFeel; 

public class Main 
{ 
    public static void main(String[]args) 
    { 
     try 
     { 
      Form f = new Form(); 
      UIManager.setLookAndFeel(new NapkinLookAndFeel()); 
     } 
     catch(Exception e) 
     { 

     } 
    } 
} 

我使用餐巾紙的外觀和感覺這裏,我得到錯誤keys we didn't overwrite: []。爲什麼是這樣?我沒有得到GUI。請幫忙。

+0

如果你掉這些語句的順序(即設置UIManager的外觀和感覺_before_實例化「Form」,而不是事後)。 – 2013-03-12 17:28:07

+0

@IanRoberts:適用於GUI顯示。但是這並沒有消除這個信息。無論如何,這不是一個例外 – 2013-03-12 17:49:41

回答

3

這可能有助於

一兩件事你可以嘗試如下:

  • 不要設置一下&感覺呢。
  • 創建您的用戶界面。
  • 在框架上調用setUndecorated(true)。
  • 設置看看&的感覺。
  • 爲框架調用SwingUtilities.updateComponentTreeUI。
  • 如有必要,請在框架上調用setUndecorated(false)。

http://www.coderanch.com/t/566070/GUI/java/Error-NapKin-Feel

編輯:

消息 「鍵,我們沒有覆蓋:[]」 在這裏印刷:

@Override 
protected void initClassDefaults(UIDefaults table) { 
    super.initClassDefaults(table); 
    String cName = NapkinLookAndFeel.class.getName(); 
    String basicPackageName = cName.replace("NapkinLookAndFeel", "Napkin"); 
    for (String uiType : UI_TYPES) { 
     String uiClass = basicPackageName + uiType; 
     table.put(uiType, uiClass); 
    } 

    Set<Object> keys = new HashSet<Object>(table.keySet()); 
    keys.removeAll(Arrays.asList(UI_TYPES)); 
    if (keys.size() != 0) { 
     System.out.println("keys we didn't overwrite: " + keys); 
    } 
} 

http://www.jarvana.com/jarvana/view/net/sf/squirrel-sql/thirdparty-non-maven/napkinlaf/1.2/napkinlaf-1.2-sources.jar!/net/sourceforge/napkinlaf/NapkinLookAndFeel.java?format=ok

+0

不,這並不帶走消息 – 2013-03-12 17:48:52

+0

@Yohan是你的應用程序拋出一些異常?嘗試在catch塊中添加'e.printStackTrace();'。 – Pshemo 2013-03-12 17:56:17

+0

@Pshemo:沒有例外。 GUI工作正常。只是想知道這個消息 – 2013-03-12 18:09:14

0

設置的外觀和感覺首先,您創建Form實例前:

public static void main(String[]args) 
{ 
    try 
    { 
     UIManager.setLookAndFeel(new NapkinLookAndFeel()); 
     Form f = new Form(); 
    } 
    catch(Exception e) 
    { 
     // it's generally a bad idea to silently swallow exceptions, 
     // you should at least do... 
     e.printStackTrace(); 
    } 
}