📄 imptheme.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Implementing themes</title>
<meta name="AUTHOR" content="Manish Hatwalne">
</head>
<body>
<font color="#000066" size="7">Implementing themes</font>
<p>This page will briefly discuss how these themes are implemented for Swiing's
default metal Look and Feel. Implementing themes is comparatively a very simple
and frivolous task, keeping in mind the power that Swing gives to GUI developers
with its pluggable L&F.</p>
<p>This application implements themes in two ways. One, by using in-built classes
which offer predefined themes, and secondly by allowing the user to use his own
custom themes as a property files. We will look into both the case.</p>
<h2><font color="#000066">In-built themes</font></h2>
<p>These themes are implemented as clases, which in turn extend the class
<code>javax.swing.plaf.metal.DefaultMetalTheme</code>. The theme class
provides new default values for three primary colors and three secondary colors.
Additionally, it also provides fonts for Frame title, menu text and control text
in some cases in this code. As a possibility, theme class may specify many other things
such as default icons, borders and so on.</p>
<h2><font color="#000066">Custom themes</font></h2>
<p>In this case, the theme is provided through a text property file. The application
implements this through a class called <code>CustomTheme</code>, it also extends
<code>javax.swing.plaf.metal.DefaultMetalTheme</code>, which really is the heart of
all this themes stuff. The <code>CustomTheme</code> class however, extracts all the
necessary information from the input stream of the property file which has been provided
to it. Rest of the implementation is same as above.</p>
<h2><font color="#000066">Updating the GUI</font></h2>
<p>Once the theme is changed, it is important that the GUI reflects the change immediately.
This is achieved by the following code -</p>
<pre>
MetalLookAndFeel.setCurrentTheme(theme);
//show theme name as the title
this.setTitle(theme.getName());
try{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e)
{
System.out.println(e);
}
</pre>
<h2><font color="#000066">Java 1.4 enhacements</font></h2>
<p>Java 1.4 adds a bit more glamour to Swing, it gives extra decoration to all its frames and
dialogs. If you are developing your theme based Swing application for Java 1.4 platform you can
use this extra bit of code in your application to enhance its visual appeal for Java 1.4 platform.
This will change the minimize, maximize and close icons of the frame, and also it will show the theme
color in the title bar of your window.</p>
<pre>
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
</pre>
<br><hr>
<center><a href="theme.html"><img src="home.gif" width="64" height="64" alt="Home" border="0"><br><b>Home</b></a></center>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -