📄 tij0149.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0148.html">Prev</a> | <a href="tij0150.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Java
1.1 UI APIs
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
1.1<A NAME="Index2108"></A>
has also added some important new functionality, including focus traversal,
desktop color access, printing “inside the sandbox,” and the
beginnings of clipboard support.
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="Index2109"></A><A NAME="Index2110"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Focus
traversal is quite easy, since it’s transparently present in the AWT
library components and you don’t have to do anything to make it work. If
you make your own components and want them to handle focus traversal, you
override <A NAME="Index2111"></A><A NAME="Index2112"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>isFocusTraversable( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to return
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If you want to capture the keyboard focus on a mouse click, you catch the mouse
down event and call <A NAME="Index2113"></A><A NAME="Index2114"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>requestFocus( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc408018714"></a><P></DIV>
<A NAME="Heading448"></A><H3 ALIGN=LEFT>
Desktop
colors
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index2115"></A><A NAME="Index2116"></A><A NAME="Index2117"></A>desktop
colors provide a way for you to know what the various color choices are on the
current user’s desktop. This way, you can use those colors in your
program if you desire. The colors are automatically initialized and placed in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
members of class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SystemColor</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so all you need to do is read the member you’re interested in. The names
are intentionally self-explanatory:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>desktop</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>activeCaption</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>activeCaptionText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>activeCaptionBorder</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>inactiveCaption</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>inactiveCaptionText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>inactiveCaptionBorder</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>window</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowBorder</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>windowText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>menu</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>menuText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>text</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>textText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>textHighlight</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>textHighlightText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>textInactiveText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>control</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>controlText</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>controlHighlight</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>controlLtHighlight</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>controlShadow</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>controlDkShadow</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>scrollbar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>info
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(for
help), and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>infoText
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(for
help text).
</FONT><a name="_Toc408018715"></a><P></DIV>
<A NAME="Heading449"></A><H3 ALIGN=LEFT>
Printing</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Unfortunately,
there isn’t much that’s automatic with printing. Instead you must
go through a number of mechanical, non-OO steps in order to print. Printing a
component graphically can be slightly more automatic: by default, the <A NAME="Index2118"></A><A NAME="Index2119"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>print( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method calls <A NAME="Index2120"></A><A NAME="Index2121"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to do its work. There are times when this is satisfactory, but if you want to
do anything more specialized you must know that you’re printing so you
can in particular find out the page dimensions.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following example demonstrates the printing of both text and graphics, and the
different approaches you can use for printing graphics. In addition, it tests
the printing support:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: PrintDemo.java</font>
<font color="#009900">// Printing with Java 1.1</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> PrintDemo <font color="#0000ff">extends</font> Frame {
Button
printText = <font color="#0000ff">new</font> Button("Print Text"),
printGraphics = <font color="#0000ff">new</font> Button("Print Graphics");
TextField ringNum = <font color="#0000ff">new</font> TextField(3);
Choice faces = <font color="#0000ff">new</font> Choice();
Graphics g = <font color="#0000ff">null</font>;
Plot plot = <font color="#0000ff">new</font> Plot3(); <font color="#009900">// Try different plots</font>
Toolkit tk = Toolkit.getDefaultToolkit();
<font color="#0000ff">public</font> PrintDemo() {
ringNum.setText("3");
ringNum.addTextListener(<font color="#0000ff">new</font> RingL());
Panel p = <font color="#0000ff">new</font> Panel();
p.setLayout(<font color="#0000ff">new</font> FlowLayout());
printText.addActionListener(<font color="#0000ff">new</font> TBL());
p.add(printText);
p.add(<font color="#0000ff">new</font> Label("Font:"));
p.add(faces);
printGraphics.addActionListener(<font color="#0000ff">new</font> GBL());
p.add(printGraphics);
p.add(<font color="#0000ff">new</font> Label("Rings:"));
p.add(ringNum);
setLayout(<font color="#0000ff">new</font> BorderLayout());
add(p, BorderLayout.NORTH);
add(plot, BorderLayout.CENTER);
String[] fontList = tk.getFontList();
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < fontList.length; i++)
faces.add(fontList[i]);
faces.select("Serif");
}
<font color="#0000ff">class</font> PrintData {
<font color="#0000ff">public</font> PrintJob pj;
<font color="#0000ff">public</font> <font color="#0000ff">int</font> pageWidth, pageHeight;
PrintData(String jobName) {
pj = getToolkit().getPrintJob(
PrintDemo.<font color="#0000ff">this</font>, jobName, <font color="#0000ff">null</font>);
<font color="#0000ff">if</font>(pj != <font color="#0000ff">null</font>) {
pageWidth = pj.getPageDimension().width;
pageHeight= pj.getPageDimension().height;
g = pj.getGraphics();
}
}
<font color="#0000ff">void</font> end() { pj.end(); }
}
<font color="#0000ff">class</font> ChangeFont {
<font color="#0000ff">private</font> <font color="#0000ff">int</font> stringHeight;
ChangeFont(String face, <font color="#0000ff">int</font> style,<font color="#0000ff">int</font> point){
<font color="#0000ff">if</font>(g != <font color="#0000ff">null</font>) {
g.setFont(<font color="#0000ff">new</font> Font(face, style, point));
stringHeight =
g.getFontMetrics().getHeight();
}
}
<font color="#0000ff">int</font> stringWidth(String s) {
<font color="#0000ff">return</font> g.getFontMetrics().stringWidth(s);
}
<font color="#0000ff">int</font> stringHeight() { <font color="#0000ff">return</font> stringHeight; }
}
<font color="#0000ff">class</font> TBL <font color="#0000ff">implements</font> ActionListener {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
PrintData pd =
<font color="#0000ff">new</font> PrintData("Print Text Test");
<font color="#009900">// Null means print job canceled:</font>
<font color="#0000ff">if</font>(pd == <font color="#0000ff">null</font>) <font color="#0000ff">return</font>;
String s = "PrintDemo";
ChangeFont cf = <font color="#0000ff">new</font> ChangeFont(
faces.getSelectedItem(), Font.ITALIC,72);
g.drawString(s,
(pd.pageWidth - cf.stringWidth(s)) / 2,
(pd.pageHeight - cf.stringHeight()) / 3);
s = "A smaller point size";
cf = <font color="#0000ff">new</font> ChangeFont(
faces.getSelectedItem(), Font.BOLD, 48);
g.drawString(s,
(pd.pageWidth - cf.stringWidth(s)) / 2,
(<font color="#0000ff">int</font>)((pd.pageHeight -
cf.stringHeight())/1.5));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -