Swing Synth L&F

本文介绍了如何使用Java Swing的SynthLookAndFeel通过XML配置文件定制Swing组件的外观。通过创建并加载XML布局文件,我们可以轻松实现自定义的Swing皮肤。示例代码展示了如何加载XML并设置JFrame的外观,同时提供了自定义组件样式的详细解释,包括颜色、字体、内边距、图片和图标的应用。
摘要由CSDN通过智能技术生成
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值