Java编程思想第4版[中文版](PDF格式)-第117部分
按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
〃KEY pressed: 〃;
break;
case KeyEvent。KEY_RELEASED:
display。evnt'Display。KEY' =
〃KEY released: 〃;
break;
case KeyEvent。KEY_TYPED:
display。evnt'Display。KEY' =
〃KEY typed: 〃;
break;
default:
}
int code = e。getKeyCode();
display。evnt'Display。KEY' +=
KeyEvent。getKeyText(code);
repaint();
super。processKeyEvent(e);
}
public void processMouseEvent(MouseEvent e) {
switch(e。getID()) {
case MouseEvent。MOUSE_CLICKED:
requestFocus(); // Get FOCUS on click
display。evnt'Display。MOUSE' =
〃MOUSE clicked〃;
break;
case MouseEvent。MOUSE_PRESSED:
display。evnt'Display。MOUSE' =
〃MOUSE pressed〃;
break;
case MouseEvent。MOUSE_RELEASED:
display。evnt'Display。MOUSE' =
〃MOUSE released〃;
break;
case MouseEvent。MOUSE_ENTERED:
display。evnt'Display。MOUSE' =
〃MOUSE entered〃;
break;
case MouseEvent。MOUSE_EXITED:
display。evnt'Display。MOUSE' =
〃MOUSE exited〃;
break;
default:
}
display。evnt'Display。MOUSE' +=
〃; x = 〃 + e。getX() +
〃; y = 〃 + e。getY ();
repaint();
super。processMouseEvent(e);
}
public void
processMouseMotionEvent(MouseEvent e) {
switch(e。getID()) {
446
…………………………………………………………Page 448……………………………………………………………
case MouseEvent。MOUSE_DRAGGED:
display。evnt'Display。MOUSE_MOVE' =
〃MOUSE dragged〃;
break ;
case MouseEvent。MOUSE_MOVED:
display。evnt'Display。MOUSE_MOVE' =
〃MOUSE moved〃;
break;
default:
}
display。evnt'Display。MOUSE_MOVE' +=
〃; x = 〃 + e。getX() +
〃; y = 〃 + e。getY();
repaint();
super。processMouseMotionEvent(e);
}
}
class MyButton extends Button {
int clickCounter;
String label = 〃〃;
public MyButton() {
enableEvents(AWTEvent。ACTION_EVENT_MASK);
}
public void paint(Graphics g) {
g。setColor(Color。green);
Dimension s = getSize();
g。fillRect(0; 0; s。width; s。height);
g。setColor(Color。black);
g。drawRect(0; 0; s。width 1; s。height 1);
drawLabel(g);
}
private void drawLabel(Graphics g) {
FontMetrics fm = g。getFontMetrics();
int width = fm。stringWidth(label);
int height = fm。getHeight();
int ascent = fm。getAscent();
int leading = fm。getLeading();
int horizMargin =
(getSize()。width width)/2;
int verMargin =
(getSize()。height height)/2;
g。setColor(Color。red);
g。drawString(label; horizMargin;
verMargin + ascent + leading);
}
public void processActionEvent(ActionEvent e) {
clickCounter++;
label = 〃click #〃 + clickCounter +
〃 〃 + e。toString();
repaint();
super。processActionEvent(e);
447
…………………………………………………………Page 449……………………………………………………………
}
}
public class BadTechnique extends Frame {
BadTechnique() {
setLayout(new GridLayout(2;2));
add(new EnabledPanel(1; Color。cyan));
add(new EnabledPanel(2; Color。lightGray));
add(new EnabledPanel(3; Color。yellow));
// You can also do it for Windows:
enableEvents(AWTEvent。WINDOW_EVENT_MASK);
}
public void processWindowEvent(WindowEvent e) {
System。out。println(e);
if(e。getID() == WindowEvent。WINDOW_CLOSING) {
System。out。println (〃Window Closing〃);
System。exit(0);
}
}
public static void main(String'' args) {
Frame f = new BadTechnique();
f。setTitle(〃Bad Technique〃);
f。setSize(700;700);
f。setVisible(true);
}
} ///:~
的确,它能够工作。但却实在太蹩脚,而且很难编写、阅读、调试、维护以及再生。既然如此,为什么还不
使用内部接收器类呢?
13。17 Java 1。1 用户接口 API
Java 1。1 版同样增加了一些重要的新功能,包括焦点遍历,桌面色彩访问,打印“沙箱内”及早期的剪贴板
支持。
焦点遍历十分的简单,因为它显然存在于AWT 库里的组件并且我们不必为使它工作而去做任何事。如果我们
制造我们自己组件并且想使它们去处理焦点遍历,我们过载 isFocusTraversable()以使它返回真值。如果我
们想在一个鼠标单击上捕捉键盘焦点,我们可以捕捉鼠标按下事件并且调用requestFocus()需求焦点方法。
13。17。1 桌面颜色
利用桌面颜色,我们可知道当前用户桌面都有哪些颜色选择。这样一来,就可在必要的时候通过自己的程序
来运用那些颜色。颜色都会得以自动初始化,并置于SystemColor 的static 成员中,所以要做的唯一事情就
是读取自己感兴趣的成员。各种名字的意义是不言而喻的:desktop,activeCaption,
activeCaptionText,activeCaptionBorder, inactiveCapt ion, inactiveCaptionText,
inactiveCaptionBorder, window, windowBorder , windowText, menu,menuText,text, textText,
textHighlight, textHighlightText ,textInactiveText,control, controlText, controlHighlight,
controlLtHighlight,controlShadow ,controlDkShadow, scrollbar, info (用于帮助)以及infoText
(用于帮助文字)。
13。17。2 打印
非常不幸,打印时没有多少事情是可以自动进行的。相反,为完成打印,我们必须经历大量机械的、非 OO
(面向对象)的步骤。但打印一个图形化的组件时,可能多少有点儿自动化的意思:默认情况下,print()方
法会调用paint()来完成自己的工作。大多数时候这都已经足够了,但假如还想做一些特别的事情,就必须
448
…………………………………………………………Page 450……………………………………………………………
知道页面的几何尺寸。
下面这个例子同时演示了文字和图形的打印,以及打印图形时可以采取的不同方法。此外,它也对打印支持
进行了测试:
//: PrintDemo。java
// Printing with Java 1。1
import java。awt。*;
import java。awt。event。*;
public class PrintDemo extends Frame {
Button
printText = new Button(〃Print Text〃);
printGraphics = new Button(〃Print Graphics〃);
TextField ringNum = new TextField(3);
Choice faces = new Choice();
Graphics g = null;
Plot plot = new Plot3(); // Try different plots
Toolkit tk = Toolkit。getDefaultToolkit();
public PrintDemo() {
ringNum。setText(〃3〃);
ringNum。addTextListener(new RingL());
Panel p = new Panel();
p。setLayout(new FlowLayout());
printText。addActionListener(new TBL());
p。add(printText);
p。add(new Label(〃Font:〃));
p。add(faces);
printGraphics。addActionListener(new GBL());
p。add(printGraphics);
p。add(new Label(〃Rings:〃));
p。add(ringNum);
setLayout(new BorderLayout());
add(p; BorderLayout。NORTH);
add(plot; BorderLayout。CENTER);
String'' fontList = tk。getFontList();
for(int i = 0; i 《 fontList。length; i++)
faces。add(fontList'i');
faces。select(〃Serif〃);
}
class PrintData {
public PrintJob pj;
public int pageWidth; pageHeight;
PrintData(String jobName) {
pj = getToolkit()。getPrintJob(
PrintDemo。this; jobName; null);
if(pj != null) {
pageWidth = pj。getPageDimension()。width;
pageHeight= pj。getPageDimension()。height;
g = pj。getGraphics();
}
}
void end() { pj。end(); }
449
…………………………………………………………Page 451……………………………………………………………
}
class ChangeFont {
private int stringHeight;
ChangeFont(String face; int style;int point){
if(g != null) {
g。setFont(new Font(face; style; point));
stringHeight =
g。getFontMetrics()。getHeight();
}
}
int stringWidth(String s) {
return g。getFontMetrics()。stringWidth(s);
}
int stringHeight() { return stringHeight; }
}
class TBL implements ActionListener {
public void actionPerformed(ActionEvent e) {
PrintData pd =
new PrintData(〃Print Text Test〃);
// Null means print job canceled:
if (pd == null) return;
String s = 〃PrintDemo〃;
ChangeFont cf = new 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 = new ChangeFont(
faces。getSelectedItem(); Font。BOLD; 48);
g。drawString(s;
(pd。pageWidth cf。stringWidth(s)) / 2;
(int)((pd。pageHeight
cf。stringHeight())/1。5));
g。dispose();
pd。end();
}
}
class GBL implements ActionListener {
public void actionPerformed(ActionEvent e) {
PrintData pd =
new PrintData(〃Print Graphics Test〃);
if(pd == null) return;
plot。print(g);
g。dispose();
pd。end();
}
}
class RingL implements TextListener {
public void textValueChanged(TextEvent e) {
int i = 1;
450
…………………………………………………………Page 452……………………………………………………………
try {
i = Integer。parseInt(ringNum。getText());
} catch(NumberFormatException ex) {
i = 1;