swingui主题_Javaswing的主题风格设置1风格种类
// Metal风格 (默认)
String lookAndFeel = "javax.al.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.if.MotifLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
// Mac风格 (需要在相关的操作系统上⽅可实现)
String lookAndFeel = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
// GTK风格 (需要在相关的操作系统上⽅可实现)
String lookAndFeel = "com.sun.java.k.GTKLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
// 可跨平台的默认风格
String lookAndFeel = CrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
/
/ 当前系统的风格
String lookAndFeel = SystemLookAndFeelClassName();
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.al.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.if.MotifLookAndFeel");
//(需要在相关的操作系统上⽅可实现),win会报错
themeMap.put("Mac风格","com.sun.java.swing.plaf.mac.MacLookAndFeel");
//(需要在相关的操作系统上⽅可实现),win会报错
themeMap.put("GTK风格","com.sun.java.k.GTKLookAndFeel");
themeMap.put("nimbus风格","com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
themeMap.put("可跨平台的默认风格", CrossPlatformLookAndFeelClassName()); themeMap.put("当前系统的风格",SystemLookAndFeelClassName());
//遍历显⽰所有主题
for(Map.Entry Set()){
JPanel returnShowJP=Key(),Value());
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.ateTitledBorder(name)); if(!name.equals("默认风格")){
try{
UIManager.setLookAndFeel(type);
}catch(Exception e){
System.out.println(name+"主题出错");
e.printStackTrace();
}
}
//加⼊所有的组件显⽰测试
showMainJP.add(new JLabel("标签"));
javaswing实现购买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.效果显⽰

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。