返回列表 发帖

[原创] Swing外观包使用

今天订的《Java Swing(第二版)》刚到,晚上就想试试Swing写的UI到底能达到什么程度。书上第二章范例代码如下。


// ToolbarFrame2.java
// The Swing-ified button example.  
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame2 extends Frame {
  // This time, let's use JButtons!
  JButton cutButton, copyButton, pasteButton;
  JButton javaButton, macButton, motifButton, winButton;
  public ToolbarFrame2() {
    super("Toolbar Example (Swing)");
    setSize(450, 250);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    ActionListener printListener = new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        System.out.println(ae.getActionCommand());
      }
    };
  
    // JPanel works similarly to Panel, so we'll use it
    JPanel toolbar = new JPanel();
    toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
    cutButton = new JButton("Cut");
    cutButton.addActionListener(printListener);
    toolbar.add(cutButton);
    copyButton = new JButton("Copy");
    copyButton.addActionListener(printListener);
    toolbar.add(copyButton);
    pasteButton = new JButton("Paste");
    pasteButton.addActionListener(printListener);
    toolbar.add(pasteButton);
    add(toolbar, BorderLayout.NORTH);  // The new BorderLayout add
    // Add the look-and-feel controls
    JPanel lnfPanel = new JPanel();
    LnFListener lnfListener = new LnFListener(this);
    macButton = new JButton("Mac");
    macButton.addActionListener(lnfListener);
    lnfPanel.add(macButton);
    javaButton = new JButton("Metal");
    javaButton.addActionListener(lnfListener);
    lnfPanel.add(javaButton);
    motifButton = new JButton("Motif");
    motifButton.addActionListener(lnfListener);
    lnfPanel.add(motifButton);
    winButton = new JButton("Windows");
    winButton.addActionListener(lnfListener);
    lnfPanel.add(winButton);
    add(lnfPanel, BorderLayout.SOUTH);
  }
  public static void main(String args[]) {
    ToolbarFrame2 tf2 = new ToolbarFrame2();
    tf2.setVisible(true);
  }
}


// LnFListener.java
// A listener that can change the look-and-feel of a frame based on
// the actionCommand of an ActionEvent object. Supported look-and-feels are:
//   Mac, Metal, Motif, Windows.
// Not all L&Fs will be available on a given machine. Notably, the Mac and
// Windows L&Fs work only on their specific platforms.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LnFListener implements ActionListener {
  Frame frame;
  public LnFListener(Frame f) {
    frame = f;
  }
  public void actionPerformed(ActionEvent e) {
    String lnfName = null;
    if (e.getActionCommand().equals("Mac")) {
      lnfName = "com.apple.mrj.swing.MacLookAndFeel";
    } else if (e.getActionCommand().equals("Metal")) {
      lnfName = "javax.swing.plaf.metal.MetalLookAndFeel";
    } else if (e.getActionCommand().equals("Motif")) {
      lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
    } else if (e.getActionCommand().equals("Windows")) {
      lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    } else {
      System.err.println("Unrecognized L&F request action: " +
        e.getActionCommand());
      return;
    }
    try {
      UIManager.setLookAndFeel(lnfName);
      SwingUtilities.updateComponentTreeUI(frame);
    }
    catch (UnsupportedLookAndFeelException ex1) {
      System.err.println("Unsupported LookAndFeel: " + lnfName);
    }
    catch (ClassNotFoundException ex2) {
      System.err.println("LookAndFeel class not found: " + lnfName);
    }
    catch (InstantiationException ex3) {
      System.err.println("Could not load LookAndFeel: " + lnfName);
    }
    catch (IllegalAccessException ex4) {
      System.err.println("Cannot use LookAndFeel: " + lnfName);
    }
  }
}
1

评分人数

  • chinanic

按照第一章所说内容需要swingall.jar以及其他外观包(motif.jar,windows.jar,wmulti.jar等)。但是这些外观包不容易下载到,而且我是在Oracle安装目录下(..oracle\ora92\olap\wrksht\lib)发现Swingall.jar的。把swingall.jar写到CLASSPATH环境变量里,只能显示windows和motif外观,Mac和Metal外观无法使用。
   一番搜索后找到一些公共的外观包,我下的是liquidlnf.rar。使用这个jar后,这两个Java程序所要达到的UI效果就显示出来了。把Swing.porperty文件拷贝到jre\lib下另外把liquidlnf.jar,拷贝到jre\lib\ext里(我另外把liquidlnf.jar的路径写到CLASSPATH里了)。但是搜索到的原文说要显示Mac外观用“UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");就可以了。我在上面代码的改动为,把原先的“lnfName = "com.apple.mrj.swing.MacLookAndFeel";”改成“lnfName = "com.birosoft.liquid.LiquidLookAndFeel";”。运行程序就显示出相应的外观了。
    以下分别是Mac、Metal、Motif、Windows外观。

[ 本帖最后由 starlight 于 2007-4-9 16:46 编辑 ]
附件: 您需要登录才可以下载或查看附件。没有帐号?注册

TOP

liquidlnf.rar下载。
附件: 您需要登录才可以下载或查看附件。没有帐号?注册

TOP

不错不错!!
天行健,君子以自强不息
地势坤,君子以厚德载物
   黑色海岸线欢迎您

TOP

swing 的确是给4人的视觉带来了巨大的冲击,对追求感官的程序员来说,无疑一个好事

TOP

都几靓啊

TOP

返回列表 回复 发帖