请查看以下代码
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 :(得分: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);
}
}
答案 1 :(得分: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();
}
}