Java Tutorial/Swing/Look and Feel — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:31, 31 мая 2010
Содержание
- 1 14. Changing the Look and Feel
- 2 14. Changing the Look and Feel to MotifLookAndFeel
- 3 14. Default look and feel can be set in a file called "swing.properties" located in the "<JAVAHOME>/lib" directory.
- 4 14. Demonstrating the new GTK look and feel
- 5 14. Get the system look and feel
- 6 14. Install Cross Platform Look And Feel
- 7 14. Late loading of icon image files: public Object makeIcon(Class baseClass, String imageFile).
- 8 14. Listing the names of the look-and-feel classes that are installed with the JDK
- 9 14. LookAndFeel Introduction
- 10 14. Start up a program from the command line with a new look and feel.
- 11 14. Windows look and feel in Java 6
14. Changing the Look and Feel
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ChangeLook {
public static void main(String args[]) {
final JFrame frame = new JFrame("Change Look");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
String lafClassName = null;
lafClassName = actionEvent.getActionCommand();
String finalLafClassName = lafClassName;
try {
UIManager.setLookAndFeel(finalLafClassName);
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception exception) {
JOptionPane.showMessageDialog(frame, "Can"t change look and feel", "Invalid PLAF",
JOptionPane.ERROR_MESSAGE);
}
}
};
UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
JComboBox comboBox = new JComboBox(new String[] { "a", "b" });
JPanel panel = new JPanel();
for (int i = 0, n = looks.length; i < n; i++) {
JButton button = new JButton(looks[i].getName());
button.setActionCommand(looks[i].getClassName());
button.addActionListener(actionListener);
panel.add(button);
}
frame.add(comboBox, BorderLayout.NORTH);
frame.add(panel, BorderLayout.SOUTH);
frame.setSize(350, 150);
frame.setVisible(true);
}
}
14. Changing the Look and Feel to MotifLookAndFeel
import javax.swing.JFrame;
import javax.swing.UIManager;
public class ChangeLookFeelToMotifLookAndFeel {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (Exception e) {
System.err.println("Look and feel not set.");
}
JFrame aWindow = new JFrame("This is the Window Title");
aWindow.setBounds(50, 100, 300,300);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aWindow.setVisible(true);
}
}
14. Default look and feel can be set in a file called "swing.properties" located in the "<JAVAHOME>/lib" directory.
# Specify the default look and feel
swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel
14. Demonstrating the new GTK look and feel
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
public class GTKLookAndFeelDemo {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
JLabel label = new JLabel("Label");
JTextField field = new JTextField("www.jexp.ru!");
JList list = new JList(new String[] { "A", "B", "C" });
JScrollPane listPane = new JScrollPane(list);
listPane.setPreferredSize(new Dimension(250, 100));
JScrollPane treePane = new JScrollPane(new JTree());
treePane.setPreferredSize(new Dimension(250, 100));
JButton button = new JButton("Click me");
JPanel cp = new JPanel();
cp.add(label);
cp.add(field);
cp.add(listPane);
cp.add(treePane);
cp.add(button);
JFrame frame = new JFrame();
frame.setTitle("Windows Look and Feel Demo");
frame.setPreferredSize(new Dimension(280, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(cp);
frame.pack();
frame.setVisible(true);
}
}
14. Get the system look and feel
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("System LAF Demo");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frame);
frame.setVisible(true);
}
}
14. Install Cross Platform Look And Feel
import javax.swing.JFrame;
import javax.swing.UIManager;
public class GetCrossPlatformLookAnd {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Look and feel not set.");
}
JFrame aWindow = new JFrame("This is the Window Title");
aWindow.setBounds(50, 100, 300, 300);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aWindow.setVisible(true);
}
}
14. Late loading of icon image files: public Object makeIcon(Class baseClass, String imageFile).
Object iconObject = LookAndFeel.makeIcon(this.getClass(), "World.gif");
UIManager.put("Tree.leafIcon", iconObject);
14. Listing the names of the look-and-feel classes that are installed with the JDK
import javax.swing.UIManager;
public class MainClass {
public static void main(String[] a) {
UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo look : looks) {
System.out.println(look.getClassName());
}
}
}
javax.swing.plaf.metal.MetalLookAndFeel com.sun.java.swing.plaf.motif.MotifLookAndFeel com.sun.java.swing.plaf.windows.WindowsLookAndFeel com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
14. LookAndFeel Introduction
The default JDK has provided a few subclass of LookAndFeel that you can use as the argument to setLookAndFeel.
- com.sun.java.swing.plaf.gtk.GTKLookAndFeel
- javax.swing.plaf.metal.MetalLookAndFeel
- com.sun.java.swing.plaf.windows.WindowsLookAndFeel
- com.sun.java.swing.plaf.motif.MotifLookAndFeel
For instance, the following code forces the application to use the Motif look and feel.
UIManager.setLookAndFeel ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
14. Start up a program from the command line with a new look and feel.
java -Dswing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel ChangeLook
14. Windows look and feel in Java 6
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
public class WindowsLookAndFeelDemo {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
JLabel label = new JLabel("Label");
JTextField field = new JTextField("www.jexp.ru!");
JList list = new JList(new String[] { "A", "B", "C" });
JScrollPane listPane = new JScrollPane(list);
listPane.setPreferredSize(new Dimension(250, 100));
JScrollPane treePane = new JScrollPane(new JTree());
treePane.setPreferredSize(new Dimension(250, 100));
JButton button = new JButton("Click me");
JPanel cp = new JPanel();
cp.add(label);
cp.add(field);
cp.add(listPane);
cp.add(treePane);
cp.add(button);
JFrame frame = new JFrame();
frame.setTitle("Windows Look and Feel Demo");
frame.setPreferredSize(new Dimension(280, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(cp);
frame.pack();
frame.setVisible(true);
}
}