NOVOTS KMS 词汇表 Glossary    联系我们 Contact Us
查询 Search  
   
按类别浏览 Browse by Category
NOVOTS KMS .: JAVA应用服务器 .: 动态地改变程序的Look and Feel

动态地改变程序的Look and Feel

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);      //对由this所指示的组件重新设置外观

 
注意:由于JFrame、JApplet等为重量级组件,因此它的外观只与操作系统平台有关系,在相同的操作系统平台下表现出相同的外观。


1、Metal风格 (默认)

  String lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";

  UIManager.setLookAndFeel(lookAndFeel);

  2、Windows风格

  String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

  UIManager.setLookAndFeel(lookAndFeel);

  3、Windows Classic风格

  String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";

  UIManager.setLookAndFeel(lookAndFeel);

  4、Motif风格

  String lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

  UIManager.setLookAndFeel(lookAndFeel);

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

  String lookAndFeel = "com.sun.java.swing.plaf.mac.MacLookAndFeel";

  UIManager.setLookAndFeel(lookAndFeel);

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

  String lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";

  UIManager.setLookAndFeel(lookAndFeel);

  7、可跨平台的默认风格

  String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();

  UIManager.setLookAndFeel(lookAndFeel);

  8、当前系统的风格

  String lookAndFeel = UIManager.getSystemLookAndFeelClassName();

  UIManager.setLookAndFeel(lookAndFeel);

  在Java中让用户能够动态地更改应用的外观,可以给用户更好地体验,具体的实现方式是:

  1,先使用UIManager.setLookAndFeel(String s)方法设定对应的外观

  2,再使用SwingUtilities.updateComponentTreeUI(Component c)方法立刻更新应用的外观

  这两个类均在javax.swing包中

 

样例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LookAndFeelDemo extends JFrame {
  JPanel contentPane = new JPanel();
  JPanel jPanel1 = new JPanel();
  JRadioButton jRadioButton1 = new JRadioButton();
  JRadioButton jRadioButton2 = new JRadioButton();
  JRadioButton jRadioButton3 = new JRadioButton();
  JRadioButton jRadioButton4 = new JRadioButton();
  JRadioButton jRadioButton5 = new JRadioButton();
  static LookAndFeelDemo frame;

  public LookAndFeelDemo() {
    try {jbInit();}catch(Exception e) {}
  }

  public static void main(String[] args) {
    //Windows风格的窗口
    try {
      UIManager.setLookAndFeel(
          "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception e) { }
    frame=new LookAndFeelDemo();
    frame.setVisible(true);
  }

  //窗口关闭时清空内存
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  private void jbInit() throws Exception {
    contentPane=(JPanel)this.getContentPane();
    //设置窗口的标题,大小与布局
    setTitle("窗口风格设置程序演示");
    setSize(new Dimension(400,300));
    contentPane.setLayout(null);

    //设置放置单选框容器的布局,位置,大小
    jPanel1.setLayout(new BoxLayout(jPanel1,BoxLayout.Y_AXIS));
    jPanel1.setBounds(new Rectangle(10, 10, 380, 280));

    //分别设置单选框的标题与ActionCommand属性(在动作事件中获取该变量)
    jRadioButton1.setText("Windows风格的窗口");
    jRadioButton1.setActionCommand(
           "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //Windows风格,只能在Windows系统下使用   
    jRadioButton2.setText("跨平台风格的窗口");
    jRadioButton2.setActionCommand(
           "UIManager.getCrossPlatformLookAndFeelClassName()");  // 跨平台风格,此时显示模式不要加引号
    jRadioButton3.setText("Java风格的窗口");
    jRadioButton3.setActionCommand(
           "javax.swing.plaf.metal.MetalLookAndFeel");  //java平台默认外观,金属质感,可用在所有平台
    jRadioButton4.setText("CDE/Motif 风格的窗口");
    jRadioButton4.setActionCommand(
           "com.sun.java.swing.plaf.motif.MotifLookAndFeel");  //Motif风格,仿UNIX外观,可用在所有平台
    jRadioButton5.setText("苹果机风格的窗口");
    jRadioButton5.setActionCommand(
           "javax.swing.plaf.mac.MacLookAndFeel");  //Macintosh风格,只能在Macintosh操作系统下使用

    //创建单选框接收器
    RadioListener radioListener=new RadioListener();

    //分别为各个单选框加入动作接收器
    jRadioButton1.addActionListener(radioListener);
    jRadioButton2.addActionListener(radioListener);
    jRadioButton3.addActionListener(radioListener);
    jRadioButton4.addActionListener(radioListener);
    jRadioButton5.addActionListener(radioListener);

    //将单选框放入同一个容器内
    ButtonGroup group = new ButtonGroup();
    group.add(jRadioButton1);
    group.add(jRadioButton2);
    group.add(jRadioButton3);
    group.add(jRadioButton4);
    group.add(jRadioButton5);

    //向容器加入各种控件
    jPanel1.add(jRadioButton1, null);
    jPanel1.add(jRadioButton2, null);
    jPanel1.add(jRadioButton3, null);
    jPanel1.add(jRadioButton4, null);
    jPanel1.add(jRadioButton5, null);
    contentPane.add(jPanel1, null);
  }

  //单选框的动作事件处理类
  class RadioListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        String lnfName=e.getActionCommand();
        //重新设置平台的显示模式
        try{UIManager.setLookAndFeel(lnfName);}
        catch(Exception el){}
        //更新窗口的风格
        SwingUtilities.updateComponentTreeUI(frame);
      }
  }
}


这篇文章对你多有用?

用户评语

添加评语
当前还没有评语.


.: .: .: .: .:
[ 登陆 ]
北京护航科技有限公司 2006

Novots Technologies Limited