3

Starting a Swing program

  1. import javax.swing.*;
  2.  
  3. public class HelloSwing {
  4.  
  5.     private static void createAndShowGUI() {
  6.         JFrame.setDefaultLookAndFeelDecorated(true);
  7.  
  8.         JFrame frame = new JFrame("Swing");
  9.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.  
  11.         JLabel label = new JLabel("Let's swing!");
  12.         frame.getContentPane().add(label);
  13.  
  14.         frame.pack();
  15.         frame.setVisible(true);
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  20.             public void run() {
  21.                 createAndShowGUI();
  22.             }
  23.         });
  24.     }
  25. }
$ javac HelloSwing.java
$ java HelloSwing

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].