GNU Free Documentation License . .

Swing

: ,

Swing  Java. Swing Sun Microsystems. (. Swing widgets), , ,  . .

Swing JFC, . Java 2D, Accessibility-API, Drag & Drop-API AWT.

[]

Java 1.2 (1998 ) Swing Java Runtime Environment.

[]

Look and Feel

Swing , «look and feel» (L&F) . «Look» , «Feel»  . Suns JRE L&F[1]:

  • CrossPlatformLookAndFeel  L&F Java- ( Metal). , , , .
  • SystemLookAndFeel  L&F, , . L&F . Windows «Windows» L&F, ,   Windows, XP, Vista. Linux Solaris «GTK+», GTK+ 2.2 , «Motif».
  • Synth  L&F.
  • Multiplexing  L&F .

[] AWT

Swing , AWT. AWT, Swing - , AWT . AWT , (), , AWT ( ), AWT Swing- ( Swing ).

Swing (. plugable look-and-feel), ( , , ). , , Swing, . «» (. Lightweight) .   .

[] Lightweight

«Lightweight» , Swing , . «» AWT, Swing , , (, JFrame). Swing AWT ,   , AWT Swing , JPopupMenu JComboBox. , setLightWeightPopupEnabled(boolean), «» . true (setLightWeightPopupEnabled(true)), AWT .

[] SWT

[]

«Hello World» Swing:

 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.SwingUtilities; 
 
 public final class HelloWorld implements Runnable {
 
     public static void main(String[] args) {
 
         // Swing     (.. dispatching thread),
         //      (,    main())
         // .         ( main ),
         //     Swing-    . 
         //       ,     
         // (  ,    ).   Java 6,    ,
         //     .
         // 
         //   ,   ,   ,  :
 
         SwingUtilities.invokeLater (new HelloWorld());
     } 
 
     public void run() {
 
         //     "Hello, World!"
 
         JFrame f = new JFrame ("Hello, World!");
 
         //   :  listener  
         //    ,    windowClosing() 
         //      System.exit()
         //     ""      .
         //     ,    . 
         //          .
 
         f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
 
         //     :
         //            f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 
         //        .
 
         //f.getContentPane().add (new JLabel("Hello, World!")); -  
         f.add(new JLabel("Hello World"));
 
         // pack() ""    ,     
         //     .
 
         f.pack();
 
         //  
 
         f.setVisible(true);
     }
 }

Look-And-Feel:

try { 
    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    SwingUtilities.updateComponentTreeUI(this); 
}
catch (Exception e){
    System.out.println("   Metal-Look-And-Feel");
}

Look-And-Feel:

// ,   L&F      
// -  Swing,     
//Java L&F,   ,  L&F  .
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } 
    catch (Exception e) {
    //Exception handle
    }

[] .

[]

[]

  1. How to Set the Look and Feel (The Java Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)

[]