Java Reference
In-Depth Information
these are known as the GUI's look-and-feel. Swing has a cross-platform look-and-feel
known as Nimbus . For GUI screen captures like Fig. 12.1, we've configured our systems
to use Nimbus as the default look-and-feel. There are three ways that you can use Nimbus:
1. Set it as the default for all Java applications that run on your computer.
2. Set it as the look-and-feel at the time that you launch an application by passing a
command-line argument to the java command.
3. Set it as the look-and-feel programatically in your application (see Section 22.6).
To set Nimbus as the default for all Java applications, you must create a text file
named swing.properties in the lib folder of both your JDK installation folder and your
JRE installation folder. Place the following line of code in the file:
swing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
In addition to the standalone JRE, there is a JRE nested in your JDK's installation folder.
If you're using an IDE that depends on the JDK, you may also need to place the
swing.properties file in the nested jre folder's lib folder.
If you prefer to select Nimbus on an application-by-application basis, place the fol-
lowing command-line argument after the java command and before the application's
name when you run the application:
-Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
12.3 Simple GUI-Based Input/Output with JOptionPane
The applications in Chapters 2-10 display text in the command window and obtain input
from the command window. Most applications you use on a daily basis use windows or
dialog boxes (also called dialogs ) to interact with the user. For example, an e-mail program
allows you to type and read messages in a window the program provides. Dialog boxes are
windows in which programs display important messages to the user or obtain information
from the user. Java's JOptionPane class (package javax.swing ) provides prebuilt dialog
boxes for both input and output. These are displayed by invoking static JOptionPane
methods. Figure 12.2 presents a simple addition application that uses two input dialogs
to obtain integers from the user and a message dialog to display the sum of the integers
the user enters.
1
// Fig. 12.2: Addition.java
2
// Addition program that uses JOptionPane for input and output.
3
import javax.swing.JOptionPane;
4
5
public class Addition
6
{
7
public static void main(String[] args)
8
{
9
// obtain user input from JOptionPane input dialogs
String firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );
10
11
Fig. 12.2 | Addition program that uses JOptionPane for input and output. (Part 1 of 2.)
 
 
Search WWH ::




Custom Search