[转]改变JAVA程序默认的外观

本文介绍如何使用Java Swing自定义应用程序的外观和感觉(LookAndFeel),包括直接设置不同平台风格及通过菜单选择的方式。提供了代码示例展示如何实现Windows、Motif等风格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://blog.youkuaiyun.com/vincent__hu/archive/2005/10/10/498887.aspx

JAVA程序默认的外观(LOOKANDFEEL)可以改变为JAVA外观、MOTIF外观、WINDOWS外观、MAC外观;
而JAVA外观又有五种风格分别是海蓝宝石风格、祖母绿风格、红宝石风格、木炭风格、
高对比风格。
外观改变具体方法如下:

1:可以直接在程序中调用下面三个中的一个:
import javax.swing.*;
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel") ;
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel") ;
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel") ;
2:或采用菜单来变换:
import javax.swing.*;
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("Look & Feel", true);
ButtonGroup buttonGroup = new ButtonGroup();
final UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < info.length; i++) {
JRadioButtonMenuItem item = new
JRadioButtonMenuItem(info[i].getName(), i == 0);
final String className = info[i].getClassName();
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try { UIManager.setLookAndFeel(className); }
catch (Exception e) { System.out.println(e); }
SwingUtilities.updateComponentTreeUI(TouchyFeely.this); }
});
buttonGroup.add(item);
file.add(item);
}
mb.add(file);
setJMenuBar(mb);
}




 

原文地址:http://blog.youkuaiyun.com/lydavid/archive/2005/08/15/454936.aspx

一直不擅长java的界面编程,看到eclipse才知道原来java也能作出如此完美的界面。

原来是LookAndFeel,hehe!

import javax.swing.JFrame;
import javax.swing.UIManager;
import com.sun.java.swing.plaf.gtk.GTKLookAndFeel;
public class TT  {

 public static void main(String[] args){
  try{
       UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  }catch(Exception e){
      System.out.println(e); 
  }


  JFrame t= new JFrame();
  t.setSize(100,200);
  t.show();

  try{
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  }catch(Exception e1){
      System.out.println(e1); 
  }
  
  JFrame j= new JFrame();
  j.setSize(100,200);
  j.show();
  
  try{
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.GTKLookAndFeel");
  }catch(Exception e1){
     System.out.println(e1); 
  }

  JFrame k= new JFrame();  
  k.setSize(100,200);
  k.show();
  } 
}

 

如果引用第三方的LookAndFeel只要替换UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.GTKLookAndFeel");中的参数

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值