天天看点

【Java swing】如何配置Java swing主题主题种类简单实现代码示例代码

【Java swing】如何配置Java swing主题

  • 主题种类
    • Metal风格 (默认)
    • Windows风格
    • Windows Classic风格
    • Motif风格
    • Mac风格 (需要在相关的操作系统上方可实现)
    • GTK风格 (需要在相关的操作系统上方可实现)
    • 可跨平台的默认风格
    • 当前系统的风格
  • 简单实现代码
  • 示例代码

主题种类

Metal风格 (默认)

String lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";
UIManager.setLookAndFee(lookAndFeel);
           

Windows风格

String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFee(lookAndFeel);  
           

Windows Classic风格

String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
UIManager.setLookAndFee(lookAndFeel);  
           

Motif风格

String lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
           

Mac风格 (需要在相关的操作系统上方可实现)

String lookAndFeel = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
           

GTK风格 (需要在相关的操作系统上方可实现)

String lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
           

可跨平台的默认风格

String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
           

当前系统的风格

String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
           

简单实现代码

import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager;
public class SwingTheme{
    public JPanel showJP;//主面板

    public static void main(String[] args){
        SwingTheme swingTheme=new SwingTheme();
        swingTheme.init();
    }
    public void init(){
        JFrame jframe=new JFrame();
        jframe.setSize(800,200);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setTitle("Swing主题测试");
        jframe.setLayout(new BorderLayout());
        String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        try {
            UIManager.setLookAndFeel(lookAndFeel);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        JPanel mainJP=new JPanel(new BorderLayout());
        jframe.add(mainJP,BorderLayout.CENTER);

        showJP=new JPanel(new GridLayout(0,1));
        JScrollPane jsp=new JScrollPane(showJP,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
        mainJP.add(jsp,BorderLayout.CENTER);
        JPanel showMainJP=new JPanel(new FlowLayout());
        showMainJP.setPreferredSize(new Dimension(800,150));
        showMainJP.setBorder(BorderFactory.createTitledBorder("Windows风格"));
        //加入所有的组件显示测试
        showMainJP.add(new JLabel("标签"));
        showMainJP.add(new JButton("按钮"));
        showMainJP.add(new JRadioButton("单选按钮"));
        showMainJP.add(new JCheckBox("复选框"));
        showMainJP.add(new JToggleButton("开关按钮"));
        showMainJP.add(new JTextField("文本框"));
        showMainJP.add(new JPasswordField("密码框"));
        showMainJP.add(new JTextArea("文本区域"));
        String item[]={"下拉列表框"};
        showMainJP.add(new JComboBox());
        item[0]="列表";
        showMainJP.add(new JList(item));
        showMainJP.add(new JProgressBar(10,100));
        showMainJP.add(new JSlider(10,100,50));
        showMainJP.add(new JTable(2,2));
        item[0]="树";
        showMainJP.add(new JTree(item));
        showJP.add(showMainJP);
        jframe.setVisible(true);
    }
}
           

示例代码

import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager;
import java.util.LinkedHashMap;
import java.util.Map;
public class SwingTheme{
	public JPanel showJP;//主面板
	
	//java Swing的8种类主题比较
	public static void main(String[] args){
		SwingTheme swingTheme=new SwingTheme();
		swingTheme.init();
	}
	public void init(){
		JFrame jframe=new JFrame();
		jframe.setSize(800,800);
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setTitle("Swing主题测试");
		jframe.setLayout(new BorderLayout()); 
		JPanel mainJP=new JPanel(new BorderLayout());
		jframe.add(mainJP,BorderLayout.CENTER);
		
		showJP=new JPanel(new GridLayout(0,1));
		JScrollPane jsp=new JScrollPane(showJP,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
			ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
		mainJP.add(jsp,BorderLayout.CENTER);
		//存储10中主题风格
		LinkedHashMap<String,String> themeMap=new LinkedHashMap<String,String>();
		themeMap.put("默认风格","");
		themeMap.put("Metal风格","javax.swing.plaf.metal.MetalLookAndFeel");
		themeMap.put(" Windows风格","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		themeMap.put("Windows Classic风格","com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		themeMap.put("Motif风格","com.sun.java.swing.plaf.motif.MotifLookAndFeel");
		//(需要在相关的操作系统上方可实现),win会报错
		themeMap.put("Mac风格","com.sun.java.swing.plaf.mac.MacLookAndFeel");
		//(需要在相关的操作系统上方可实现),win会报错
		themeMap.put("GTK风格","com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
		themeMap.put("nimbus风格","com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		themeMap.put("可跨平台的默认风格", UIManager.getCrossPlatformLookAndFeelClassName());
		themeMap.put("当前系统的风格",UIManager.getSystemLookAndFeelClassName());
		//遍历显示所有主题
		for(Map.Entry<String,String> entry:themeMap.entrySet()){
			JPanel returnShowJP=getSwingThemeJP(entry.getKey(),entry.getValue());
			showJP.add(returnShowJP);
		}			
 
		jframe.setVisible(true);
	}
	public static JPanel  getSwingThemeJP(String name,String type){
		JPanel showMainJP=new JPanel(new FlowLayout());
		showMainJP.setPreferredSize(new Dimension(800,150));
		showMainJP.setBorder(BorderFactory.createTitledBorder(name));
		if(!name.equals("默认风格")){
			try{
				UIManager.setLookAndFeel(type);
			}catch(Exception e){
				System.out.println(name+"主题出错");
				e.printStackTrace();
			}
		}
		//加入所有的组件显示测试
		showMainJP.add(new JLabel("标签"));
		showMainJP.add(new JButton("按钮"));
		showMainJP.add(new JRadioButton("单选按钮"));
		showMainJP.add(new JCheckBox("复选框"));
		showMainJP.add(new JToggleButton("开关按钮"));
		showMainJP.add(new JTextField("文本框"));
		showMainJP.add(new JPasswordField("密码框"));
		showMainJP.add(new JTextArea("文本区域"));
		String item[]={"下拉列表框"};
		showMainJP.add(new JComboBox());
		item[0]="列表";
		showMainJP.add(new JList(item));
		showMainJP.add(new JProgressBar(10,100));
		showMainJP.add(new JSlider(10,100,50));
		showMainJP.add(new JTable(2,2));
		item[0]="树";
		showMainJP.add(new JTree(item));
		return showMainJP;
	}	
}