Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Using Image Icons and Toolbars 229

LISTING 16.3 The Full Text of Tool.java
1: importjava.awt.;
2: importjava.awt.event.
;
3: importjavax.swing.*;
4:
5: public classTool extendsJFrame {
6: publicTool() {
7: super(“Tool”);
8: setLookAndFeel();
9: setSize(370, 200);
10: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11:
12: // build toolbar buttons
13: ImageIcon image1 = new ImageIcon(“newfile.gif”);
14: JButton button1 = new JButton(image1);
15: ImageIcon image2 = new ImageIcon(“openfile.gif”);
16: JButton button2 = new JButton(image2);
17: ImageIcon image3 = new ImageIcon(“savefile.gif”);
18: JButton button3 = new JButton(image3);
19:
20: // build toolbar
21: JToolBar bar = new JToolBar();
22: bar.add(button1);
23: bar.add(button2);
24: bar.add(button3);
25:
26: // build text area
27: JTextArea edit = new JTextArea(8, 40);
28: JScrollPane scroll = new JScrollPane(edit);
29:
30: // create frame
31: BorderLayout border = new BorderLayout();
32: setLayout(border);
33: add(“North”, bar);
34: add(“Center”, scroll);
35: setVisible(true);
36: }
37:
38: private voidsetLookAndFeel() {
39: try{
40: UIManager.setLookAndFeel(
41: “com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
42: );
43: } catch(Exception exc) {
44: // ignore error
45: }
46: }
47:
48: public static voidmain(String[] arguments) {
49: Tool frame = new Tool();
50: }
51: }

Free download pdf