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