2013-03-12 99 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(); 
    } 
}