LookAndFeel在Ubuntu中没有变化

时间:2010-04-16 20:31:49

标签: java swing ubuntu look-and-feel

任何人都知道为什么laf在以下代码中没有改变? (在Ubuntu中运行)

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class TEST extends JPanel {

    public TEST() {
    final LookAndFeelInfo[] lafArray = UIManager.getInstalledLookAndFeels();
    String[] names = new String[lafArray.length];
    for (int i = 0; i < names.length; i++) {
        names[i] = lafArray[i].getName();
    }

    final JComboBox cb = new JComboBox(names);
    cb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
        try {
            int index = cb.getSelectedIndex();
            LookAndFeelInfo lafInfo = lafArray[index];
            String lafClassName = lafInfo.getClassName();
            System.out.println(lafClassName);
            UIManager.setLookAndFeel(lafClassName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    });

    add(cb);
    }

    public static void main(String[] args) throws Exception {
    System.out.println("start");

    JDialog dialog = new JDialog(null, Dialog.ModalityType.APPLICATION_MODAL);
    dialog.setContentPane(new TEST());
    dialog.pack();
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
    dialog.dispose();

    System.out.println("end");
    }

}

2 个答案:

答案 0 :(得分:4)

致电UIManager.setLookAndFeel(lafClassName)后,请致电SwingUtilities.updateComponentTreeUI(cb.getParent)。在更完整的应用程序中,您必须在每个顶级Container上调用它。您还应该在Container上调用pack(),因为lafs之间的组件大小不同。

答案 1 :(得分:2)

渲染组件后,不应更改外观。必须在组件初始化之前完成。来自here

  

注意:如果您要设置L&amp; F,您应该将其作为应用程序的第一步。否则,无论你要求什么样的L&amp; F,你都有可能初始化Java L&amp; F.当静态字段引用Swing类时会无意中发生这种情况,这会导致加载L&amp; F.如果尚未指定L&amp; F,则加载JRE的默认L&amp; F.对于Sun的JRE,默认情况是Java L&amp; F,Apple的JRE是Apple L&amp; F,等等。