有一种方法可以创建自己的外观和感觉。
我不确定这有多好,但它有效。假设您将扩展“Classic Windows”L&F.您需要在leas 2类一个是Look&Feel本身,让我们称之为WindowsClassicLookAndFeelExt。您只需要重写方法 initClassDefaults。
package testSwing;
import javax.swing.UIDefaults;
import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
public class WindowsClassicLookAndFeelExt extends WindowsClassicLookAndFeel {
@Override protected void initClassDefaults(UIDefaults table){
super.initClassDefaults(table);
Object[] uiDefaults = { "LabelUI", WindowsLabelExtUI.class.getCanonicalName()};
table.putDefaults(uiDefaults);
}
}
您还需要一个 WindowsLabelExtUI 类来管理所有 JLabels 并设置属性:
package testSwing;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import com.sun.java.swing.plaf.windows.WindowsLabelUI;
public class WindowsLabelExtUI extends WindowsLabelUI{
static WindowsLabelExtUI singleton = new WindowsLabelExtUI();
public static ComponentUI createUI(JComponent c){
c.putClientProperty("html.disable", Boolean.TRUE);
return singleton;
}
}
最后,当您将主题设置为WindowsClassicLookAndFeelExt时,这是一个测试类
package testSwing;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class Main{
public static void main(String[] args){
try{ UIManager.setLookAndFeel(WindowsClassicLookAndFeelExt.class.getCanonicalName());
}catch (Exception e){
e.printStackTrace();
}
JFrame frame = new JFrame("JList Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] selections = {"<html><img src='http:\\\\invalid\\url'>", "<html><H1>Hello</h1></html>", "orange", "dark blue"};
JList list = new JList(selections);
list.setSelectedIndex(1);
System.out.println(list.getSelectedValue());
JLabel jLabel = new JLabel("<html><h2>standard Label</h2></html>");
frame.add(new JScrollPane(list));
frame.add(jLabel);
frame.pack();
frame.setVisible(true);
}
}
你会看到类似的东西