📄 tij0151.html
字号:
and for each
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the array that’s the second argument to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>makeBPanel( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it adds an object of the class represented by the first argument:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: ButtonGroups.java</font>
<font color="#009900">// Uses reflection to create groups of different</font>
<font color="#009900">// types of AbstractButton.</font>
<font color="#0000ff">package</font> c13.swing;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> com.sun.java.swing.*;
<font color="#0000ff">import</font> com.sun.java.swing.border.*;
<font color="#0000ff">import</font> java.lang.reflect.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> ButtonGroups <font color="#0000ff">extends</font> JPanel {
<font color="#0000ff">static</font> String[] ids = {
"June", "Ward", "Beaver",
"Wally", "Eddie", "Lumpy",
};
<font color="#0000ff">static</font> JPanel
makeBPanel(Class bClass, String[] ids) {
ButtonGroup bg = <font color="#0000ff">new</font> ButtonGroup();
JPanel jp = <font color="#0000ff">new</font> JPanel();
String title = bClass.getName();
title = title.substring(
title.lastIndexOf('.') + 1);
jp.setBorder(<font color="#0000ff">new</font> TitledBorder(title));
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < ids.length; i++) {
AbstractButton ab = <font color="#0000ff">new</font> JButton("failed");
<font color="#0000ff">try</font> {
<font color="#009900">// Get the dynamic constructor method</font>
<font color="#009900">// that takes a String argument:</font>
Constructor ctor = bClass.getConstructor(
<font color="#0000ff">new</font> Class[] { String.<font color="#0000ff">class</font> });
<font color="#009900">// Create a new object:</font>
ab = (AbstractButton)ctor.newInstance(
<font color="#0000ff">new</font> Object[]{ids[i]});
} <font color="#0000ff">catch</font>(Exception ex) {
System.out.println("can't create " +
bClass);
}
bg.add(ab);
jp.add(ab);
}
<font color="#0000ff">return</font> jp;
}
<font color="#0000ff">public</font> ButtonGroups() {
add(makeBPanel(JButton.<font color="#0000ff">class</font>, ids));
add(makeBPanel(JToggleButton.<font color="#0000ff">class</font>, ids));
add(makeBPanel(JCheckBox.<font color="#0000ff">class</font>, ids));
add(makeBPanel(JRadioButton.<font color="#0000ff">class</font>, ids));
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String args[]) {
Show.inFrame(<font color="#0000ff">new</font> ButtonGroups(), 500, 300);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
title for the border is taken from the name of the class, stripping off all the
path information. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>AbstractButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is initialized to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that has the label “Failed” so if you ignore the exception message,
you’ll still see the problem on screen. The <A NAME="Index2308"></A><A NAME="Index2309"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getConstructor( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method produces a <A NAME="Index2310"></A><A NAME="Index2311"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Constructor</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object that takes the array of arguments of the types in the <A NAME="Index2312"></A><A NAME="Index2313"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">array
passed to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getConstructor( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Then all you do is call <A NAME="Index2314"></A><A NAME="Index2315"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>newInstance( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
passing it an array of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
containing your actual arguments – in this case, just the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
from the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ids</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
array.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
adds a little complexity to what is a simple process. To get “exclusive
or” behavior with buttons, you create a button group and add each button
for which you want that behavior to the group. When you run the program,
you’ll see that all the buttons except
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
exhibit this “exclusive or” behavior.
</FONT><a name="_Toc408018732"></a><P></DIV>
<A NAME="Heading469"></A><H3 ALIGN=LEFT>
Icons</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can use an <A NAME="Index2316"></A><A NAME="Index2317"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Icon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inside a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JLabel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or anything that inherits from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>AbstractButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(including
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
<A NAME="Index2318"></A><A NAME="Index2319"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JCheckbox</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
<A NAME="Index2320"></A><A NAME="Index2321"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JradioButton,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the different kinds of <A NAME="Index2322"></A><A NAME="Index2323"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JMenuItem</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Using
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Icon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JLabel</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
is quite straightforward (you’ll see an example later). The following
example explores all the additional ways you can use
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Icon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
with buttons and their descendants.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can use any
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>gif</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
files you want, but the ones used in this example are part of the book’s
code distribution, available at
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>www.BruceEckel.com</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
To open a file and bring in the image, simply create an <A NAME="Index2324"></A><A NAME="Index2325"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ImageIcon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and hand it the file name. From then on, you can use the resulting
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Icon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in your program.
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Faces.java</font>
<font color="#009900">// Icon behavior in JButtons</font>
<font color="#0000ff">package</font> c13.swing;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> com.sun.java.swing.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Faces <font color="#0000ff">extends</font> JPanel {
<font color="#0000ff">static</font> Icon[] faces = {
<font color="#0000ff">new</font> ImageIcon("face0.gif"),
<font color="#0000ff">new</font> ImageIcon("face1.gif"),
<font color="#0000ff">new</font> ImageIcon("face2.gif"),
<font color="#0000ff">new</font> ImageIcon("face3.gif"),
<font color="#0000ff">new</font> ImageIcon("face4.gif"),
};
JButton
jb = <font color="#0000ff">new</font> JButton("JButton", faces[3]),
jb2 = <font color="#0000ff">new</font> JButton("Disable");
<font color="#0000ff">boolean</font> mad = <font color="#0000ff">false</font>;
<font color="#0000ff">public</font> Faces() {
jb.addActionListener(<font color="#0000ff">new</font> ActionListener() {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e){
<font color="#0000ff">if</font>(mad) {
jb.setIcon(faces[3]);
mad = <font color="#0000ff">false</font>;
} <font color="#0000ff">else</font> {
jb.setIcon(faces[0]);
mad = <font color="#0000ff">true</font>;
}
jb.setVerticalAlignment(JButton.TOP);
jb.setHorizontalAlignment(JButton.LEFT);
}
});
jb.setRolloverEnabled(<font color="#0000ff">true</font>);
jb.setRolloverIcon(faces[1]);
jb.setPressedIcon(faces[2]);
jb.setDisabledIcon(faces[4]);
jb.setToolTipText("Yow!");
add(jb);
jb2.addActionListener(<font color="#0000ff">new</font> ActionListener() {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e){
<font color="#0000ff">if</font>(jb.isEnabled()) {
jb.setEnabled(<font color="#0000ff">false</font>);
jb2.setText("Enable");
} <font color="#0000ff">else</font> {
jb.setEnabled(<font color="#0000ff">true</font>);
jb2.setText("Disable");
}
}
});
add(jb2);
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String args[]) {
Show.inFrame(<font color="#0000ff">new</font> Faces(), 300, 200);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">An
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Icon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be used in many constructors, but you can also use <A NAME="Index2326"></A><A NAME="Index2327"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setIcon( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to add or change an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Icon</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This example also shows how a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(or any
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>AbstractButton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
can set the various different sorts of icons that appear when things happen to
that button: when it’s pressed, disabled, or “rolled <A NAME="Index2328"></A><A NAME="Index2329"></A>over”
(the mouse moves over it without clicking). You’ll see that this gives
the button a rather animated feel.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that a <A NAME="Index2330"></A><A NAME="Index2331"></A>tool
tip is also added to the button.
</FONT><a name="_Toc408018733"></a><P></DIV>
<A NAME="Heading470"></A><H3 ALIGN=LEFT>
Menus<P><A NAME="Index2332"></A><A NAME="Index2333"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Menus
are much improved and more flexible in Swing – for example, you can use
them just about anywhere, including panels and applets. The syntax for using
them is much the same as it was in the old AWT, and this preserves the same
problem present in the old AWT: you must hard-code your menus and there
isn’t any support for menus as resources (which, among other things,
would make them easier to change for other languages). In addition, menu code
gets long-winded and sometimes messy. The following approach takes a step in
the direction of solving this problem by putting all the information about each
menu into a two-dimensional <A NAME="Index2334"></A><A NAME="Index2335"></A>array
of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -