swing ui主题_Java swing的主题风格设置

本文介绍了如何在Java Swing中设置不同的UI主题,包括Metal、Windows、Windows Classic、Motif、Mac、GTK、Nimbus、跨平台默认和系统默认风格,并提供了代码实现和效果展示,帮助开发者更好地自定义Swing应用的视觉样式。
摘要由CSDN通过智能技术生成

1风格种类

// 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);

2.代码实现

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 themeMap=new LinkedHashMap();

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

}

}

3.效果显示

推荐一个学习java swing的地址,总结的很好很全https://blog.csdn.net/xietansheng/article/details/72814531

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值