Contents - Cultural View

Contents - Cultural View Contents - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

Swing (Java) 288 Examples A basic example The following is a rather simple Swing-based program. It displays a window (a JFrame) containing a label and a button. Notice how all instantiation and handling of all Swing components are done on the Event Dispatch Thread by use of the method EventQueue.invokeLater(Runnable)) and an anonymous Runnable class (see Swing and thread safety). // Import the swing and AWT classes needed import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; /** * Basic Swing example. */ public class SwingExample { public static void main(String[] args) { on the // Make sure all Swing/AWT instantiations and accesses are done // Event Dispatch Thread (EDT) EventQueue.invokeLater(new Runnable() { @Override "decorations", i.e. contained else the public void run() { // Create a JFrame, which is a Window with // title, border and close-button JFrame f = new JFrame("Swing Example Window"); // Set a simple Layout Manager that arranges the // Components f.setLayout(new FlowLayout()); // Add some Components f.add(new JLabel("Hello, world!")); f.add(new JButton("Press me!")); // "Pack" the window, making it "just big enough". f.pack(); // Set the default close operation for the window, or

Swing (Java) 289 } window } See also }); } • Abstract Window Toolkit • Layout manager // program won't exit when clicking close button // (The default is HIDE_ON_CLOSE, which just makes the // invisible, and thus doesn't exit the app) f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Set the visibility as true, thereby displaying it f.setVisible(true); • SwingLabs - Extensions to Swing that might be included in Swing in the future • Standard Widget Toolkit A third party competing widget toolkit resembling the Abstract Window Toolkit in function. References • Matthew Robinson, Pavel Vorobiev: Swing, Second Edition, Manning, ISBN 1-930110-88-X • David M. Geary: Graphic Java 2, Volume 2: Swing, Prentice Hall, ISBN 0-13-079667-0 • John Zukowski: The Definitive Guide to Java Swing, Third Edition, Apress, ISBN 1-590-59447-9 • James Elliott, Robert Eckstein, Marc Loy, David Wood, Brian Cole: Java Swing, O'Reilly, ISBN 0-596-00408-7 • Kathy Walrath, Mary Campione, Alison Huml, Sharon Zakhour: The JFC Swing Tutorial: A Guide to Constructing GUIs, Addison-Wesley Professional, ISBN 0-201-91467-0 • Joshua Marinacci, Chris Adamson: Swing Hacks, O'Reilly, ISBN 0-596-00907-0 External links • The Swing API documentation [7] • The Swing architecture [4] References [1] Swing as MVC (http:/ / java. sun. com/ products/ jfc/ tsc/ articles/ architecture/ ) [2] Swing vs. SWT Performance - Have a Look at the Call Stacks (http:/ / www. javalobby. org/ java/ forums/ t65168. html) [3] Igor, Križnar (2005-05-10). "SWT Vs. Swing Performance Comparison" (http:/ / cosylib. cosylab. com/ pub/ CSS/ DOC-SWT_Vs. _Swing_Performance_Comparison. pdf). cosylab.com. . Retrieved 2008-05-24. "It is hard to give a rule-of-thumb where SWT would outperform Swing, or vice versa. In some environments (e.g., Windows), SWT is a winner. In others (Linux, VMware hosting Windows), Swing and its redraw optimization outperform SWT significantly. Differences in performance are significant: factors of 2 and more are common, in either direction." [4] http:/ / java. sun. com/ products/ jfc/ tsc/ articles/ architecture/

Swing (Java) 288<br />

Examples<br />

A basic example<br />

The following is a rather simple Swing-based program. It displays a window (a JFrame) containing a label and a<br />

button.<br />

Notice how all instantiation and handling of all Swing components are done on the Event Dispatch Thread by use of<br />

the method EventQueue.invokeLater(Runnable)) and an anonymous Runnable class (see Swing and thread safety).<br />

// Import the swing and AWT classes needed<br />

import java.awt.EventQueue;<br />

import java.awt.FlowLayout;<br />

import javax.swing.JButton;<br />

import javax.swing.JFrame;<br />

import javax.swing.JLabel;<br />

/**<br />

* Basic Swing example.<br />

*/<br />

public class SwingExample {<br />

public static void main(String[] args) {<br />

on the<br />

// Make sure all Swing/AWT instantiations and accesses are done<br />

// Event Dispatch Thread (EDT)<br />

EventQueue.invokeLater(new Runnable() {<br />

@Override<br />

"decorations", i.e.<br />

contained<br />

else the<br />

public void run() {<br />

// Create a JFrame, which is a Window with<br />

// title, border and close-button<br />

JFrame f = new JFrame("Swing Example Window");<br />

// Set a simple Layout Manager that arranges the<br />

// Components<br />

f.setLayout(new FlowLayout());<br />

// Add some Components<br />

f.add(new JLabel("Hello, world!"));<br />

f.add(new JButton("Press me!"));<br />

// "Pack" the window, making it "just big enough".<br />

f.pack();<br />

// Set the default close operation for the window, or

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

Saved successfully!

Ooh no, something went wrong!