Click here to load reader
Upload
javadevru
View
149
Download
0
Embed Size (px)
Citation preview
Java 11. java.awt20 2003 : ( Sun ) ( Sun ) < [email protected]>Copyright 2003 Sun , , .
java.awt ....
Rendered by www.RenderX.com
ii
11. java.awt...................................................................................................11. .......................................................................................................................... 2 2. ............................................................................................................................2 2.1. HTML .................................................................................................3 2.2. ............................................................................................ 5 2.3. .................................................................................................... 5 2.4. .................................................................................................5 2.5. ..........................................................................6 2.6. ............................................................................................................7 2.7. .....................................................7 2.8. Graphics......................................................................... 7 2.9. ......................................................................................................................... 8 2.9.1. Color..................................................................................... 9 2.10. ................................................................................................................. 9 2.10.1. .......................................................................... 10 2.10.2. : FontMetrics............................................. 11 2.10.3. FontMetrics....................................................................... 11 2.10.4. ...............................................................................12 3. ............................................................................................................. 12 4. .................................................................................................. 13 5. ................................................................................................23 6. ................................................................................................................................ 26 7. ...............................................................................................................................27 8. ....................................................................................................... 28 8.1. "" Java............................................................................. 32 8.2. "" ...................... 33 9. .................................................................................................................... 34 10. .................................................................................................35
Java
Rendered by www.RenderX.com
. 1 38
11. java.awt .1. ......................................................................................................................... 2 2. ........................................................................................................................... 22.1. HTML .......................................................................................................3 2.2. ..................................................................................................5 2.3. .......................................................................................................... 5 2.4. .......................................................................................................5 2.5. ................................................................................6 2.6. ................................................................................................................. 7 2.7. ...........................................................7 2.8. Graphics............................................................................... 7 2.9. ............................................................................................................................... 8 2.9.1. Color........................................................................................... 9 2.10. .......................................................................................................................9 2.10.1. ................................................................................ 10 2.10.2. : FontMetrics................................................... 11 2.10.3. FontMetrics.............................................................................11 2.10.4. .....................................................................................12
3. ............................................................................................................ 12 4. ................................................................................................. 13 5. ............................................................................................... 23 6. ............................................................................................................................... 26 7. .............................................................................................................................. 27 8. ...................................................................................................... 288.1. "" Java................................................................................... 32 8.2. "" ............................ 33
9. ................................................................................................................... 34 10. ................................................................................................ 35
Java
Rendered by www.RenderX.com
. 2 38
1. , , , , , . . , - , . , , "" - . Java Abstraction Window Toolkit (AWT) . AWT .
2. (applets) - , Internet, , , HTML. , . HelloWorld, :import java.awt.*; import java.applet.*; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 20, 20); } }
, java.applet java.awt. paint, Applet. , Graphics. . drawString, Graphics, (20,20) "Hello World". , , html-.
Java
Rendered by www.RenderX.com
HTML
. 3 38
html-, appletviewer . .
2.1. HTML HTML-, appletviewer. appletviewer , . : [< PARAM NAME = AttributeNamel VALUE = AttributeValuel >] [< PARAM NAME = AttributeName2 VALUE = AttributeValue2 >] [HTML-, Java]
CODE = appletClassFile CODE - , , . codebase, , , CODEBASE. Java 1.1 OBJECT.
OBJECT = appletClassSerialFile , , . init(), start(). CODE, OBJECT, .
WIDTH = pixels HEIGHT = pixels WIDTH HEIGHT - , .
ARCHIVE = jarFiles jar- ( ), Web-. Java
Rendered by www.RenderX.com
. 4 38
, , , . JAR, TAR Unix:c:\> jar cf soundmap.jar *.class image.gif sound.wav
, jar- . (Lotus JavaBeans, Borland JBuilder) jar-. CODEBASE = codebaseURL CODEBASE - , URL , , ( CODE). , HTML-. CODEBASE , HTML-. ALT = alternateAppletText ALT - , , ( , ) , , . , HTML-, , . NAME = appletInstanceName NAME - , . , . , MyApplet Applet "Duke", : MyApplet a = getAppletContext().getApplet("Duke"); , , , . ALIGN = alignment ALIGN - , . , IMG, - LEFT, RIGHT, TOP, TEXTTOP, MIDDLE, ABSMIDDLE, BASELINE, BOTTOM, ABSBOTTOM. VSPACE = pixels HSPACE = pixels (VSPACE), (HSPACE). , IMG. PARAM NAME = appletAttribute1 VALUE = value1 HTML- . , getParameter(), . Java
Rendered by www.RenderX.com
. 5 38
2.2. getParameter(String) getParameter String, . - , - . getParameter :
, :String String String String FontName = getParameter("fontName"); FontSize = Integer.parseInt(getParameter("fontSize")); Leading = Float.valueOf(getParameter("leading")); PaidUp = Boolean.valueOf(getParameter("accountEnabled"));
2.3. getDocumentBase getCodeBase , , . Java , HTML-, ( - getDocumentBase), , class- ( - getCodeBase). AppletContext showDocument AppletContext , . showDocument , .
2.4. : . , , :System.out.println("Hello there, welcome to Java");
, , . Netscape Java Options, "Show Java Console".
Java
Rendered by www.RenderX.com
. 6 38
showStatus pletviewer Java. .
2.5. , Applet, , . init init . . start start init. , . , init - , start HTML-, , . , , WWW-, , start. paint paint . AWT , , , . , , , paint. update update Applet , paint. paint , update - , , . , update. update, paint, AWT, update. stop stop , HTML-, . stop . , . , , start. destroy destroy , (, Netscape) , . , .
Java
Rendered by www.RenderX.com
. 7 38
2.6. HelloWorldApplet. paint, . Applet , . , update . update repaint. repaint repaint . , , update. , , update . AWT, update . , repaint(time) - , . repaint(time) repaint, ( repaint). repaint(x, y, w, h) , , . repaint(time, x, , w, h) - .
2.7. , , . size, Dimension. Dimension, width height:Dimension d = size(); System.out.println(d. width + "," + d.height);
2.8. Graphics Graphics . , . drawRect, drawOval, fillRect fillOval : int x, int y, int width int height. , width height . drawLinedrawline(int x1, int 1, int 2, int 2) Java
Rendered by www.RenderX.com
. 8 38
(1,1) (2,2). 1 . . drawArc fillArc drawArc fillArc :drawArc(int x, int , int width, int height, int startAngle, int sweepAngle)
(fillArc ) , (x,y,width, height), startAngle sweepAngle. 3 , (, 90 12 , 180 - 9 , ). drawPolygon fillPolygon :drawPolygon(int[], int[], int) fillPolygon(int[], int[], int)
drawPolygon ( ), , , - . drawPolygon . , , . Java Graphics.
2.9. AWT , . , , Java , . , - , , , . Color.black - - black, white, red, green, blue, cyan, yellow, magenta, orange, pink, gray, darkGray lightGray. . Color(int, int, int) 0 255 , . Color(int) - , , . 16-23, - 8-15, - 0-7. Java
Rendered by www.RenderX.com
. 9 38
Color(float, float, float) , Color(float, float, float), float ( 0.0 1.0) , .
2.9.1. Color HSBtoRGB(float, float, float) RGBtoHSB(int, int, int, float[]) HSBtoRGB , , (HSB), RGB, Color(int). RGBtoHSB , , float HSB, . HSB (Hue-Saturation-Brightness, --) Red-Green-Blue . ( 0.0 1.0, , , , , , , , ). ( 0.0 1.0) - , . - 0.0 1.0, , - . getRed(), getGreen(), getBlue() . getRGB() , , red = Oxff & (getRGB() >> 16); green = Oxff & (getRGB() >> 8); blue = Oxff & getRGB();
Graphics: setPaintMode() setXORMode(Color) paint - , . XOR , XOR ( ) ( ).
2.10. AWT Java
Rendered by www.RenderX.com
. 10 38
. , Java ./* * * * */ import java.applet.*; import java.awt.*; public class WhatFontsAreHere extends Applet { public void init() { String fontList[]; // : // Toolkit.getDefaultToolkit().getFontList() fontList = GraphicsEnvironment.getLocalGraphicsEnvironment(). getAvailableFontFamilyNames(); for (int i=0; i < fontList.length; i++) { System.out.println(i + ": " + fontList[i]); } } }
drawString drawString(String, x, ). . (,) , , . , , , FontMetrics.
2.10.1. Font , :Font StrongFont = new Font("Helvetica", Font.BOLD|Font.ITALIC, 24);
: Dialog, Helvetica, TimesRoman, Courier Symbol. . - Font.PLAIN, Font.BOLD Font.ITALIC, , . . getFamily getName
Java
Rendered by www.RenderX.com
. 11 38
getFamily . getName . getSize , . getStyle , . Font: - PLAIN, BOLD ITALIC. isBold, isItalic, isPlain true , - (bold), (italic) (plain), .
2.10.2. : FontMetrics Java , FontMetrics . - , : (height) - . (baseline) - , ( (descent)). (ascent) - . (descent) - .
2.10.3. FontMetrics FontMetrics: stringWidth . bytesWidth, charsWidth . getAscent, getDescent, getHeight , . . - g . .. getMaxAscent getMaxDescent .
Java
Rendered by www.RenderX.com
. 12 38
2.10.4. FontMetrics , , , ./* * * * */ import java.applet.*; import java.awt.*; public class HelloWorld extends Applet { final Font f = new Font("Helvetica", Font.BOLD, 18); public void paint(Graphics g) { Dimension d = this.size(); g.setColor(Color.white); g.fillRect(0,0,d.width,d.height); g.setColor(Color.black); g.setFont(f); drawCenteredString("Hello World!", d.width, d.height, g); g.drawRect(0,0,d.width-1,d.height-1); } public void drawCenteredString(String s, int w, int h, Graphics g) { FontMetrics fm = g.getFontMetrics(); int x = (w - fm.stringWidth(s)) / 2; int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent()))/2); g.drawString(s, x, y); } }
:
3. , Graphics Fonts AWT, . Component - , - , , , / , , ,
Java
Rendered by www.RenderX.com
. 13 38
, ( 100 ). Component. Container Container - Component, , , . Container LayoutManager, . Panel Panel - Container. , . Panel , . add Panel . , - , setLocation, setSize setBounds Component. Panel - Applet. , Applet, paint update Panel. , Panel, AWT, Panel .
4. Canvas Canvas , . Canvas . , , "" - , . Canvas GrayCanvas, . , ./* */ import java.awt.*; import java.applet.*; class GrayCanvas extends Canvas { Color gray; public GrayCanvas(float g) {
Java
Rendered by www.RenderX.com
. 14 38
gray = new Color(g, g, g); } public void paint(Graphics g) { Dimension size = size(); g.setColor(gray); g.fillRect(0, 0, size.width, size.height); g.setColor(Color.black); g.drawRect(0, 0, size.width-1, size.height-1); } } public class PanelDemo extends Applet { static final int n = 4; public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { float g = (i * n + j) / (float) (n * n); Canvas = new GrayCanvas(g); add(c); c.setSize(width / n, height / n); c.setLocation(i * width / n, j * height / n); } } } }
:
Canvas , size, Dimension. , Canvas resize move. , . setLayout(null). Label Label , , String - , . , , Component. Java
Rendered by www.RenderX.com
. 15 38
getFont/setFont getForeground/setForeground. setText. Label LEFT, RIGHT CENTER. , , - ./* */ import java.awt.*; import java.applet.*; public class LabelDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); Label left = new Label("Left", Label.LEFT); Label right = new Label("Right", Label.RIGHT); Label center = new Label("Center", Label.CENTER); add(left); add(right); add(center); left.setBounds(0, 0, width, height / 3); right.setBounds(0, height / 3, width, height / 3); center.setBounds(0, 2 * height / 3, width, height / 3); } }
, Label, reshape. , - 1/3 . , :
Button - , Label ( ). , , . , ./* */ import java.awt.*; Java
Rendered by www.RenderX.com
. 16 38
import java.applet.*; public class ButtonDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); Button yes = new Button("Yes"); Button no = new Button("No"); Button maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.setBounds(0, 0, width, height / 3); no.setBounds(0, height / 3, width, height / 3); maybe.setBounds(0, 2 * height / 3, width, height / 3); } }
:
Checkbox Checkbox . Checkbox , . getState setState. Checkbox, ./* */ import java.awt.*; import java.applet.*; public class CheckboxDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); Checkbox win95 = new Checkbox("Windows 95/98", null, true); Checkbox Solaris = new Checkbox("Solaris 2.5"); Checkbox mac = new Checkbox("MacOS 7.5"); Java
Rendered by www.RenderX.com
. 17 38
add(win95); add(solaris); add(mac); win95.setBounds(0, 0, width, height / 3); Solaris.setBounds(0, height / 3, width, height / 3); mac.setBounds(0, 2 * height / 3, width, height / 3); } }
:
CheckboxGroup Checkbox ( null) Checkbox. CheckboxGroup, Checkbox, ( ). , , Checkbox - getCheckboxGroup setCheckboxGroup. getCurrent setCurrent Checkbox. , , ./* */ import java.awt.*; import java.applet.*; public class CheckboxGroupDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); CheckboxGroup g = new CheckboxGroup(); Checkbox win95 = new Checkbox("Windows 95/98", g, true); Checkbox solaris = new Checkbox("Solaris 2.5", g, false); Checkbox mac = new Checkbox("MacOS 7.5", g, false); add(win95); add(solaris); add(mac); win95.setBounds(0, 0, width, height / 3); solaris.setBounds(0, height / 3, width, height / 3); Java
Rendered by www.RenderX.com
. 18 38
mac.setBounds(0, 2 * height / 3, width, height / 3); } }
- , , :
Choice Choice () ( ComboBox Windows). Choice , , , , . - , , . , Choice. countItems . , , select, ( ), , . , getSelectedItem getSelectedIndex , , - . , Choice./* */ import java.awt.*; import java.applet.*; public class ChoiceDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); Choice os = new Choice(); Choice browser = new Choice(); os.addItem("Windows 95/98"); os.addItem("Solaris 2.5"); os.addItem("MacOS 7.5"); browser.addItem("Netscape Navigator 3.0"); browser.addItem("Netscape Communicator 4.5"); browser.addItem("Internet Explorer 3.0"); browser.addItem("Mosaic 3.0"); browser.addItem("Lynx 2.4"); browser.select("Netscape Communicator 4.5"); add(os); Java
Rendered by www.RenderX.com
. 19 38
add(browser); os.setBounds(0, 0, width, height / 2); browser.setBounds(0, height / 2, width, height / 2); } }
:
List List ( ListBox Windows). , , ./* */ import java.awt.*; import java.applet.*; public class ListDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); List os = new List(0, true); List browser = new List(0, false); os.addItem("Windows 95/98"); os.addItem("Solaris 2.5"); os.addItem("MacOS 7.5"); browser.addItem("Netscape Navigator 3.0"); browser.addItem("Netscape Communicator 4.5"); browser.addItem("lnternet Explorer 4.0"); browser.addItem("Mosaic 3.0"); browser.addItem("Lynx 2.4"); browser.select(1); add(os); add(browser); os.setBounds(0, 0, width, height / 2); browser.setBounds(0, height / 2, width, height / 2); } }
Java
Rendered by www.RenderX.com
. 20 38
, , :
Scrollbar Scrollbar ( ) . , , . , , , . , , . Scrollbar - VERTICAL HORIZONTAL. , , , . getValue setValue. , getMinimum getMaximum, . , , ./* */ import java.awt.*; import java.applet.*; public class ScrollbarDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer. parseInt(getParameter("height")); Scrollbar hs = new Scrollbar(Scrollbar.HORIZONTAL, 50, width / 10, 0, 100); Scrollbar vs = new Scrollbar(Scrollbar.VERTICAL, 50, height / 2, 0, 100); add(hs); add(vs); int thickness = 16; hs.setBounds(0, height - thickness, width - thickness, thickness); vs.setBounds(width - thickness, 0, thickness, height - thickness); } }
Java
Rendered by www.RenderX.com
. 21 38
, , :
TextField TextField . . "" TextField setEditable, isEditable , . getText setText. select , , . selectAll. setEchoChar , . , TextField , echoCharIsSet, , -, getEchoChar. , ./* */ import java.awt.*; import java.applet.*; public class TextFieldDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); Label namep = new Label("Name : ", Label.RIGHT); Label passp = new Label("Password : ", Label.RIGHT); TextField name = new TextField(8); TextField pass = new TextField(8); pass.setEchoChar('*'); add(namep); add(name); add(passp); add(pass); int space = 25; int w1 = width / 3; namep.setBounds(0, (height - space) / 2, w1, space); name.setBounds(w1, (height - space) / 2, w1, space); passp.setBounds(0, (height + space) / 2, w1, space); pass.setBounds(w1, (height + space) / 2, w1, space);
Java
Rendered by www.RenderX.com
. 22 38
} }
:
TextArea . AWT , TextArea. TextArea String . , , . , TextArea: appendText String ; insertText ; rplaceText - , , -. , TextArea ./* */ import java.awt.*; import java.applet.*; public class TextAreaDemo extends Applet { public void init() { setLayout(null); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); String val = "Java, whose original name\n"+ "was Oak, was developed as a part\n"+ "of the Green project at Sun.\n"; System.out.println(val); TextArea text = new TextArea(val, 80, 40); add(text); text.setBounds(0, 0, width, height); } }
:
Java
Rendered by www.RenderX.com
. 23 38
5. Layout , , "". setLayout(null). . AWT (layout managers). LayoutManager , LayoutManager, , String. , Panel, . Panel, minimumLayoutSize preferredLayoutSize. , , preferredSize minimumSize. , . , . Java - , . FlowLayout FlowLayout , , , . , . , , . FlowLayout. , , LEFT, RIGHT CENTER . - CENTER, - 5 . , Panel Label. Panel FlowLayout RIGHT./* */ import java.awt.*; import java.applet.*; import java.util.*; public class FlowLayoutDemo extends Applet { public void init() {
Java
Rendered by www.RenderX.com
. 24 38
setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 3)); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); String val = "Data is not information " + "is not knowledge is not wisdom."; StringTokenizer st = new StringTokenizer(val); while (st.hasMoreTokens()) { add(new Button(st.nextToken())); } } }
( 200 250 ) , , Label , :
BorderLayout BorderLayout , , , , . -: String.North, String.South, String.East String.West , a Center - . BorderLayout ./* */ import java.awt.*; import java.applet.*; import java.util.*; public class BorderLayoutDemo extends Applet { public void init() { setLayout(new BorderLayout()); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); add("North", new Button("This is across the top")); add("South", new Label("The footer message might go here")); add("East", new Button("Left")); add("West", new Button("Right")); String msg = "The origins of Java go back to 1990,\n"+ "when the World Wide Web was\n"+ Java
Rendered by www.RenderX.com
. 25 38
"barely a glimmer in a British\n"+ "programmer's eye.\n\n"+ "-- The Java Saga, by David Bank"; add("Center", new TextArea(msg)); } }
:
GridLayout GridLayout . . , GridLayout 44, 15 16 , . , , , - ""./* */ import java.awt.*; import java.applet.*; public class GridLayoutDemo extends Applet { static final int n = 4; public void init() { setLayout(new GridLayout(n, n)); setFont(new Font("Helvetica", Font.BOLD, 24)); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int k = i * n + j; if (k > 0) add(new Button("" + k)); } } } }
Java
Rendered by www.RenderX.com
. 26 38
- :
, ( , "" Windows), AWT- , Windows . Insets Insets , Panel , , . , Panel, Insets , Insets , , , .public Insets insets() { return new Insets(10, 10, 10, 10); }
CardLayout CardLayout . , , . , . , , .
6. Window Window Panel , . Window, Frame. Frame Frame - , . Frame , . / Frame, show hide. , Frame TextArea./* Java
Rendered by www.RenderX.com
. 27 38
*/ import java.awt.*; import java.applet.*; public class FrameDemo extends Applet { public void init() { int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); Frame f = new Frame("Demo Frame"); f.setSize(width, height); f.setLayout(new FlowLayout(FlowLayout.LEFT)); f.add(new Label(" ...")); f.add(new Label(" , ")); f.add(new Label(" , - .")); f.add(new Label("- (George Gilder), Forbes ASAP")); f.show(); } }
:
7. . MenuBar Menu. , , - MenuItem. Menu - MenuItem, Menu , . , ./* */ import java.awt.*; import java.applet. *; Java
Rendered by www.RenderX.com
. 28 38
public class MenuDemo extends Applet { public void init() { int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); Frame f = new Frame("Demo Frame"); f.setSize(width, height); MenuBar mbar = new MenuBar(); f.setMenuBar(mbar); Menu file = new Menu("File"); file.add(new MenuItem("New... ")); file.add(new MenuItem("0pen...")); file.add(new MenuItem("Close")); file.add(new MenuItem("-")); file.add(new MenuItem("Quit...")); mbar.add(file); Menu edit = new Menu("Edit"); edit.add(new MenuItem("Cut")); edit.add(new MenuItem("Copy")); edit.add(new Menultem("Paste")); edit.add(new MenuItem("-")); Menu sub = new Menu("Special"); sub.add(new MenuItem("First")); sub.add(new MenuItem("Second")); sub.add(new MenuItem("Third")); edit.add(sub); edit.add(new CheckBoxMenuItem("Debug")); edit.add(new CheckBoxMenuItem("Testing")); mbar.add(edit); f.show(); } }
:
8. AWT , , (callback). GUI- , Java
Rendered by www.RenderX.com
. 29 38
( , ..). C++, ( , ). Java ( ). , . GUI-, . , GUI- , . Java AWT, JavaBeans API. Java. java.util.EventObject. AWT, , java.awt.AWTEvent. AWT (, MouseEvent tionEvent) java.awt.event. , getSource(), AWT , getID(). , , . , FocusEvent : FocusEvent.FOCUS_GAINED FocusEvent.FOCUS_LOST. , . , MouseEvent getX(), getY() getClickCount (). , , getModifiers() getWhen(). Java . , . , ( ), , , , , . ( , ), , . ( EventObject). , . , . , ActionEvent ActionListener. java.awt.event (, MouseEvent : MouseListener MouseMotionListener). java.util.EventListener. , -, . . , , MouseEvent, , , Java
Rendered by www.RenderX.com
. 30 38
. . , , , . , . 6 java.awt.event , , , . 1. , Java ActionEvent AdjustmentEvent ComponentEvent ActionListener AdjustmentListener ComponentListener actionPerformed() adjustmentValueChanged() componentHidden() componentMoved() componentResized() componentShown() componentAdded() componentRemoved() focusGained() focusLost () itemStateChanged() keyPressed() keyReleased() keyTyped() mouseClicked() mouseEntered() mouseExited() mousePressed() mouseReleased() mouseDragged() mouseMoved() textValueChanged() windowActivated() windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowlconified() windowOpened()
ContainerEvent FocusEvent ItemEvent KeyEvent
ContainerListener FocusListener ItemListener KeyListener
MouseEvent
MouseListener
MouseMotionEvent TextEvent WindowEvent
MouseMotionListener TextListener WindowListener
, , java.awt.event -, . , -, . , , , . , , , Listener Adapter: MouseAdapter, WindowAdapter .. -, , . Java
Rendered by www.RenderX.com
. 31 38
. AWT - . : X, addXListener () removeXListener() . Java , . , . , API Button , ActionEvent. 7 AWT , . 2. AWT Java1.1 Button CheckBox CheckBoxMenuItem Choice Component ActionEvent ItemEvent ItemEvent ItemEvent ComponentEvent FocusEvent KeyEvent MouseEvent , , , , , , , ,
Container List
ContainerEvent ActionEvent ItemEvent
MenuItem Scrollbar TextComponent TextField Window
ActionEvent AdjustmentEvent TextEvent ActionEvent WindowEvent
Java
Rendered by www.RenderX.com
. 32 38
8.1. "" Java , Java. MouseListener MouseMotionListener, addMouseListener() addMouseMotionListener()./* * */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble2 extends Applet implements MouseListener, MouseMotionListener { private int last_x, last_y; public void init() { // , // MouseListener MouseMotionListener // , . // , // . this.addMouseListener(this) ; this.addMouseMotionListener(this); } // MouseListener. // . public void mousePressed(MouseEvent e) { last_x = e.getX(); last_y = e.getY(); } // MouseMotionListener. // . public void mouseDragged(MouseEvent e) { Graphics g = this.getGraphics(); int x = e.getX(), y = e.getY(); g.drawLine(last_x, last_y, x, y); last_x = x; last_y = y; } // , MouseListener. public void mouseReleased(MouseEvent e) {;} public void mouseClicked(MouseEvent e) {;} public void mouseEntered(MouseEvent e) {;} public void mouseExited(MouseEvent e) {;}
Java
Rendered by www.RenderX.com
""
. 33 38
// MouseMotionListener. public void mouseMoved(MouseEvent e) {;} }
- :
8.2. "" Java , Java: . , , . . , - Clear. ActionListener, ./* * */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble3 extends Applet { int last_x, last_y; public void init() { // , MouseListener. this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { last_x = e.getX(); last_y = e.getY(); } }); // , MouseMotionListener. this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Graphics g = getGraphics(); int x = e.getX(), y= e.getY(); g.setColor(Color.black); g.drawLine(last_x, last_y, x, y); last_x = x; last_y = y; }
Java
Rendered by www.RenderX.com
. 34 38
}); // Clear. Button b = new Button("Clear"); // , // , . b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Graphics g = getGraphics(); g.setColor(getBackground()); g.fillRect(0, 0, getSize().width, getSize().height); } }); // . this.add(b); } }
:
, 3 : Scribble3$1,2,3.
9. (GUI) Java, AWT. , , HTML-. -, , . , , . -, , , main. , Java Graphics, , . AWT, Component . Container, . Component Java
Rendered by www.RenderX.com
. 35 38
( . controls), Container . (Layout managers). Container Window, . Dialog Frame. Dialog FileDialog. MenuComponent . , , , , . 11 GUI-.
10. 11-1. Applet? a.) java.awt.Panel 11-2. init ? start? a.) init . start , . 11-3. paint, update, repaint? a.) paint , update (background) , paint repaint , update 11-4. Color, ? a.) new Color(0,0,255). Color.blue 11-5. Java ? a.) , ( ), (, , ). 11-6. FontMetrics? a.) , , , Java
Rendered by www.RenderX.com
. 36 38
. .
FontMetrics , , . 11-7. -, 1010. a.) public class SquareComponent extends Canvas {public void paint(Graphics g) { g.drawRect(getWidth()-5, getHeight()-5, 10, 10); } }
11-8. AWT (check-box)? - (radio-button)? a.) Checkbox:Checkbox chbox = new Checkbox();
- Checkbox CheckboxGroup:CheckboxGroup group = new CheckboxGroup(); Checkbox rb1 = new Checkbox(" 1",group, true); Checkbox rb2 = new Checkbox(" 2",group, false);
11-9. List Choice? a.) List , Choise . List ( multiselect), Choice . 11-10. ? ? a.) . , , , . : constraints, ..) (
Java
Rendered by www.RenderX.com
. 37 38
11-11. Dialog Frame? a.) , Frame , Dialog Frame. Dialog . Frame . Dialog . 11-12. , , ? a.) . -, Listener-, . , addListener, . 11-13. , ? a.) , addListener .
Java
Rendered by www.RenderX.com
Java ()
Java
Rendered by www.RenderX.com