Contents - Cultural View
Contents - Cultural View Contents - Cultural View
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/
- Page 241 and 242: New I/O 237 References [1] "JSR 51:
- Page 243 and 244: Omniscient Debugger 239 Omniscient
- Page 245 and 246: OpenJDK 241 Inclusion in software d
- Page 247 and 248: OpenJDK 243 [12] "Open JDK is here!
- Page 249 and 250: PHP/Java Bridge 245 References [1]
- Page 251 and 252: Java package 247 External links jav
- Page 253 and 254: Parboiled (Java) 249 } } return seq
- Page 255 and 256: Plain Old Java Object 251 POJO gene
- Page 257 and 258: Pluggable look and feel 253 Pluggab
- Page 259 and 260: Primitive wrapper class 255 Primiti
- Page 261 and 262: Quark Framework 257 Quark Framework
- Page 263 and 264: Quark Framework 259 has proven to p
- Page 265 and 266: Quark Framework 261 //illustrates p
- Page 267 and 268: Quark Framework 263 As well as LECC
- Page 269 and 270: Quark Framework 265 `catch` (\ex ->
- Page 271 and 272: REPLAY (software) 267 Qualities REP
- Page 273 and 274: Real time Java 269 References [1] J
- Page 275 and 276: Restlet 271 Connectors The list of
- Page 277 and 278: SLAMD 273 SLAMD The SLAMD Distribut
- Page 279 and 280: SLAMD Server 275 SLAMD Server A SLA
- Page 281 and 282: Bruno Souza 277 Bruno Souza Born Na
- Page 283 and 284: Static import 279 References [1] Ja
- Page 285 and 286: strictfp 281 Behaviors and restrict
- Page 287 and 288: Sun Web Developer Pack 283 Sun Web
- Page 289 and 290: Swing (Java) 285 The Swing Architec
- Page 291: Swing (Java) 287 Relationship to AW
- Page 295 and 296: SwingLabs 291 • JDNC [7] : Contai
- Page 297 and 298: Synth Look and Feel 293 Supposing t
- Page 299 and 300: TeachScheme! 295 • a function def
- Page 301 and 302: TeachScheme! 297 TeachScheme! and B
- Page 303 and 304: Unified Expression Language 299 Imp
- Page 305 and 306: Java Virtual Machine 301 Bytecode v
- Page 307 and 308: Java Virtual Machine 303 Heap The J
- Page 309 and 310: Xerlin 305 • Steinbrenner, Eugen
- Page 311 and 312: XStream 307 } and add some of these
- Page 313 and 314: Article Sources and Contributors 30
- Page 315 and 316: Article Sources and Contributors 31
- Page 317 and 318: Image Sources, Licenses and Contrib
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