📄 jan04_johnz.txt
字号:
System.out.println("Day: " + value);
}
};
inputCombo.addActionListener(inputComboListener);
contentPane.add(inputCombo);
JButton inputList = new JButton("Input List");
ActionListener inputListListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object largeList[] =
System.getProperties().keySet().toArray();
String value =
(String)JOptionPane.showInputDialog(
Options.this, "Which Property?", "Property",
JOptionPane.QUESTION_MESSAGE, null,
largeList, largeList[largeList.length-1]);
System.out.println("Property: " + value);
}
};
inputList.addActionListener(inputListListener);
contentPane.add(inputList);
JButton all = new JButton("All");
ActionListener allListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String options[] =
{"Yes", "Not Now", "Go Away"};
int value = JOptionPane.showOptionDialog(
Options.this,
"Lunch?",
"Lunch Time",
JOptionPane.YES_NO_OPTION,
// Message type
JOptionPane.QUESTION_MESSAGE,
null, // Use default icon for message type
options,
options[1]);
if (value == JOptionPane.CLOSED_OPTION) {
System.out.println("Closed window");
} else {
System.out.println(
"Selected: " + options[value]);
}
}
};
all.addActionListener(allListener);
contentPane.add(all);
JButton wide = new JButton("Wide");
ActionListener wideListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg =
"This is a really long message. " +
"This is a really long message. " +
"This is a really long message. " +
"This is a really long message. " +
"This is a really long message. " +
"This is a really long message.";
JOptionPane pane = getNarrowOptionPane(50);
pane.setMessage(msg);
pane.setMessageType(
JOptionPane.INFORMATION_MESSAGE);
JDialog dialog =
pane.createDialog(Options.this, "Width 50");
dialog.show();
}
};
wide.addActionListener(wideListener);
contentPane.add(wide);
JButton twoLine = new JButton("Two Line");
ActionListener twoLineListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg[] = {"Welcome", "Home"};
JOptionPane.showMessageDialog(
Options.this, msg);
}
};
twoLine.addActionListener(twoLineListener);
contentPane.add(twoLine);
JButton slider = new JButton("Slider");
ActionListener sliderListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane optionPane = new JOptionPane();
JSlider slider = getSlider(optionPane);
Object msg[] = {"Select a value:", slider};
optionPane.setMessage(msg);
optionPane.setMessageType(
JOptionPane.QUESTION_MESSAGE);
optionPane.setOptionType(
JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = optionPane.createDialog(
Options.this, "Select Value");
dialog.show();
Object value = optionPane.getValue();
if (value == null || !(value instanceof Integer)) {
System.out.println("Closed");
} else {
int i = ((Integer)value).intValue();
if (i == JOptionPane.CLOSED_OPTION) {
System.out.println("Closed");
} else if (i == JOptionPane.OK_OPTION) {
System.out.println("OKAY - value is: " +
optionPane.getInputValue());
} else if (i == JOptionPane.CANCEL_OPTION) {
System.out.println("Cancelled");
}
}
}
};
slider.addActionListener(sliderListener);
contentPane.add(slider);
setSize(300, 200);
}
public static void main(String args[]) {
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.defaultCueList"));
JFrame frame = new Options();
Runnable runner = new FrameShower(frame);
EventQueue.invokeLater(runner);
}
}
For additional information about JOptionPane, see the How to Make
Dialogs trail in The Java Tutorial
(http://java.sun.com/tutorial/uiswing/components/dialog.html)
and the javadoc for the JOptionPane class
(http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MONITORING CLASS LOADING AND GARBAGE COLLECTION
Have you ever wondered what classes are loaded when you launch an
application or from where the classes are loaded? Have you ever
wondered when garbage collection runs or how long it takes? The
"java" command line tool offers several different command line
options that you can use to get answers to those questions.
You might already be familiar with a number of command line
options available with the "java" command line tool, such as -cp,
-Xms, and -Xmx. The -cp option is used for specifying the
classpath. The -Xms and -Xmx options are used to specify the heap
size. For example, instead of setting the CLASSPATH environment
variable, you can use the -cp option to tell the system to look
in a specific directory for necessary class files:
java -cp ExampleDir MyExample
Here, the system will look in the ExampleDir subdirectory for the
MyExample.class file and anything else needed besides the system
classes. The ExampleDir in the command line tells the system to
look only in the ExampleDir directory (assume that it's the
parent directory). If MyExample.class is located in the current
working directory, the system would not find it.
Two less frequently used command line features report on class
loading and garbage collection. The -verbose:class option reports
when a class is loaded into the Java virtual machine and from
where it came. For instance, if you use the -verbose:class option
when loading the SwingSet2 demo that comes with the J2SE 1.4.2
SDK, you get a report on the many different classes that are
loaded as part of the demo, such the following two:
java -verbose:class -jar
C:\j2sdk1.4.2\demo\jfc\SwingSet2\SwingSet2.jar
[Loaded FilePreviewer]
[Loaded javax.swing.plaf.TableUI from
C:\j2sdk1.4.2\jre\lib\rt.jar]
The first line indicates that the class came from the main JAR
for the demo (assuming it was started with
java -jar SwingSet2.jar). The second line indicates that the
TableUI class was loaded from the rt.jar file that comes with the
runtime located in the c:\j2sdk1.4.2\jre directory. (From there,
the rt.jar file is located in the lib subdirectory.) Different
implementations of the Java platform can have different formats
here. The only requirement is that -verbose:class displays
messages as classes get loaded and unloaded.
Let's see when classes are loaded, and how many classes are
needed for the following simple program:
public class Sample {
public static void main(String args[]) {
System.out.println("Hello, World");
}
}
Compile the Sample class. Then run it with the -verbose:class
option enabled:
java -verbose:class Sample
When you run the command, you'll see that this simple program
requires the opening of five jar files (such as rt.jar) and the
loading of almost 250 classes.
To see an example of a class unloading message, try the
-verbose:class command line option with the RunItReload class
shown in the August 19, 2003 Tech Tip titled "Unloading and
Reloading Classes"
(http://java.sun.com/developer/JDCTechTips/2003/tt0819.html#2).
The -verbose:gc option reports on each garbage collection event.
This includes the time for garbage collection to run, and the
before and after heap sizes. This is demonstrated in the
following lines:
[GC 27872K->26296K(42216K), 0.0069590 secs]
[GC 28973K->26455K(42216K), 0.0036812 secs]
[GC 29134K->26474K(42216K), 0.0016388 secs]
[GC 29117K->26487K(42216K), 0.0008859 secs]
[GC 29134K->26498K(42216K), 0.0009197 secs]
[GC 29180K->26479K(42216K), 0.0008711 secs]
[GC 29149K->26484K(42216K), 0.0008716 secs]
Like the output for -verbose:class, there is no requirement for
output format, and it is subject to change without notice. The
"GC" at the beginning indicates what kind of collection occurred.
The number before the "->" is the heap occupancy before the
collection. The number after the "->" is the heap occupancy after
the collection. The number in parentheses is the currently
allocated size of the heap. The seconds are the duration of the
collection.
This information can be useful in debugging. For example, it
could help you determine if garbage collection happened at a
critical point in time, and might have caused a program to crash.
This sometimes happens when mixing Java and C/C++ code with JNI,
especially when there is an underlying bug on the C/C++ code side.
If you're ever curious about why it takes so long for an
application to start, or if garbage collection in the middle of
an operation appears to cause a problem, be sure to try out these
command line options.
For additional information on these and other command line
options, see the documentation on the java command specific to
your platform:
Linux:
http://java.sun.com/j2se/1.4.2/docs/tooldocs/linux/java.html
Windows:
http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/java.html
Solaris:
http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/java.html
. . . . . . . . . . . . . . . . . . . . . . .
IMPORTANT: Please read our Terms of Use, Privacy, and Licensing
policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/
http://developers.sun.com/dispatcher.jsp?uid=6910008
* FEEDBACK
Comments? Please enter your feedback on the Tech Tips at:
http://developers.sun.com/contact/feedback.jsp?category=sdn
* SUBSCRIBE/UNSUBSCRIBE
Subscribe to other Java developer Tech Tips:
- Enterprise Java Technologies Tech Tips. Get tips on using
enterprise Java technologies and APIs, such as those in the
Java 2 Platform, Enterprise Edition (J2EE).
- Wireless Developer Tech Tips. Get tips on using wireless
Java technologies and APIs, such as those in the Java 2
Platform, Micro Edition (J2ME).
To subscribe to these and other JDC publications:
- Go to the JDC Newsletters and Publications page,
(http://developer.java.sun.com/subscription/),
choose the newsletters you want to subscribe to and click
"Update".
- To unsubscribe, go to the subscriptions page,
(http://developer.java.sun.com/subscription/),
uncheck the appropriate checkbox, and click "Update".
- To use our one-click unsubscribe facility, see the link at
the end of this email:
- ARCHIVES
You'll find the Core Java Technologies Tech Tips archives at:
http://java.sun.com/developer/TechTips/index.html
- COPYRIGHT
Copyright 2004 Sun Microsystems, Inc. All rights reserved.
4150 Network Circle, Santa Clara, California 95054 USA.
This document is protected by copyright. For more information, see:
http://java.sun.com/jdc/copyright.html
Core Java Technologies Tech Tips
January 22, 2004
Trademark Information: http://www.sun.com/suntrademarks/
Java, J2SE, J2EE, J2ME, and all Java-based marks are trademarks
or registered trademarks of Sun Microsystems, Inc. in the
United States and other countries.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -