Java的桌面系统可以利用javax.swing.UIManager.setLookAndFeel来设置界面风格。这个主要介绍Flamingo-Ribbon中几种风格优美的样式。
关于Flamingo-Ribbon的介绍和安装参见: Flamingo-Ribbon
下面主要介绍如何Ribbon中界面风格的设置。
1 设置界面风格
import java.awt.Dimension;
import java.util.Arrays;
import java.util.List;
import javax.swing.SwingUtilities;
import org.pushingpixels.flamingo.api.common.JCommandButton;
import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
import org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority;
import org.pushingpixels.flamingo.api.ribbon.RibbonTask;
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
import org.pushingpixels.flamingo.api.ribbon.resize.IconRibbonBandResizePolicy;
public class MainFrame extends JRibbonFrame {
static { //*****
//获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
//根据图片的地址获取该图片,返回ResizableIcon
public static ResizableIcon getResizableIconFromResource(String resource) {
return ImageWrapperResizableIcon.getIcon(
MainFrame.class.getClassLoader().getResource(resource),
new Dimension(48, 48));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void run() {
MainFrame frame = new MainFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
JRibbonBand band1 = new JRibbonBand("Hello", null); //新建一个Band
band1.setResizePolicies((List) Arrays.asList(
new CoreRibbonResizePolicies.None(band1.getControlPanel()),
new IconRibbonBandResizePolicy(band1.getControlPanel())));
JCommandButton button1 =
new JCommandButton("Square",
getResizableIconFromResource("resources/Project.png"));
JCommandButton button2 =
new JCommandButton("Circle",
getResizableIconFromResource("resources/Clear.png"));
JCommandButton button3 =
new JCommandButton("Triangle",
getResizableIconFromResource("resources/ZoomOut.png"));
JCommandButton button4 =
new JCommandButton("Star",
getResizableIconFromResource("resources/ZoomIn.png"));
band1.addCommandButton(button1, RibbonElementPriority.TOP);
band1.addCommandButton(button2, RibbonElementPriority.MEDIUM);
band1.addCommandButton(button3, RibbonElementPriority.MEDIUM);
band1.addCommandButton(button4, RibbonElementPriority.MEDIUM);
//新建一个Task,并将Band添加到该Task中去
RibbonTask task1 = new RibbonTask("One", band1);
//先获取Ribbon,然后在Ribbon上添加一个Task
frame.getRibbon().addTask(task1);
}
});
}
}
运行上述代码,其结果如下:
上述界面使用的是操作系统的样式风格,设置样式如下:
static {因此要改变系统样式时也是通过修改里面的代码进行修改的。
//设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
2 修改界面风格
2.1 SubstanceOfficeBlue2007LookAndFeel
代码如下:
static {其界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
更加完美一点:
由以上界面风格中可以看到在界面的周围还是可以看到一圈操作系统界面的样式,可以通过在代码的第一行添加如下两句话将外围的操作系统样式去掉:
JFrame.setDefaultLookAndFeelDecorated(true); //windows功能失效(注意:这两行代码必须放在程序的开头)
JDialog.setDefaultLookAndFeelDecorated(true); //Dialog功能失效
添加完上述两行代码后重新运行上述代码可以发现其界面如下:
这样就完全摆脱了操作系统的样式。
2.2 SubstanceOfficeSilver2007LookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
2.3 SubstanceOfficeBlack2007LookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceOfficeBlack2007LookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
2.4 SubstanceTwilightLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceTwilightLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
2.5 SubstanceSaharaLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceSaharaLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
2.6 SubstanceMagellanLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceMagellanLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
2.7 SubstanceAutumnLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceAutumnLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
2.8 SubstanceBusinessBlackSteelLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
2.9 SubstanceEmeraldDuskLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceEmeraldDuskLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
3.0 SubstanceMarinerLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceMarinerLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
3.1 SubstanceMistAquaLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceMistAquaLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
3.2 SubstanceRavenLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceRavenLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
3.3 SubstanceDustLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceDustLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
3.4 SubstanceChallengerDeepLookAndFeel
代码如下:
static {界面风格如下:
// 设置界面风格:获取系统样式
try {
javax.swing.UIManager.setLookAndFeel(
"org.pushingpixels.substance.api.skin.SubstanceChallengerDeepLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
未完待续。