Synth 是Java Swing 中使用XML文件对Swing的外观进行定义的一种方式, 不必了解过于细节的UI的实现方式,只要学会编写Synth的XML格式文件, 就可以实现自己的Swing漂亮的皮肤.
public class MainFrame extends JFrame {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
createAndShowUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static void createAndShowUI() throws Exception {
SynthLookAndFeel lookAndFeel = new SynthLookAndFeel();
lookAndFeel.load(MainFrame.class.getResourceAsStream("/laf.xml"), MainFrame.class);
UIManager.setLookAndFeel(lookAndFeel);
MainFrame mainFrame = new MainFrame();
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainFrame.setSize(800, 900);
mainFrame.getContentPane().add(new ComponentSet());
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.setBackground(new Color(43, 43, 43));
mainFrame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
/**
* Created by GuoLiang on 2016/1/9.
*/
public class ComponentSet extends JPanel {
public ComponentSet() {
this.setLayout(new FlowLayout());
this.setBorder(BorderFactory.createEmptyBorder(30, /*top*/ 30, /*left*/ 10, /*bottom*/ 30) /*right*/);
JTextField jTextField = new JTextField("我是输入框 hello", 100);
jTextField.setSelectionColor(new Color(90, 117, 171));
this.add(jTextField);
JTextField jTextField1 = new JTextField("我是输入框 hello", 100);
jTextField1.setSelectionColor(new Color(90, 117, 171));
this.add(jTextField1);
}
}
/**
* Created by GuoLiang on 2016/1/9.
*/
public class GradientPainter extends SynthPainter {
public void paintTextFieldBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
// For simplicity this always recreates the GradientPaint. In a
// real app you should cache this to avoid garbage.
// Graphics2D g2 = (Graphics2D) g;
// g2.setColor(Color.decode("#cccccc"));
// g2.setStroke(new BasicStroke(1.5f));
// g2.drawRoundRect(x+1, y+1, w-2, h-2, 5, 5);
Graphics2D g2 = (Graphics2D) g;
g2.fillRect(x, y, w, h);
g2.setStroke(new BasicStroke(1f));
g2.setColor(new Color(69, 73, 74));
g2.fillRoundRect(x + 2, y + 2, w - 3, h - 3, 10, 10);
g2.setColor(new Color(100, 100, 100));
g2.drawRoundRect(x + 1, y + 1, w - 2, h - 2, 10, 10);
}
}
laf.xml文件:<!--<?xml version="1.0" encoding="UTF-8"?>-->
<!--<!DOCTYPE synth PUBLIC "-//oracle.com//DTD swign synth configuration 1.0//EN" "https://docs.oracle.com/javase/8/docs/api/javax/swing/plaf/synth/doc-files/synth.dtd">-->
<synth>
<!-- Style that all regions will use -->
<style id="backingStyle">
<!-- Make all the regions opaque-->
<opaque value="TRUE"/>
<font name="微软雅黑" size="12"/>
<state>
<!-- Provide default colors -->
<color value="#2B2B2B" type="BACKGROUND"/>
<color value="#ffffff" type="FOREGROUND"/>
</state>
</style>
<bind style="backingStyle" type="REGION" key=".*"/>
<style id="buttonStyle">
<!-- Shift the text one pixel when pressed -->
<property key="Button.textShiftOffset" type="integer" value="0"/>
<insets top="10" left="10" right="10" bottom="10"/>
<state>
<imagePainter method="buttonBackground" path="images/button.png" sourceInsets="10 10 10 10"/>
</state>
<state value="PRESSED">
<imagePainter method="buttonBackground" path="images/button2.png" sourceInsets="10 10 10 10"/>
</state>
</style>
<!-- Bind buttonStyle to all JButtons -->
<bind style="buttonStyle" type="REGION" key="button"/>
<style id="textfield">
<opaque value="TRUE" />
<object id="gradient" class="com.lang.swing.GradientPainter"/>
<state>
<painter method="textFieldBackground" idref="gradient"/>
</state>
<state value="SELECTED">
</state>
<insets top="4" left="4" right="4" bottom="4"/>
</style>
<bind style="textfield" type="REGION" key