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

(singke) #1
ptg7068951

Using Components 173

The following statement sets Nimbus as the look and feel:


UIManager.setLookAndFeel(
“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
);


One last thing is required to make the frame visible: Call its setVisible()
method with trueas an argument:


setVisible(true);


This opens the frame at the defined width and height. You also can call it
with falseto stop displaying a frame.


Listing 13.1 contains the source code described in this section. In an empty
Java file named SalutonFrame, enter these statements.


LISTING 13.1 The Full Text of SalutonFrame.java
1: importjavax.swing.*;
2:
3: public classSalutonFrame extendsJFrame {
4: public SalutonFrame() {
5: super(“Salutonmondo!”);
6: setLookAndFeel();
7: setSize(350, 100);
8: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9: setVisible(true);
10: }
11:
12: private voidsetLookAndFeel() {
13: try {
14: UIManager.setLookAndFeel(
15: “com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
16: );
17: } catch(Exception exc) {
18: // ignore error
19: }
20: }
21:
22: public staticvoidmain(String[] arguments) {
23: SalutonFrame sal = new SalutonFrame();
24: }
25: }


Lines 22–24 of Listing 13.1 contain a main()method, which turns this
frame class into an application. When you run the class, you see the frame
shown in Figure 13.1.

Free download pdf