⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jan04_johnz.txt

📁 TechTips.zip
💻 TXT
📖 第 1 页 / 共 3 页
字号:
Core Java Technologies Tech Tips


                      Tips, Techniques, and Sample Code


Welcome to the Core Java Technologies Tech Tips for January 22, 
2004. Here you'll get tips on using core Java technologies and 
APIs, such as those in Java 2 Platform, Standard Edition (J2SE).

This issue covers:

         * Beyond the Basics of JOptionPane
         * Monitoring Class Loading and Garbage Collection
                  
These tips were developed using Java 2 SDK, Standard Edition, 
v 1.4.2.

This issue of the Core Java Technologies Tech Tips is written by 
John Zukowski, president of JZ Ventures, Inc. 
(http://www.jzventures.com).

You can view this issue of the Tech Tips on the Web at
http://java.sun.com/developer/JDCTechTips/2004/tt0122.html

See the Subscribe/Unsubscribe note at the end of this newsletter
to subscribe to Tech Tips that focus on technologies and products 
in other Java platforms.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

BEYOND THE BASICS OF JOPTIONPANE

The Swing component set in J2SE includes a special class for 
creating a panel to be placed in a popup window. You can use 
this panel to display a message to the user and to get a response 
from the user. The panel presents content in four areas, one each 
for an icon, message, input, and buttons. You don't need to use 
any of the areas, although you would typically present at least 
a message and a button.

The icon area is for the display of a javax.swing.Icon. The icon
is meant to indicate the type of message being displayed. There 
are default icons provided by the specific look and feel you use. 
The default images for these icons are specified through a 
property setting to the look and feel. The property setting 
points to the appropriate resource for each message type: 

o OptionPane.informationIcon
o OptionPane.warningIcon
o OptionPane.errorIcon
o OptionPane.questionIcon

You don't have to change the default properties, though you 
could with a call to UIManager.put(poperty name, new resource). 
With the defaults in place, you simply identify the appropriate 
message type with a constant of JOptionPane:

o JOptionPane.PLAIN_MESSAGE
o JOptionPane.INFORMATION_MESSAGE
o JOptionPane.WARNING_MESSAGE
o JOptionPane.ERROR_MESSAGE
o JOptionPane.QUESTION_MESSAGE

The plain variety maps to no icon, while the others use a default
icon from the look and feel.

The second area of the JOptionPane is the message area. This is 
typically used to display a text message. However, you could show 
any number of objects here. The message type determines how a
message is displayed.

The input area is next. Here is where you can get a response to 
a message from the user. To prompt for a response you can use a
free form text field, a combo box, or even a list control. For 
Yes/No type prompts, you would instead use the button area, 
described next.

Last is the button area, another response-like area. When a user
selects a button it signals the end of usage for the JOptionPane. 
Default sets of button labels are available. (As is the case for 
icons, these too come from property settings, such as 
OptionPane.yesButtonText.) You can also provide your own button 
labels. You can display any number of buttons (including no 
buttons) with any set of labels. The predefined button label is 
localized for several languages: English (en), German (de), 
Spanish (es), French (fr), Italian (it), Japanese (ja), 
Korean (ko), Swedish (sv), and two varieties of 
Chinese (zh_CN / zh_TW). If you provide your own labels, you need 
to localize them yourself. 

The predefined sets of buttons are defined by a set of 
JOptionPane constants:

o DEFAULT_OPTION
o YES_NO_OPTION
o YES_NO_CANCEL_OPTION
o OK_CANCEL_OPTION

Default maps to a single OK button.

Using JOptionPane

The JOptionPane class contains seven constructors, each returns
a component that you can place anywhere. However, more typically 
you would use a factory method that creates the component and 
automatically places it inside a JDialog or JInternalFrame. There 
are eight of these factory methods (two varieties of four methods) 
that JOptionPane provides:

o show[Internal]MessageDialog
o show[Internal]ConfirmDialog
o show[Internal]InputDialog
o show[Internal]OptionDialog

The Internal variety creates a JOptionPane and shows it in a 
JInternalFrame. The other variety creates a JOptionPane and shows 
it in a JDialog. This tip only discusses the dialog variety.

The message dialog is meant to show a message that the user 
confirms by selecting the button (or closing the dialog). The 
message dialog is not meant to return a value, 

In its simplest case, you would use code like the following to
show a message dialog:

   JOptionPane.showMessageDialog(
      component, "Hello, World");

The system centers the dialog over the component argument.

There are two other variations for showing a message dialog. 
These allow you to customize the window title, customize the 
message type (to use the default icon), and set a customized icon:

o showMessageDialog(Component parentComponent, 
                     Object message,
                     String title,
                     int messageType) 
o showMessageDialog(Component parentComponent,
                     Object message,                      
                     String title,
                     int messageType, 
                     Icon icon) 

The method for showing a confirm dialog has four variations:

o showConfirmDialog(Component parentComponent, 
                     Object message) 
o showConfirmDialog(Component parentComponent, 
                     Object message,
                     String title,
                     int optionType) 
o showConfirmDialog(Component parentComponent, 
                     Object message,
                     String title,
                     int optionType,
                     int messageType) 
o showConfirmDialog(Component parentComponent, 
                     Object message, 
                     String title,
                     int optionType,
                     int messageType, 
                     Icon icon)  

The simplest variation brings up a dialog with a question icon.
The dialog displays Select an Option as the title, has Yes, No, 
and Cancel as button labels.

   JOptionPane.showConfirmDialog(component, "Lunch?");

If you don't like the defaults, you can change them by using one 
of the other methods. You'll find that everything is changeable 
with JOptionPane.

Unlike the message dialog, the confirm dialog does require a 
return value. Here, you really do want to know which button the 
user selected. The confirm dialog returns an integer, indicated 
by one of the following constants of the JOptionPane class:

o CANCEL_OPTION
o CLOSED_OPTION (used when the used closed popup window)
o NO_OPTION
o OK_OPTION
o YES_OPTION

Passing in a setting of OK_CANCEL_OPTION for the option type 
changes the buttons shown in the confirm dialog to OK and Cancel.
Comparing the value returned to the constant, indicates which 
button the user selected.

Checking for the return value changes the one line above to quite 
a few more:

   int returnValue = JOptionPane.showConfirmDialog(
     component, "Lunch?");
   String message = null;
   if (returnValue == JOptionPane.YES_OPTION) {
     message = "Yes";
   } else if (returnValue == JOptionPane.NO_OPTION) {
     message = "No";
   } else if (returnValue == JOptionPane.CANCEL_OPTION) {
     message = "Cancel";
   } else if (returnValue == JOptionPane.CLOSED_OPTION) {
     message = "Closed";
   }
   System.out.println("User selected: " + message);

The input dialog method has six variations. Five return a String:

o showInputDialog(Object message) 
o showInputDialog(Object message, 
                  Object initialSelectionValue)
o showInputDialog(Component parentComponent, 
                  Object message) 
o showInputDialog(Component parentComponent, 
                  Object message, 
                  Object initialSelectionValue)
o showInputDialog(Component parentComponent, 
                  Object message, 
                  String title, 
                  int messageType) 

One of the variations returns an Object:

o showInputDialog(Component parentComponent, 
                  Object message, 
                  String title, 
                  int messageType, 
                  Icon icon, 
                  Object[] selectionValues, 
                  Object initialSelectionValue) 

The simplest variation shows the message in a question dialog. 
The dialog displays Input as the frame title, and has OK and 
Cancel buttons. Because there is no component provided, the frame
is centered over the whole screen.

   String value = JOptionPane.showInputDialog("Name");

As is the case for other types, when you show the input dialog, 
you can set the parent component, frame title, message type, or 
icon. However the buttons are fixed at OK and Cancel. You can 
also set the initial value in the text field.

The last method variation for the input dialog is special. 
Instead of offering the user a text field to enter selections, 
you provide an array of objects (typically strings) from which to 
choose. Depending on how many values you provide, the look and 
feel will present either a JComboBox or JList for the user 
selections. Here's an example that uses this variation. Here
an input dialog prompts a user to select a day of the week. The 
default is the last day of the week. The example uses a smallList 
for the days of the week.
 
   String smallList[] = {
     "Sunday",
     "Monday",
     "Tuesday",
     "Wednesday",
     "Thursday",
     "Friday",
     "Saturday"
   };
   String value = 
     (String)JOptionPane.showInputDialog(
       component, 
       "Which Day?", 
       "Day", 
       JOptionPane.QUESTION_MESSAGE, 
       null, // Use default icon
       smallList, 
       smallList[smallList.length-1]);
   System.out.println("Day: " + value);

For a larger list, the code looks essentially the same. Here the 
list of system properties are used as the choices.

   Object largeList[] = 
     System.getProperties().keySet().toArray();
   String value = 
     (String)JOptionPane.showInputDialog(
       component, 
       "Which Property?", 
       "Property", 
      JOptionPane.QUESTION_MESSAGE, 
       null, 
       largeList, 
       largeList[largeList.length-1]);


   System.out.println("Property: " + value);
   
The final dialog type is an all encompassing one,
showOptionDialog. There is only one version of the method. Here, 
you can set everything:

o showOptionDialog(Component parentComponent,
                   Object message,
                   String title,
                   int optionType,
                   int messageType,
                   Icon icon,
                   Object[] options,
                   Object initialValue)

The only thing new here is the options array. This is how you can 
customize the set of available buttons. Notice that this is an 
Object array, not a String array. If the Object is a Component, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -