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

(singke) #1
ptg7068951

Creating Your Own Component 183

. Line 11—The timelabel is added to the clock panel by calling the
panel’s add()method with the label as an argument.
. Line 12—The currentlabel is added to the panel in the same
manner.


To try out this panel, create the ClockFrameapplication, which is defined
in Listing 13.4.


LISTING 13.4 The Full Text of ClockFrame.java
1: importjava.awt.;
2: importjavax.swing.
;
3:
4: public classClockFrame extendsJFrame {
5: publicClockFrame() {
6: super(“Clock”);
7: setLookAndFeel();
8: setSize(225, 125);
9: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10: FlowLayout flo = new FlowLayout();
11: setLayout(flo);
12: ClockPanel time = new ClockPanel();
13: add(time);
14: setVisible(true);
15: }
16:
17: private voidsetLookAndFeel() {
18: try {
19: UIManager.setLookAndFeel(
20: “com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
21: );
22: } catch(Exception exc) {
23: // ignore error
24: }
25: }
26:
27: public static voidmain(String[] arguments) {
28: ClockFrame clock = new ClockFrame();
29: }
30: }


When you run the application, it should resemble Figure 13.7.


FIGURE 13.7
Displaying a clock panel
component.
Free download pdf