Swing (Java): differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m correzioni ortografiche e spazi AWB
m souce
Riga 78:
Il seguente è un programma [[Hello world]] di esempio che usa Swing.
 
<source lang="java">
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.EventQueue;
 
public final class HelloWorld {
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.EventQueue;
 
public finalstatic classvoid HelloWorldmain(String[] args) {
// Swing has its own dispatch thread, which is
// distinct from the main JVM (launcher) thread.
// This means that even if the (launcher) thread exited
// (this) main method, the Swing GUI thread would
// still be running, waiting to respond to user input, etc.
// and if the user closes the window, then the program would
// continue to run (due to the live gui thread). As of Java 1.4, the
// GUI thread will automatically stop if all Components are hidden and
// disposed.
 
// Execute all GUI code on the event dispatch thread, even initialization:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
// Swing has its own dispatch thread, which is
public void run() {
// distinct from the main JVM (launcher) thread.
// Create frame with title "Hello, World!"
// This means that even if the (launcher) thread exited
JFrame f = new JFrame("Hello, World!");
// (this) main method, the Swing GUI thread would
// still be running, waiting to respond to user input, etc.
// and if the user closes the window, then the program would
// continue to run (due to the live gui thread). As of Java 1.4, the
// GUI thread will automatically stop if all Components are hidden and
// disposed.
//
// Execute all GUI code on the event dispatch thread, even initialization:
EventQueue.invokeLater(new Runnable() { public void run() {
 
// Previously we typically attached a window listener to our main JFrame
// Create frame with title "Hello, World!"
// and on windowClosing() event notification callback, we would
JFrame f = new JFrame("Hello, World!");
// explicitly call System.exit(stat) or some other such nastiness.
// The following is the new (clean) way of defining default close behavior
// for a JFrame. The following will merely dispose the JFrame but it will not
// stop the application unless *all* Windows are disposed.
 
f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
// Previously we typically attached a window listener to our main JFrame
// and on windowClosing() event notification callback, we would
// explicitly call System.exit(stat) or some other such nastiness.
// The following is the new (clean) way of defining default close behavior
// for a JFrame. The following will merely dispose the JFrame but it will not
// stop the application unless *all* Windows are disposed.
 
// this stops the app on window close
f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
// (it is commented out.)
// f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 
// JFrame, which is the 'window' component of Swing, has an interesting feature:
// this stops the app on window close
// it has a layered content architecture which (conceptually) allows for placement
// (it is commented out.)
// of a component of the JFrame in a designated layer. The layers can be conceptually
// f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// viewed as a series of transparencies placed one above another in a overhead projector
// Swing typically places user components (such as buttons, etc.) in what it calls its
// 'content pane'. This pane is layer that is below most of the other (transparent) layers.
// What are the layers for? Think of when you right-click on a control and it 'pops' a
// contextual menu 'over' the original control; or, when you press 'help' for a control or
// hover the mouse for a 'tool tip' and the information is 'placed over the control'. How is that
// done? By rendering the information in a layer 'above' the lower level 'content' pane.
// So:
// The standard way of adding a component to a 'multi-pane' container such as JFrame is by first
// establishing a reference to its 'content pane' (which is, by default just a JPanel with
// BorderLayout and adding the components the JFrame to the content pane and NOT directly
// to the JFrame. (The latest Swing releasehowever alters the semantics of the JFrame.add(..)
// to assume convenient addition to the content pane).
 
f.getContentPane().add(new JLabel("Hello, World!"));
// JFrame, which is the 'window' component of Swing, has an interesting feature:
// it has a layered content architecture which (conceptually) allows for placement
// of a component of the JFrame in a designated layer. The layers can be conceptually
// viewed as a series of transparencies placed one above another in a overhead projector
// Swing typically places user components (such as buttons, etc.) in what it calls its
// 'content pane'. This pane is layer that is below most of the other (transparent) layers.
// What are the layers for? Think of when you right-click on a control and it 'pops' a
// contextual menu 'over' the original control; or, when you press 'help' for a control or
// hover the mouse for a 'tool tip' and the information is 'placed over the control'. How is that
// done? By rendering the information in a layer 'above' the lower level 'content' pane.
// So:
// The standard way of adding a component to a 'multi-pane' container such as JFrame is by first
// establishing a reference to its 'content pane' (which is, by default just a JPanel with
// BorderLayout and adding the components the JFrame to the content pane and NOT directly
// to the JFrame. (The latest Swing releasehowever alters the semantics of the JFrame.add(..)
// to assume convenient addition to the content pane).
 
// Remember the relative layouts of Swing?
f.getContentPane().add(new JLabel("Hello, World!"));
// pack() tells the receiving container (here 'this' JFrame)
// for it to optimally pack its components according to its layout.
f.pack();
 
f.setVisible(true);
// Remember the relative layouts of Swing?
} });
// pack() tells the receiving container (here 'this' JFrame)
}
// for it to optimally pack its components according to its layout.
}
f.pack();
</source>
 
f.setVisible(true);
} });
}
}
 
== Voci correlate ==