2015-09-07 11:44:36 -06:00
|
|
|
// ui/LookAndFeel.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
// Selecting different looks & feels.
|
|
|
|
// {Args: motif}
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.util.*;
|
2015-11-11 20:20:04 -08:00
|
|
|
import static onjava.SwingConsole.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
public class LookAndFeel extends JFrame {
|
|
|
|
private String[] choices =
|
|
|
|
"Eeny Meeny Minnie Mickey Moe Larry Curly"
|
|
|
|
.split(" ");
|
|
|
|
private Component[] samples = {
|
|
|
|
new JButton("JButton"),
|
|
|
|
new JTextField("JTextField"),
|
|
|
|
new JLabel("JLabel"),
|
|
|
|
new JCheckBox("JCheckBox"),
|
|
|
|
new JRadioButton("Radio"),
|
|
|
|
new JComboBox<>(choices),
|
|
|
|
new JList<>(choices),
|
|
|
|
};
|
|
|
|
private static
|
|
|
|
Map<String,String> optionMap = new HashMap<>();
|
|
|
|
private static String options = "";
|
|
|
|
public LookAndFeel() {
|
|
|
|
super("Look And Feel");
|
|
|
|
setLayout(new FlowLayout());
|
|
|
|
for(Component component : samples)
|
|
|
|
add(component);
|
|
|
|
}
|
|
|
|
public static void initOptions() {
|
|
|
|
if(optionMap.isEmpty()) {
|
|
|
|
UIManager.LookAndFeelInfo[] lafi =
|
|
|
|
UIManager.getInstalledLookAndFeels();
|
|
|
|
for(UIManager.LookAndFeelInfo lf : lafi) {
|
|
|
|
String classname = lf.getClassName();
|
|
|
|
String[] parts = classname.split("\\.");
|
|
|
|
String option = parts[parts.length - 2];
|
|
|
|
optionMap.put(option, classname);
|
|
|
|
}
|
|
|
|
for(String option : optionMap.keySet())
|
|
|
|
options += option + " | ";
|
|
|
|
options =
|
|
|
|
options.substring(0, options.length() - 3);
|
|
|
|
options = "cross | system | " + options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private static void usageError() {
|
|
|
|
System.out.println(
|
|
|
|
"Usage:LookAndFeel [ " + options + " ]");
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
initOptions();
|
|
|
|
if(args.length == 0) usageError();
|
|
|
|
try {
|
|
|
|
switch(args[0]) {
|
|
|
|
case "cross":
|
|
|
|
UIManager.setLookAndFeel(UIManager.
|
|
|
|
getCrossPlatformLookAndFeelClassName());
|
|
|
|
break;
|
|
|
|
case "system":
|
|
|
|
UIManager.setLookAndFeel(UIManager.
|
|
|
|
getSystemLookAndFeelClassName());
|
|
|
|
break;
|
|
|
|
case "motif":
|
|
|
|
UIManager.setLookAndFeel("com.sun.java."+
|
|
|
|
"swing.plaf.motif.MotifLookAndFeel");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(optionMap.containsKey(args[0]))
|
|
|
|
UIManager.setLookAndFeel(
|
|
|
|
optionMap.get(args[0]));
|
|
|
|
else
|
|
|
|
usageError();
|
|
|
|
}
|
|
|
|
} catch(ClassNotFoundException |
|
|
|
|
InstantiationException |
|
|
|
|
IllegalAccessException |
|
|
|
|
UnsupportedLookAndFeelException e) {
|
|
|
|
usageError();
|
|
|
|
}
|
|
|
|
// Note the look & feel must be set before
|
|
|
|
// any components are created.
|
|
|
|
run(new LookAndFeel(), 300, 300);
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|