13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

342Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetPro16.2A SIMPLE SWING PROGRAMLet’s take a look at a very simple (in its functi<strong>on</strong>, not in its compositi<strong>on</strong>)Swing program—a simple “Hello, world” that will appear in a window(Example 16.1).Example 16.1 A simple Swing applicati<strong>on</strong>import java.awt.*;import javax.swing.*;public classhw{public static voidmain(String[] args){//Create the top-level c<strong>on</strong>tainerJFrame frame = new JFrame();JLabel hi = new JLabel("Hello, world.");frame.getC<strong>on</strong>tentPane().add(hi, BorderLayout.CENTER);frame.setDefaultCloseOperati<strong>on</strong>(JFrame.EXIT_ON_CLOSE);frame.pack(); // kicks the UI into acti<strong>on</strong>frame.setVisible(true);} // main} // class hwNow compile this program and run it from the command line like this:$ javac hw.java$ java hwYou should then see a small window appear, looking as in Figure 16.1.Figure 16.1 “Hello, world” in a Swing window

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!