import
java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.text.DecimalFormat;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
/** */ /**
* Create at 2007-2-5 10:16:15<br>
* 平台工具类
*
* @author Brad.Wu
* @version 1.0
*/
public class PlafUtil ... {
/** *//**
* 切换L&F
*/
public void switchLookAndFeel() ...{
LookAndFeel lnf = UIManager.getLookAndFeel();
if (lnf.getClass() == MetalLookAndFeel.class) ...{
switchToMetal();
} else if (lnf.getClass() == WindowsLookAndFeel.class) ...{
switchToWindows();
}
}
/** *//**
* 切换到Windows风格
*/
protected void switchToWindows() ...{
UIManager.getDefaults().put(JChat.uiClassID, WindowsChatUI.class.getName());
initJChatDefaults();
}
/** *//**
* 切换到Metal风格
*/
protected void switchToMetal() ...{
UIManager.getDefaults().put(JChat.uiClassID, MetalChatUI.class.getName());
initJChatDefaults();
}
/** *//**
* 初始化JChat的UI定义
*/
protected void initJChatDefaults() ...{
UIManager.put("Chat.background", Color.WHITE);
UIManager.put("Chat.foreground", UIManager.get("Label.foreground"));
UIManager.put("Chat.font", UIManager.get("Label.font"));
UIManager.put("Chat.axisMargin", new Insets(2, 2, 2, 2));
UIManager.put("Chat.axisLineColor", Color.BLACK);
UIManager.put("Chat.scaleSize", 3);
UIManager.put("Chat.axisForeground", Color.BLACK);
UIManager.put("Chat.axisFont", UIManager.get("Label.font"));
UIManager.put("Chat.textLineGap", 3);
UIManager.put("Chat.axisTitleForeground", Color.BLACK);
UIManager.put("Chat.axisTitleFont", UIManager.get("Label.font"));
UIManager.put("Chat.maxValueQuotiety", 1.1d);
UIManager.put("Chat.minValueQuotiety", 0.8d);
UIManager.put("Chat.seriesGap", 10);
UIManager.put("Chat.mainRectBackground", new Color(198, 195, 198));
UIManager.put("Chat.cellValueFont", new Font(UIManager.getFont("Label.font").getName(), Font.PLAIN, 10));
UIManager.put("Chat.seriesSampleFont", UIManager.get("Label.font"));
UIManager.put("Chat.percentFormat", new DecimalFormat("00.#%"));
}
}
import java.awt.Font;
import java.awt.Insets;
import java.text.DecimalFormat;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
/** */ /**
* Create at 2007-2-5 10:16:15<br>
* 平台工具类
*
* @author Brad.Wu
* @version 1.0
*/
public class PlafUtil ... {
/** *//**
* 切换L&F
*/
public void switchLookAndFeel() ...{
LookAndFeel lnf = UIManager.getLookAndFeel();
if (lnf.getClass() == MetalLookAndFeel.class) ...{
switchToMetal();
} else if (lnf.getClass() == WindowsLookAndFeel.class) ...{
switchToWindows();
}
}
/** *//**
* 切换到Windows风格
*/
protected void switchToWindows() ...{
UIManager.getDefaults().put(JChat.uiClassID, WindowsChatUI.class.getName());
initJChatDefaults();
}
/** *//**
* 切换到Metal风格
*/
protected void switchToMetal() ...{
UIManager.getDefaults().put(JChat.uiClassID, MetalChatUI.class.getName());
initJChatDefaults();
}
/** *//**
* 初始化JChat的UI定义
*/
protected void initJChatDefaults() ...{
UIManager.put("Chat.background", Color.WHITE);
UIManager.put("Chat.foreground", UIManager.get("Label.foreground"));
UIManager.put("Chat.font", UIManager.get("Label.font"));
UIManager.put("Chat.axisMargin", new Insets(2, 2, 2, 2));
UIManager.put("Chat.axisLineColor", Color.BLACK);
UIManager.put("Chat.scaleSize", 3);
UIManager.put("Chat.axisForeground", Color.BLACK);
UIManager.put("Chat.axisFont", UIManager.get("Label.font"));
UIManager.put("Chat.textLineGap", 3);
UIManager.put("Chat.axisTitleForeground", Color.BLACK);
UIManager.put("Chat.axisTitleFont", UIManager.get("Label.font"));
UIManager.put("Chat.maxValueQuotiety", 1.1d);
UIManager.put("Chat.minValueQuotiety", 0.8d);
UIManager.put("Chat.seriesGap", 10);
UIManager.put("Chat.mainRectBackground", new Color(198, 195, 198));
UIManager.put("Chat.cellValueFont", new Font(UIManager.getFont("Label.font").getName(), Font.PLAIN, 10));
UIManager.put("Chat.seriesSampleFont", UIManager.get("Label.font"));
UIManager.put("Chat.percentFormat", new DecimalFormat("00.#%"));
}
}
假设我们只在Windows和Metal里面切换. 现在有一个自定义组件JChat, 很自然我们会声明一个ChatUI的超类如下:
import
javax.swing.plaf.ComponentUI;
/** */ /**
* Create at 2007-2-2 16:53:01<br>
* 图表UI
*
* @author Brad.Wu
* @version 1.0
*/
public abstract class ChatUI extends ComponentUI ... {
}
/** */ /**
* Create at 2007-2-2 16:53:01<br>
* 图表UI
*
* @author Brad.Wu
* @version 1.0
*/
public abstract class ChatUI extends ComponentUI ... {
}
然后有一个BasicChatUI继承于它(这里我省略了很多代码了):
import
java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
/** */ /**
* Create at 2007-2-2 17:31:09<br>
* 基本图表UI
*
* @author Brad.Wu
* @version 1.0
*/
public class BasicChatUI extends ChatUI ... {
/** *//**
* (non-Javadoc)
*
* @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
*/
public static ComponentUI createUI(JComponent c) ...{
return new BasicChatUI();
}
}
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
/** */ /**
* Create at 2007-2-2 17:31:09<br>
* 基本图表UI
*
* @author Brad.Wu
* @version 1.0
*/
public class BasicChatUI extends ChatUI ... {
/** *//**
* (non-Javadoc)
*
* @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
*/
public static ComponentUI createUI(JComponent c) ...{
return new BasicChatUI();
}
}
然后分别写一个WindowsChatUI和MetalChatUI继承于BasicChatUI, 这是和Java Swing的写法一摸一样了. 当然你也可以用自己的写法, 没有规定必须如此.
最后回到JChat, 其实需要重写的就是getUIClassID()和updateUI()两个方法就可以了.
@SuppressWarnings(
"
serial
"
)
public class JChat extends JComponent implements ChatModelListener ... {
public static final String uiClassID = "ChatUI";
/** *//**
* (non-Javadoc)
*
* @see javax.swing.JComponent#getUIClassID()
*/
public String getUIClassID() ...{
return uiClassID;
}
/** *//**
* (non-Javadoc)
*
* @see javax.swing.JComponent#updateUI()
*/
@Override
public void updateUI() ...{
setUI((ChatUI) UIManager.getUI(this));
}
}
public class JChat extends JComponent implements ChatModelListener ... {
public static final String uiClassID = "ChatUI";
/** *//**
* (non-Javadoc)
*
* @see javax.swing.JComponent#getUIClassID()
*/
public String getUIClassID() ...{
return uiClassID;
}
/** *//**
* (non-Javadoc)
*
* @see javax.swing.JComponent#updateUI()
*/
@Override
public void updateUI() ...{
setUI((ChatUI) UIManager.getUI(this));
}
}
最后回到帖子第一段代码, switchLookAndFeel()方法在你切换了LookAndFeel以后调用就可以达到切换UI的目的.
写完这个以后虽然达到了目的, 但是我觉得Swing应该有更方便快捷的方法可以达到这个目的, 希望有知道的达人给予提示或者案例.