天天看點

【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;
	}	
}