Swing Book

337

Transcript of Swing Book

Swing Java Foundation Classes

http://all-ebooks.com

- -- 2005

2

Java . - , , . Java , : J2ME (Java 2 Micro Edition) , J2EE (Java 2 Enterprise Edition) , J2SE (Java 2 Standard Edition) Java , - Java . J2SE , , , , J2EE, , HTML-, JDBC .. . , , , , . , , , , . ; , , . , Java 2 ( JDK 1.2), , Java . , , Java, , . , Java . , , . Java , . , , , , , . , , , .

Java Foundation Classes J2SE Java Foundation Classes (JFC). . , Java Foundation Classes. Swing. JFC; , , . Swing, , , . , . , . Swing .

3

Java2D. JFC , , , , , . Java2D Swing . Accessibility. , . Accessibility , , , . , Swing Accessibility , , . Drag'n'Drop. , Java- (drag and drop). , Java . Java Foundation Classes Swing . Swing, . Swing, , . .

. Swing . , , Swing. , . 1. . Swing , . , Swing AWT, , Swing , JavaBeans , . Swing, . 2. . , Swing . , . , , , EventDispatchThread EventQueue. Swing; , - , , Swing. 3. Swing. Swing , JComponent, , . , , , . Swing, . 4. . . Swing, ,

4

; . , . 5. . , , . , . , , . 6. . , , : , , , . , , JLabel, . 7. . , . Swing , , . 8. . , . , , , . Swing . . JToolBar, Swing . 9. . (JList) (JComboBox) Swing. JList , , , , , . JComboBox ( ), , . Swing, , . Swing . 10. . , ( ) . JSLider . JProgressBar , . , JDK 1.4, JSpinner, , . , Swing . 11. . , . JTabbedPane, JSplitPane JScrollPane. , , , . , , ( ) . 12. . , . , Swing.

5

JOptionPane, , . JFileChooser JColorChooser . 13. . , . Swing, JTree, , . , Swing . Swing. 14. . Swing javax.swing.text, . , , . . , , Swing: Document . 15. . Swing JTable. JTable , , , , . , , , , , . , . Swing, . , . , Swing, , , .

. , Java, , AWT, Swing (Rapid Application Development, RAD), Java, , , . , - , . Swing, - .

, JDK Swing ( , , , ). Swing, , , , Swing. Swing, , . Swing

6

( JavaBeans, 1). , Swing, .

, , Swing (design patterns), ( ) - . , , . Swing , , . , Swing, . , Swing , .

, Swing . : , - , , . , , . : , , Swing. , . com.porty.swing. JAR (booktools.jar) , . . www.IPSoftware.ru.

, , , www.IPSoftware.ru. , , , Swing, Swing Java Foundation Classes.

, , [email protected] ( , ). ! http://www.piter.com.

7

1 Swing, Java Foundation Classes, . , , , . , Swing, . , , Swing. , Swing? , , . , Swing, . , , , Swing . , - , , , . Swing , , , . , Swing , . () , . Swing, , , . , Swing : , , . , , . , Java . , , . . . Swing Java . AWT, . , Java (desktop applications). Java , , Java , , , Swing, . , Swing, , AWT Swing . , Swing . Swing AWT , Swing . Java (lightweight), Swing. , ( , , ). , , Java. Swing, . Swing , , , , . Swing MVC (. ). Swing ,

8

. .

... AWT Swing, , , AWT (Abstract Window Toolkit ). Swing, Java 1.1 Java 2, java.awt Java . . AWT , , . AWT Java- , . , . AWT , 1 (peer), . AWT , , 2. . 1.1 AWT. (Unified Modeling Language, UML): , , .

1.1. AWT , Component, AWT. ( ) ( ). AWT , Container ( ). , ( , , ). AWT , (layout manager) 3. , , , , , , . Frame, Dialog Applet. AWT, , (Button) (Label), , AWT. , , . , peer , , AWT : - Java, . - ( ) . 2 , AWT, . , (bridge) , ; , . , . 3 , , Java, 5, .1

9

. ( , Peer, ButtonPeer). AWT , (, JDK Windows WButtonPeer, ButtonPeer). , Java. AWT . . . , AWT . AWT , handleEvent() Component, , ( ) switch ( if), . , , Windows, . , Windows , Java, - , . Sun . . AWT , , AWT , . - AWT , . , , . AWT, , : . , , AWT , (builders) (Graphical User Interface, GUI). Java, . AWT . , , . , , JDK 1.0 , . - AWT- . , AWT, , . Java , . ( ), , Java- .

Java- (JDK 1.0) . - , , . , . , Unicode , . , .

10

Java . web- AWT, . AWT, , Java. , Java- : . , . , , Java . , , , , , Java . Java , , , , Java . . Sun Java (1.1). Java AWT . , AWT . , Java . , . , Java AWT? . . AWT , . , Component ( AWT), , , . , , , . Java , - . - , Java. . , , Java-. . , - . AWT Java. , (heavyweight), . , , . , - , . ( ), . , . AWT JavaBeans. , Java , . (, , Java-), , . JavaBeans , Java,

11

4. JavaBeans , - . AWT . Swing JavaBeans, . Java Java- . . . , Component Container, , . , AWT , . ? , . AWT , , . , , Java- - , . , , ( Swing ). , , , , , . , ( ), , AWT . , , , Java . , , . , , , , , . , Java, . AWT (Button) (Window) Java, ( ). , . , . Java JDK 1.1 , Sun , . Sun , JDK 1.1. (, Java AWT, . , .) ( ) , Java . Java Foundation Classes.

4 ActiveX, , .

12

Sun , Java-, . JavaBeans . , . , Borland Symantec, Microsoft . (Rapid Application Development, RAD), JavaBeans . , . , Java Sun. JDK Java, Sun Sun, , , JDK, Java-. JDK 1.1, - , Swing. , , . Java- . , Swing. , , AWT, JavaBeans. Swing .

Swing AWT , Swing AWT. AWT Java ( ), . AWT , . , AWT ( Button, Label ). , Java- . .

1.2. Swing AWT, , . AWT : Component ( ) Component, Container ( , ). , 1.2, , Swing.

13

, Swing , JComponent, Container, AWT. , AWT Swing, , , Java- . Swing AWT , AWT . AWT Swing, J (, Button Swing JButton). Swing AWT 5, AWT Swing ( javax.swing J). . AWT , Swing JComponent Container. Swing : , JComponent, , JContainer? , , , , (, ). , , Swing, . , Java, ( , ). , (, , ), , , , , . ( , , , 6, ). ( ) , , - . , JComponent, , , Swing, ( ). Swing JComponent, . Swing , AWT, . , JComponent , Swing, . ( , AWT.) JComponent (tooltips), (borders), (accessibility), (keyboard actions) . . , , JComponent Swing , . , 5 , Swing AWT . AWT AWT Swing, Swing AWT ( Swing AWT, Swing ). 6 (damaged area) , , .

14

, , . 3 Swing (, ). Swing JComponent. , , , . AWT Frame Dialog, Applet. Swing , ( Swing, , AWT ). Swing , : JWindow, JDialog JFrame, JApplet. Swing, (root pane) Swing ( 4). , , , Swing AWT, . Swing AWT, , . Component Container, Swing , Java . Swing, , AWT.

AWT Swing Java 2, Java AWT ( ), Swing. . AWT Swing , ( , ) Component Container. , . Swing, AWT, . AWT ( ). AWT ( AWT Swing ). Swing AWT. , . . , , . , ( ), . , . ( ), , . . , , . , , (, z-order), , . , , .

15

( Swing) ( AWT) , .

, , ( , ), . , Swing AWT , . . , Swing , , ( ), ( ). Swing, (JInternalFrame). : (Multi-Document Interface, MDI) , , . - ( Swing ), . (Swing , AWT). , , ( JScrollPane JInternalFrame) .

, Swing , , AWT . , . , (, ).

JavaBeans , , . : , , . - . . . ( ), (, ) , . - , . : , SOAP (Simple Object Access Protocol), UDDI (Universal Description, Discovery, and Integration) WSDL (Web Services Description Language), (web-) ( , ). , (, . .). , , , . Visual Basic (VB). Windows , VB . ,

16

. VB , ActiveX, VB ( , ). (RAD) , Delphi. , . , Delphi , (, ). , . (properties) . (events) , , . , Delphi, , , , . Java AWT, , , , . , AWT, Java . JavaBeans ( Delphi). , , Java, , , . .

, - , . : ( , ) (type library), , . , RAD , . Java , , -, Java. , ( 7 Java.lang.reflection). JavaBeans Java , . , . , ( , ), . , , 8. , .

7 , . , , , , . , ( ). , . 8 JavaBeans - (design patterns), .

17

1. getXxx() setXxx(). , , color (), ( Color):private Color color;

:public Color getColor() { /* ... */ } public void setColor(Color c) { /* ... */ }

, color, , , . (getXxx()) . , Sun, get set . 2. - (boolean), . , :private boolean enabled:

:public boolean isEnabled() { / * . . . * / } public void setEnabled(boolean b) { / * . . . * / }

To , , get is. , . 3. , , - , (public). 4. , , , ( ). , 2. 5. (public) . , , . , . , , JavaBeans. , . , . , , , , , . JavaBeans . , . , , Java

18

. , JDK 1.1 JavaBeans . , Java , . Java JavaBeans : , JSP (Java Server Pages) , JavaBeans. , JavaBeans , . , , , , , . , ( Swing ).

Java Beans . , , . , , JavaBeans, Java. (bound) , . Java.beans , . , PropertyChangeEvent, , . . , , , . , , , . (constrained) . , VetoableChangeListener. , , , , PropertyVetoException. , . , , , (). JavaBeans (indexed) . , set/get , , . :public Color[] getColors(); public void setColors(Color[] ); public Color getColors(int index); public void setColors(int index, Color c);

19

colors, get/set. , . , . , . ( , , ). , , , - . JavaBeans (property editors). , , . PropertyEditorSupport, . PropertyEditorManager Java.beans. registerEditor() . , , ( , , ). , , . JavaBeans , (bean info), . , ( ), , , . , : BeanInfo. Beanlnfo, SimpleBeanInfo ( BeanInfo ). BeanInfo, . , , , . , . : , . , , ( ). (persistence). , , ( ), . JavaBeans Java (serialization). , , Serializable, ( ). Serializable , . , (- , , ), , . JDK 1.4 . XML. , , ,

20

. , - , . XML (Extension Markup Language ) , , JavaBeans . XMLEncoder XMLDecoder Java.beans.

Swing JavaBeans, , Java JavaBeans, , Swing . Swing, , . Swing get/set. JavaBeans. ( , XML). Swing JavaBeans , ( , ). ( ), , , ( , JInternalFrame). ( , ). Swing , . , JavaBeans , . , Swing JavaBeans, , , RAD, Swing. , JavaBeans . Swing , . , , (JTextField) (text), , (selected text). getText() getSelectedText(). , JTextField. , , : setText() setSelectedText(). , Swing . , , , , .

AWT, , . , . Java ( , ). , . Java Java- , , , . Java- ( AWT- Mac, Windows, , Mac, Windows ). , , . , AWT , , . ,

21

Java-. , . - . , , , . Swing , . . , , , - . , , , : , , .. . , , ( , ). . , , , , . , Windows- . Unix, Mac, . Swing . Java . Java - , . - Swing , , , . , . , . Swing , . , .

MVC ( ) 1980 Smalltalk, Smalltalk-80. , . (Model/View/Controller, MVC). , Smalltalk , , . Macintosh, Macintosh. MVC , - . , MVC . (model) , , . , ( ). , , . , : ( , , . .), . , . (view) . . , HTML (Hypertext Markup Language

22

) : , , , HTML- , . ( HTML). , , , . , . (controller) , . MVC . HTML-, , . , , ( , HTML-), , , , ( , ). : , , . MVC 1.3.

1.3. , , ( , , . .); . , , / 9. : , 10 . ( ), , . , . , , . : ( ), , . - MVC, , . , , ( ), . , ( ) , , , .9 , , , . , MVC - . 10 , MVC, , (observer), , - ( - ). 2, Swing, .

23

, ( ), MVC. . , , , , , , . , ( ) . MVC : , . , MVC , . Smalltalk MVC . , , , . : , , , . , Swing , . , MVC : , . .

MVC? : , ! , MVC , , , . , . , , , . . , : ( , . .) . , . - , , . , , , . , ( ?). . : , . , . : , : , , , . , . , MVC , . ( , , ), , . , - (look and feel) . . Java,

24

: , , - ( ), . , Java, , . MVC, . , . , , , .

Swing , , Java MVC : , . , . Swing. , ( 1.4).

1.4. , Swing , (delegate) (User Interface, UI). , , , . , ( , , / ), , - ( , ), , . ( ) . , . , , , . , , , . , , , , ( ). : , ( - , ). , . - ( , ) .

25

, , . , (UI- ) Swing. Swing - , , , , Java- . UI- . , UI-, , - , . , MVC, Swing : ( UI- ) ( , , ). , Swing MVC ( , ), Swing - (model-delegate), (separable model architecture).

, , Swing . UI-, ; , . Swing, (JButton) (JList). . UI-? , , ( ). 1.5 , UI-, ( - Swing). , -, , UI . , UI- , , , , . , . . UI- . , . . , , , UI- . ( ) . , UI- ( UIManager, ), 11. , ( JButton JTable) Swing. , : , , . : UI- , . : , . , . Swing ( , . .) , JavaBeans, .

11

, , UI- Swing (mediators), . - Swing (facade) - ( , , ).

26

1.5. UI-

Swing UI-, . , Java- (, , ). UI- , , . Swing UIManager. . setLookAndFeel() LookAndFeel. LookAndFeel , UI-, , , UIManager. UI- UIManager. Swing, JDK 1.4 ( JDK 1.3), 1.1. 1.1. Swing Java ( Metal, , )

javax.swing.plaf.metal

Swing ( ). Swing, Java . . Windows-. Windows. JDK 1.4.2 Windows XP, Windows XP Java Unix- CDE/Motif. ( Unix)

MetalLookAndFeel

Windows

com.sun.java.swing.plaf.windows

WindowsLookAndFeel

Unix

com.sun.java.swing.plaf.motif

MotifLookAndFeel

, JDK, Sun Macintosh (Mac Look & Feel). Mac , , java.sun.com. Mac, Windows, ( Apple Microsoft). , , , UIManager getSystemLookAndFeel(). Windows Windows, Unix Unix. ,

27

. , , SwingUtilities, . , , Metal, Motif. , Swing : , , , . , . : , ? , Java-, . , . : Windows, Microsoft, Unix, Windows ( Microsoft Unix , ). Metal, Java- ( , Java , ). Sun , - . 5, . Metal, . , , , , , . , Swing ( , Swing , , JD, , ). Java , Swing. , Sun. , , . Metal, Java ( , ). , - Metal, Metal, , . . www.jars.com, Swing, Alloy, , Swing. Metal, Alloy , . , Metal Kunststoff. , (Pluggable Look And Feel, PLAF) Swing. . , , , (, , ). ,

28

. .

, - ( ) ( , ). , , . , . , , Swing, , . Swing . ( , ) , Accessibility ( Java Foundation Classes). , Swing , Accessible, , (JButton) AccessibleJButton. AccessibleXXX Swing ( , ; , , , ). , ( getAccessibleContext(), Swing - Component) (, ). Swing: , ( ), , , , . , , . , , , , . Swing, , ( , , - , ), Unicode, , Swing . , Swing , . java.sun.com, , Accessibility.

, , Swing AWT. Swing , , Java. JavaBeans Swing , . , Swing .

29

2 (GUI) , (event-driven systems). , : , . , . , Swing, . , Swing, , . . , . AWT JDK . , , , , , AWT . AWT , , : , , if switch, . , . AWT. , , Java. Swing JavaBeans, . , , , . Swing , , , , , , . , . Swing, , . , Java , , (callbacks). , , . Java , , , Java . , , .

, . - , , - . , , . , , , . ( ) ( ), . , . . , .

30

- , , , . , . , . , . , , . , , . , . , , , (subject). , , (observer). ( ), . : , , ( 2.1).

2.1. , : add() ( , , ); remove() ; notify() , add(), . - , update(). , , update() , . , ( ), , , , . , , . , , ( ), . , . Java . , , . , , , , , , ( , ), - , .

31

, Swing . , , UI Swing . , UI- UI- ( ), . ( UI-), , . Swing. -. Swing ( JButton, JList . .), , . - , .

(event) ( , ), (, ). (event source) Swing , , . , . , (listener) . ( ), . . , , , , , . ( , ) . , . , .// FirstEvents.java // - import javax.swing.*; import java.awt.event.*; public class FirstEvents extends JFrame { FirstEvents() { super("FirstEvents"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // addKeyListener(new KeyL()); // setSize(200, 200); setVisible(true); } public static void main(String[] args) { new FirstEvents(); } } // class KeyL implements KeyListener { // public void keyTyped(KeyEvent k) { System.out.println(k); } // public void keyPressed(KeyEvent k) { System.out.println(k); } // public void keyReleased(KeyEvent k) { System.out.println(k); } }

32

, JFrame, setSize(), , ( setDefaultCloseOperation(), 4, ) . , . , . Java, , KeyListener, KeyL. , : keyPressed() keyReleased() , keyTyped() ( , 12). KeyEvent, ( , . .). , . . . addKeyListener(), KeyL. , , (, ). Swing. - , , , , , . , : , , ; , , , , , .

JavaBeans Swing JavaBeans, . 1 , Swing JavaBeans. , JavaBeans , , . get/set. . JavaBeans ; , . , , , , - . . . , Key () , . , , Window (); , XXX. , . 1. , ( , ). Java.util.EventObject XXXEvent, XXX . , KeyEvent.

12

keyTyped() , Java. , . : , ( , , Enter Esc).

33

2.

3.

, . , . : XXXListener KeyListener. Java.util.EventListener ( , , ). , XXXEvent. . , . , : , . : addXXXListener() , , removeXXXListener() , . , addKeyListener(), JFrame ( Component, ).

, , , . , , JavaBeans . , , , JavaBeans ( Swing ), , . , , . , , , WindowEvent. , Window/Listener addWindowListener(). , , , , , ! JavaBeans : , , , , .

, , , , , , , . , Java (low-level events) (high-level events). , : , , . . 13. Java- . . , . (fire). Swing fireXXX(); , . , (, , ). ( 2.1). , java.awt.Component (, ).13 AWTEvent.

34

2.1. KeyEvent. ,

keyPressed(KeyEvent), keyReleased(KeyEvent), keyTyped(KeyEvent) mouseClicked(MouseEvent), mouseEntered(MouseEvent), mouseExited(MouseEvent), mousePressed(MouseEvent), mouseReleased(MouseEvent) mouseDragged(MouseEvent), mouseMoved(MouseEvent) mouseWheelMoved(MouseWheelEvent) focusGained(FocusEvent), focusLost(FocusEvent) componentAdded(ContainerEvent), componentRemoved(ContainerEvent) windowActivated(WindowEvent), windowClosed(WindowEvent), windowClosing(WindowEvent), windowDeactivated(WindowEvent), windowDeiconified(WindowEvent), windowIconified(WindowEvent), windowOpened(WindowEvent)

( java.awt.Component)

.

MouseEvent MouseEvent, 14 MouseMotionListener

.

.

. MouseWheelEvent FocusEvent15

( java.awt.Container) ( java.awt.Window), , (JFrame) (JDialog)

. . ContainerEvent Window/Event , , .

.

, , . : , . , ( , ; , , ). , - , .// LowLevelEvents.java // import javax.swing.*; import java.awt.event.*; public class LowLevelEvents extends JFrame { LowLevelEvents() { super("LowLevelEvents"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // getContentPane().add(new JScrollPane(out = new JTextArea())); // JButton button = new JButton(" "); getContentPane().add(button, "South"); // OurListener ol = new OurListener(); button.addKeyListener(ol); button.addMouseListener(ol); button.addMouseMotionListener(ol); button.addFocusListener(ol); // setSize(400, 300); setVisible(true); } // JTextArea out; // - class OurListener implements MouseListener, KeyListener, MouseMotionListener, MouseWheelListener, FocusListener {14 JavaBeans. , MouseEvent MouseListener ( ), . 15 , JDK 1.4.

35

public void mouseClicked(MouseEvent e) { out.append(e.toString() + "\n"); } public void mousePressed(MouseEvent e) { out.append(e.toString() + "\n"); } public void mouseReleased(MouseEvent e) { out.append(e.toString() + "\n"); } public void mouseEntered(MouseEvent e) { out.append(e.toString() + "\n"); } public void mouseExited(MouseEvent e) { out.append(e.toString() + "\n"); } public void keyTyped(KeyEvent e) { out.append(e.toString() + "\n"); } public void keyPressed(KeyEvent e) { out.append(e.toString() + "\n"); } public void keyReleased(KeyEvent e) { out.append(e.toString() + "\n"); } public void mouseDragged(MouseEvent e) { out.append(e.toString() + "\n"); } public void mouseMoved(MouseEvent e) { out.append(e.toString() + "\n"); } public void focusGained(FocusEvent e) { out.append(e.toString() + "\n"); } public void focusLost(FocusEvent e) { out.append(e.toString() + "\n"); } public void mouseWheelMoved(MouseWheelEvent e) { out.append(e.toString() + "\n"); } } public static void main(String[] args) { new LowLevelEvents(); } }

, ( JScrollPane, 11), JButton, . . OurListener. , . , . , , , ( , , , ). , . , (, , , ). () . ( , , ), . , , . , , ( 2.2). , , , . 2.2. . PropertyChangeEvent. ChangeEvent. JavaBeans

propertyChange(PropertyChangeEvent) stateChanged(ChangeEvent)

JavaBeans ( Swing) Swing. UI Swing, - (, )

ActionEvent.

actionPerformed(ActionEvent)

36

PropertyChangeEvent ChangeEvent , , UI- . PropertyChangeEvent JavaBeans, , ( ). 1, Swing . , UI- . ChangeEvent , . , . ActionEvent. , , / . , , , - ( , , . .). 2.1 2.2 , , , . , .

, ( ) . , , , , . , , . .

, , . (, WindowListener). . , , . , . - (, ), . , Java , , . , , , . , , (adapters). , , , . , , . , , . .// Adapters.java // import javax.swing.*; import java.awt.event.*; public class Adapters extends JFrame { public Adapters() { super("Adapters");

37

// - setDefaultCloseOperation(EXIT_ON_CLOSE); // addMouseListener(new MouseL()); // setSize(200, 200); setVisible(true); } // class MouseL extends MouseAdapter { // public void mouseClicked(MouseEvent e) { System.out.println(e); } } public static void main(String[] args) { new Adapters(); } }

, MouseListener. , MouseL, MouseAdapter. ( , , , 2.1), . , , mouseClicked(). , . java.awt.event, , . , , , . : XXXListener, XXXAdapter 16. Swing , . , , - , . . . ( ), , , MouseL :MouseClicked(MouseEvent e)

, , . , , , . , . , .

, , . , . , . , - . , , , . Swing . , 16

, JavaBeans, . .

38

. , , . : , , , , . , , , : , . , , .

, , . : , , , ( , . .). , ( , , ), , , , . , .// InnerClassEvents.java // import javax.swing.*; import java.awt.*; import java.awt.event.*; public class InnerClassEvents extends JFrame { public InnerClassEvents() { super("InnerClassEvents"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // getContentPane().setLayout(new FlowLayout()); // getContentPane().add(text = new JTextField(10)); // getContentPane().add(button = new JButton("")); // button.addActionListener(new ButtonL()); // pack(); setVisible(true); } private JTextField text; private JButton button; // - class ButtonL implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(text.getText()); } } public static void main(String[] args) { new InnerClassEvents(); } }

: - (content pane) , ( 5). - , . ( ActionListener) text . , - , , InnerClassEvents

39

(public), , , . , , . ( Java 1.1 , JavaBeans.) , , , - . , , .

, , , : , - , . , , , . , (anonymous classes). , - , , ( . .) . , (, , ). . .// AnonymousClassEvents.java // import javax.swing.*; import java.awt.event.*; public class AnonymousClassEvents extends JFrame { public AnonymousClassEvents() { super("AnonymousClassEvents"); // // addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); // JButton button = new JButton(" "); getContentPane().add(button); // button.addActionListener(getButtonL()); // pack(); setVisible(true); } // public ActionListener getButtonL() { return new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("ActionListener"); }}; } public static void main(String[] args) { new AnonymousClassEvents(); } }

, . WindowEvent , WindowAdapter ( windowClosing()) . : , , . , . , : -, ,

40

, (final). . getButtonL(), ( ). : , , . , . , . , , . , - , . , . .

, . : . : Java . , . , , , . , . : , (factory); , , . , , , . , , , , . .// FactoryEvents.java // import javax.swing.*; import java.awt.event.*; public class FactoryEvents extends JFrame { // private ListenerFactory factory = new ListenerFactory(); public FactoryEvents() { super("FactoryEvents"); // addWindowListener(factory.getWindowL()); // JButton button = new JButton(" "); getContentPane().add(button); // button.addActionListener(factory.getButtonL()); // pack(); setVisible(true); } public static void main(String[] args) { new FactoryEvents(); } } // class ListenerFactory { // public ActionListener getButtonL() { return new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("ButtonListener"); }

41

}; } // public WindowListener getWindowL() { return new WindowL(); } class WindowL extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }

: , . ListenerFactory, . , , , . : , , ? Swing, , . , . , , , . , ( , ), , , . , .

, , , , : . , ( - ). , : ( , , ). (dispatching), (forwarding), . : , , (, ) ( , , , ). , .// ForwardingEvents.java // import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ForwardingEvents extends JFrame { public ForwardingEvents() { super("ForwardingEvents"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // getContentPane().setLayout(new FlowLayout()); // button1 = new JButton(""); button2 = new JButton(""); getContentPane().add(button1); getContentPane().add(button2);

42

// Forwarder forwarder = new Forwarder(); button1.addActionListener(forwarder); button2.addActionListener(forwarder); // pack(); setVisible(true); } JButton button1, button2; // - class Forwarder implements ActionListener { public void actionPerformed(ActionEvent e) { // if ( e.getSource() == button1 ) onOK(e); if ( e.getSource() == button2 ) onCancel(e); } } // "" public void onOK(ActionEvent e) { System.out.println("onOK()"); } // "" public void onCancel(ActionEvent e) { System.out.println("onCancel()"); } public static void main(String[] args) { new ForwardingEvents(); }

}

, . Forwarder, , ( ). : , , . Forwarder , , . , : , on(). : , Java , , . : , , , . : , , , Forwarder, - , . , , , , ; , - : . - , , . , , , , , . , . , ( , 17).

17 , . . , , , , . . - , .

43

Swing , : , . , , . , , , , . : , ( , ). , , . ( ). , , , removeXXXListener(), . . , Java . , Java : . , , . - , , , , . , , , , , . , Swing : . , , removeXXXListener(). , , , , . , Swing, ; , , ( ) . , , .

Swing , . , , . , , , . , - , ( , JComponent Swing, , Component AWT). , , - , - , ( , ) . , JavaBeans,

44

, . JavaBeans , . . JavaBeans . : , . ( ) , ( ) . , . JavaBeans, java.util.EventObject XXXEvent, XXX . :// com/porty/swing/event/ButtonPressEvent.java // SimpleButton package com.porty.swing.event; import java.util.EventObject; public class ButtonPressEvent extends EventObject { // . public ButtonPressEvent(Object source) { super(source); } }

, . , ; , , . , EventObject, getSource() . , JavaBeans , . , com.porty.swing.event, . : , . . - , . , JavaBeans, Java.util.EventListener. , , , . :// com/porty/swing/event/ButtonPressListener.java // package com.porty.swing.event; import java.util.EventListener; public interface ButtonPressListener extends EventListener { // void buttonPressed(ButtonPressEvent e); }

buttonPressed(), . ButtonPressEvent, , , . , , com.porty.swing.event. . ButtonPressListener, JavaBeans. addButtonPressListener() removeButtonPressListener(). , , .

45

. (unicast) . , . , , , . (multicast) . , . . , Swing ( AWT), , . , . , :// com/porty/swing/SimpleButton.java // package com.porty.swing; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import com.porty.swing.event.*; public class SimpleButton extends JComponent { // private ArrayList listenerList = new ArrayList(); // - private ButtonPressEvent event = new ButtonPressEvent(this); // - public SimpleButton() { addMouseListener(new PressL()); // setPreferredSize(new Dimension(100, 100)); } // public void addButtonPressListener(ButtonPressListener l) { listenerList.add(l); } // public void removeButtonPressListener(ButtonPressListener l) { listenerList.remove(l); } // public void paintComponent(Graphics g) { // g.setColor(Color.green); g.fillRect(0, 0, getWidth(), getHeight()); // g.setColor(Color.black); g.draw3DRect(0, 0, getWidth(), getHeight(), true); } // protected void fireButtonPressed() { Iterator i = listenerList.iterator(); while ( i.hasNext() ) ((ButtonPressListener)i.next()).buttonPressed(event); } // , class PressL extends MouseAdapter { // public void mousePressed(MouseEvent e) { // fireButtonPressed(); } } }

SimpleButton: , , , ButtonPressEvent. JComponent Swing, , ,

46

paintComponent() ( Swing 3). : , PressL, setPreferredSize() ( ). PressL , mousePressed(). , , fireButtonPressed(), . , fireXXX() ( fireXXXEvent()) , , JavaBeans. Swing . ButtonPressListener ArrayList. ArrayList : . : Iterator ( , , ) buttonPressed(), -. - , : , , , , . : , ( ArrayList ). : synchronized. fireButtonPressed() ( ). : Java.utilCollections synchronizedList(). ArrayList . , . :// SimpleButtonTest.java // import javax.swing.*; import com.porty.swing.*; import com.porty.swing.event.*; public class SimpleButtonTest extends JFrame { public SimpleButtonTest() { super("SimpleButtonTest"); setDefaultCloseOperation(EXIT_ON_CLOSE); // SimpleButton button = new SimpleButton(); // button.addButtonPressListener( new ButtonPressListener() { public void buttonPressed(ButtonPressEvent e) { System.out.println("1!"); }}); // button.addButtonPressListener(new ButtonL()); // JPanel contents = new JPanel(); contents.add(button); setContentPane(contents); // setSize(400, 300); setVisible(true); } class ButtonL implements ButtonPressListener { public void buttonPressed(ButtonPressEvent e) { System.out.println("2!"); } } public static void main(String[] args) { new SimpleButtonTest(); } }

47

, , SimpleButton ( JPanel). ButtonPressListener: , , . . , ( ), . JavaBeans: , , . , .

EventListenerList javax.swing.event, Swing, EventListenerList. , EventListener. , , Class. , EventListenerList Class EventListener. add(). ArrayList EventListenerList, :listenerList.add(ButtonPressListener.class, 1);

getListeners(), Class, . . ButtonPressListener :EventListener[] listeners = listenerList.getListeners(ButtonPressListener.class);

: ( EventListener) getListeners() ( ButtonPressListener) , , . getListeners() getListenersList(), Class EventListener . , getListenersList(), : , , , Class. getListeners() . Swing, , EventListenerList. ( , ), , ArrayList. , EventListenerList : ; . , , ( remove()) .

: Swing; , . . , , ,

48

, . Swing , . , Java, Java (Java Virtual Machine, JVM), . , . ( , , ) , Java-. , Java ( Swing).

EventDispatchThread EventQueue, Java-: main(). ( main) . , main() , - Java, java.awt.Component. . 1. Java, Java . 2. AWT, . 3. EventQueue, . 4. EventDispatchThread, EventQueue , , AWT. ( , AWT 1), . , ? EventQueue Java.awt, . , FIFO (FirstIn, First-Out , ), ( ). Java . : , , Java (, MouseEvent, ) EventQueue. EventQueue (singleton) 18; , , . , , . , , . , . , , , , . FIFO . : , , . EventDispatchThread. (pump) EventQueue ( pumpEvents()), , AWT ( dispatchEvent() EventQueue). EventDispatchThread , , , , .

18 EventQueue AWT, . AWT EventQueue ( ). .

49

EventDispatchThread Java , . , . , , , , EventDispatchThread, ( ). , . , , , main(), () . :public class ExitTest { public static void main(String[] args) { new javax.swing.JFrame().pack(); }

, , , JFrame, (), main() . Java , , ( ). EventDispatchThread. , : System.exit(). , EventDispatchThread , , , PaintEvent InvocationEvent. , : PaintEvent , , InvocationEvent EventDispatchThread , . EventDispatchThread , - , , , Swing. , , , AWT, ( Swing) . , , , , Swing . , ? , , . .

processXXXEvent(), EventDispatchThread , , , , . ( , , , ), EventDispatchThread dispatchEvent() EventQueue. , , ( ), dispatchEvent() . AWT dispatchEvent() java.awt.Component, , (final) . . dispatchEvent() , , PaintEvent paint() update() 19, , 19

EventDispatchThread PaintEvent , - , . Java- , - PaintEvent, , EventQueue PaintEvent,

50

, . , , , dispatchEvent() ( ) processEvent(). processEvent() . , processXXXEvent(), XXX , Focus Key. processXXXEvent() . , processKeyEvent() , , KeyListener (, , ). - ( ) . , . , 2.2.

2.2. AWT, . ? , ( ), . dispatchEvent() 20 , , , , . , , dispatchEvent() . java.awt.Container, , . - Swing , , . processXXXEvent() , . , . processXXXEvent(), . , :// PreProcessMouse.java // import javax.swing.*; import java.awt.event.*;

( , ), paint(). paint() . - PaintEvent: , , , paint(). 20 dispatchEvent() (final), - , dispatchEvent() dispatchEventImpl(), . dispatchEventImpl() , java.awt - .

51

public class PreProcessMouse extends JFrame { PreProcessMouse() { super("PreProcessMouse"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // addMouseListener(new MouseL()); // setSize(200, 200); setVisible(true); } // public void processMouseEvent(MouseEvent e) { if ( e.getClickCount() == 1 ) { // return; } // else super.processMouseEvent(e); } // class MouseL extends MouseAdapter { public void mouseClicked(MouseEvent e) { System.out.println("ClickCount: " + e.getClickCount()); } } public static void main(String[] args) { new PreProcessMouse(); } }

JFrame, . ( , Component), MouseL, MouseAdapter ( mouseClicked()). : processMouseEvent() JFrame. , , . , : processMouseEvent() , ( , ), , , ( , , ). , , , . , ( ) , MouseL, , , , . processXXXEvent() , , , , , . , , . , : , . , , , , . , , . , : Component Container, , , . , , : , . , , , , , , . , processXXXEvent(), : .

52

Swing , ; . 4. , (KeyEvent). : . , , , . , , , , , . , . dispatchEvent() Component . Swing, processKeyEvent() JComponent. , processKeyEvent() super.processKeyEvent(), , . 3 , .

, processEvent() processXXXEvent(), , : . , , , , - , , , . , , , - , , , , . . (event masking). , , (long) , , . , . , ( ) , MouseListener, ( ), . : , . , , . , ( ) . , , , processXXXEvent(), . processXXXEvent(): , processXXXEvent(), . , . , : , processXXXEvent() . enableEvents() disableEvents(). , - :

53

(protected), . AWTEvent, , . :// MaskingEvents.java // import java.awt.*; import javax.swing.*; public class MaskingEvents extends JFrame { public MaskingEvents() { super("MaskingEvents"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // disableEvents(AWTEvent.WINDOW_EVENT_MASK); // JPanel contents = new JPanel(); contents.add(new CustomButton("!")); setContentPane(contents); // setSize(400, 300); setVisible(true); } // class CustomButton extends JButton { public CustomButton(String label) { super(label); // disableEvents(AWTEvent.KEY_EVENT_MASK); } } public static void main(String[] args) { new MaskingEvents(); } }

JFrame , . setDefaultCloseOperation(), , ( processWindowEvent()), , . : ( disableEvents(), , JFrame) , , , , ( , , Ctrl+C). , ; , JButton Swing. , , , ( Enter ). , UI- , : , , . , , . , , , processXXXEvent(), , . , 4. (consume) . : processXXXEvent() , , - . . : , , . , ,

54

, . , . consume(), AWTEvent. , : consume() , ( AWT ). , ( , isConsumed()), . , Swing ( ) . .// ConsumingEvents.java // import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ConsumingEvents extends JFrame { public ConsumingEvents() { super("ConsumingEvents"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // if (e.getClickCount() == 1) e.consume(); System.out.println("1"); } }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // if ( !e.isConsumed() ) System.out.println("2"); } }); // setSize(300, 200); setVisible(true); } public static void main(String[] args) { new ConsumingEvents(); } }

, MouseListener. , (, ), , . , , , . , , , , , 21. , - , , , , ( ). , . Swing , Java, processXXXEvent(), . , processXXXEvent() . .

21 , , , . JDK , . , , , , , .

55

EventQueue : , , , , PaintEvent. , , , , EventQueue, , , . getSystemEventQueue() Toolkit, Toolkit getToolkit(), Component (, Toolkit , AWT). : , , , ( , , ). . , . :// UsingEventQueue.java // import java.awt.*; import java.awt.event.*; import javax.swing.*; public class UsingEventQueue extends JFrame { public UsingEventQueue() { super("UsingEventQueue"); // setDefaultCloseOperation(EXIT_ON_CLOSE); // JButton button = new JButton(" "); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // getToolkit().getSystemEventQueue().postEvent( new WindowEvent(UsingEventQueue.this, WindowEvent.WINDOW_CLOSING)); } }); // getContentPane().setLayout(new FlowLayout()); getContentPane().add(button); // setSize(400, 300); setVisible(true); } public static void main(String[] args) { new UsingEventQueue(); } }

, . ( FlowLayout) JButton . WindowEvent WINDOW_CLOSING, , . ( , , ) , postEvent(). , , , , . , , .

56

push() EventQueue. , EventQueue. dispatchEvent(), , , . , , , . 22.

EventDispatchThread, , EventDispatchThread , . , , EventQueue, Java. , , , . ? . , ? ? -, , , , main. , , main(). , -, . , , . ( ), . , , . , . , - (realized) . , , ( ). ( Swing), , ( ), . , : () ; setVisible(true) show() . , , , . , . : main, . ( , ) . processXXXEvent(), . Swing, . : , , , . Swing , , . : ? 22

AWTEventListener. Toolkit, , . AWTEventListener .

57

. , ? . -, , , . . : repaint() ; revalidate(), validate(), invalidate() . , ( ) - , , , . . -, . . , , , , . , , , . , , - , , , . . . invokeLater() invokeAndWait() EventQueue. Runnable, run() , . ( invokeLater() invokeAndWait() Runnable InvocationEvent, run().) : , , , , invokeLater() invokeAndWait(). :// InvokeLater.java // invokeLater() import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InvokeLater extends JFrame { public InvokeLater() { super("InvokeLater"); // - setDefaultCloseOperation(EXIT_ON_CLOSE); // button = new JButton(" "); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // new ComplexJobThread().start(); button.setText("..."); } }); // getContentPane().setLayout(new FlowLayout()); getContentPane().add(new JTextField(20)); getContentPane().add(button); setSize(300, 200); setVisible(true); } private JButton button; // , " " class ComplexJobThread extends Thread { public void run() { try { // sleep(3000); // ,

58

EventQueue.invokeLater(new Runnable() { public void run() { button.setText(" "); } }); } catch (Exception ex) { ex.printStackTrace(); } } } public static void main(String[] args) { new InvokeLater(); } }

, JButton . ActionListener, , , , , ( , , , , , ). , , ComplexJobThread, Thread. , , . , , : ( , Swing ). invokeLater() EventQueue. . , Runnable: run(), , . : , , , . , , ( - ). , . invokeLater() invokeAndWait(). , invokeLater() : , , , . , invokeLater() : , , , . invokeLater(), invokeAndWait() , . invokeAndWait(), : , , , , , , , . , . invokeLater() . Swing , EventQueue . , , , Swing, JTextArea JEditorPane, ( , ). , , . Swing: . : , , , EventDispatchThread. - , , , .

59

, . EventQueue.

, Swing . , Swing Java, , , , , .

Swing - . , , . , , . , , , , (). Swing, .

60

3 Swing Swing , , . Swing, . , Swing JComponent. , . , , Swing , , JComponent. , Swing, JComponent, . , , , , Swing ( ) . , , Swing. Swing, , ( JDK 1.4) . Swing , . .

AWT , Swing, , , Swing AWT ( Swing AWT). , JDK . AWT , Java, Java-. . . , , , , , . , , , , , . Java . - , AWT Java- ( Graphics, ) paint(), . paint() Java, Java.awt.Component. , , paint(), ( , ). :// AWTPainting.java // AWT

61

import java.awt.*; import java.awt.event.*; public class AWTPainting extends Frame { public AWTPainting() { super("AWTPainting"); // addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); setSize(200, 200); setVisible(true); } // public void paint(Graphics g) { // g.setColor(Color.red); g.fillRect(0, 0, 200, 200); } public static void main(String[] args) { new AWTPainting(); } }

. AWT ( Frame), , Canvas, , . , (Button) , , (peer) . , , AWT , . ( ), , (, , ). , . , , - , . AWT repaint(), , , . (AWT paint() 23). paint() , . repaint() , , , . , ( - ), repaint() . , , , , , ( paint()) . , , , . - , Component getGraphics(), Graphics, . , . getGraphics() , 23

, ( , AWT Swing, ), . repaint() AWT update(), Component. (incremental painting) repaint() , . , update() paint(). java.sun.com.

62

paint(). , , paint(), . . , getGraphics() , ( ), , , , , AWT . AWT . , , paint(), . repaint(). , , , getGraphics(). , .

AWT Java AWT , . , . , paint(), , AWT, , , . , AWT. AWT ( Frame Window, Dialog) paint(), , , . , , paint() AWT Graphics. ( : , , , ), , , , paint() ( : , , , ). paint(), . super.paint(), , . . , , , , . - , , ( -, ). . , , . . ( repaint()), . , , , , , .

63

: . , .// AWTLightweights.java // AWT import java.awt.*; import java.awt.event.*; public class AWTLightweights extends Frame { public AWTLightweights() { super("AWTLightweights"); // addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); // Lightweight1 lt1 = new Lightweight1(); Lightweight2 lt2 = new Lightweight2(); // , setLayout(null); lt1.setBounds(0, 0, 200, 200); lt2.setBounds(0, 0, 200, 200); add(lt2); add(lt1); // Button button = new Button("!"); button.setBounds(50, 50, 80, 30); add(button); // setSize(200, 200); setVisible(true); } // - class Lightweight1 extends Component { public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(20, 40, 100, 100); } } // - class Lightweight2 extends Component { public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(20, 30, 90, 90); } } public static void main(String[] args) { new AWTLightweights(); } }

, , AWTComponent, Button, . , , setLayout(null) ( 5), setBounds(). , , . , . , , , . , , , , , . . , , 24. . , , , ( Spy++ Windows), . . , Frame Button. , . Java .24

64

. ? , repaint(). , repaint() . repaint() , , . , ( , ). , , . , . , . paint(), repaint(), getGraphics(). AWT, .

Swing Swing , , , . . Swing, AWT , . , , , , . Swing , JComponent, , , . . , Swing (double buffering). Swing , , . , ( ) . ( , ) , , JDK AWT. , JDK, , Swing . JComponent Swing RepaintManager. Swing, JComponent. , , - . Swing , . RepaintManager. , Swing RepaintManager, RepaintManager.currentManager() 25. 25 RepaintManager , . , Swing

65

JComponent , , . , , , setDoubleBuffered(false). , ( , ) , - . , RepaintManager.setDoubleBufferingEnabled(false). , RepaintManager . , , ( , ), , . , RepaintManager. , , Swing, AWT , JComponent. , paint().

paint() , , , , ( ) , paint(). , Swing, , JComponent. . , , Swing, JComponent. paint() JComponent Swing, . . JComponent , , , 26. , , . , . , paint() JComponent . , RepaintManager.currentManager() RepaintManager, , , , . (clip rectangle), paint() ( AWT, , , repaint()) , . , , . , . , , ( Graphics). paint() , . , , , ANCESTOR_USING_BUFFER 27. , true, , Graphics, paint(), . , paint() , , paintDoubleBuffered(). paintDoubleBuffered() . JDK 1.4, (volatile images). , ( ) , . , , Swing. 26 : . 27 JComponent , , .

66

. , , . paintDoubleBuffered() , RepaintManager . , . , () . paintDoubleBuffered() , Graphics , paintWithOffScreenBuffer(). paintWithOffScreenBuffer() , . , JComponent . , .

paintComponent() paintComponent() , . paint(), AWT, , , , -. JComponent. , , , . , Swing MVC, , UI. , UI- paintComponent(), JComponent. : , UI- ( null), , update(). update() UI- : , , paint(), UI- ComponentUI. . : ? , . Swing , , , . , : , , . , . , , . (opaque), Swing. AWT , Swing . , , Swing , . true ( true), , . : , . - , setOpaque(false), . : . - Swing , . paintComponent(). : , UI-.

67

UI-, . UI- ComponentUI paint() 28, . . - , ( JComponent JPanel, ) paintComponent(), . , ( true) : super.paintComponent() 29.

paintBorder() paintBorder() Swing , (border). Swing , setBorder(). , paintBorder() JComponent. , paintComponent(), , - , , , Border paintBorder(). , : ? ? . . , , , . , 6, . paintBorder() . , - . , Border .

paintChildren() paintChildren(). , AWT , , paint(), paint() Container, . JComponent Swing Container -. Swing Container . AWT. paintChildren(). Swing : , isOptimizedDrawingEnabled. paintChildren() : - , . , , , . . : true, , , , , , . , : , , . isOptimizedDrawingEnabled . Swing : . paint(), ComponentUI, . , Swing , UI- . 29 , (JPanel), UI, . JComponent ( , , UI- ), , .28

68

JComponent : , isOptimizedDrawingEnabled(). isOptimizedDrawingEnabled true. . : , - , , , (, , , , , , ). , , - , . . , , . isOptimizedDrawingEnabled true, paintChildren() , , . : JLayeredPane, JDesktopPane JViewport. - .

paintComponent(), paintBorder() paintChildren() Swing . , , paintWithOffscreenBuffer(), ( , ). , , . paintDoubleBuffered() , paint() , . , ( 3.1).

3.1. , Swing , . , Swing, AWT, Swing , , JRootPane. , ,

69

JComponent, paint() , , , , , , . , ( , ). Swing .// SwingPainting.java // Swing import javax.swing.*; import java.awt.*; public class SwingPainting extends JFrame { public SwingPainting() { super("SwingPainting"); // setDefaultCloseOperation(EXIT_ON_CLOSE); // getContentPane().add(new SwingComponent()); // setSize(200, 200); setVisible(true); } // , Swing class SwingComponent extends JComponent { // public void paintComponent(Graphics g) { System.out.println("paintComponent"); super.paintComponent(g); g.setColor(Color.red); g.fillOval(10, 10, 50, 50); } // public void paintBorder(Graphics g) { System.out.println("paintBorder"); super.paintBorder(g); } // public void paintChildren(Graphics g) { System.out.println("paintChildren"); super.paintChildren(g); } } public static void main(String[] args) { new SwingPainting(); } }

, . , ( , 4) JComponent . , , . paintComponent(), , super.paintComponent(), . , . , , Swing.

repaint() RepaintManager, , Swing ( paint()), . , , . paint(). AWT repaint(). ,

70

paint() . . , Swing. , . , , , . , , , . Swing , . Swing RepaintManager. , repaint() , JComponent addDirtyRegion() RepaintManager, , , . . RepaintManager , , , , , . , RepaintManager Hashtable, . , , , . RepaintManager , SwingUtilities.computeUnion(), . , RepaintManager , . , , . AWT, Swing repaint() . , RepaintManager , , SystemEventQueueUtilities.queueComponentWorkRequest(), ( invokeLater()) paintDirtyRegions() RepaintManager. , , Swing. RepaintManager SystemEventQueueUtilities: , , , ( , ). , EventDispatchThread , RepaintManager , . Swing AWT. , Swing paintDirtyRegions(). , , collectDirtyRegions(). ( , true), , , , . , paintDirtyRegions() , , , . paintImmediately(), JComponent. paintImmediately() . , isOptimizedDrawingEnabled, , , . , , isDoubleBuffered() . , paintImmediately() , paint(). , paint() , paintImmediately() , .

71

, . paintImmediately() getGraphics() paintDoubleBuffered(), , paint(), . , . 3.2 .

3.2. , RepaintManager Swing. Swing, repaint(). , , , . DoubleBufferInfo, , , . getOffscreenBuffer(). JDK , RepaintManager .

, (validation), AWT, Swing. , , Swing RepaintManager. AWT , . , , (, , ) , . , , ( ). validate(). , , , , , . - ( ) invalidate(). , invalidate() - validate(), , . Swing , . , . ( Swing, , , , , , JRootPane),

72

, . , , . , , , (scrollbars). -, , , . validate() , invalidate() ( ). Swing . , JComponent isValidateRoot(), , , . , , true, , , , . , ( false), , , , , -. Swing (JScrollPane) (JRootPane) 30. isValidateRoot() true. , , , . , 4, , , , ( , ). , Swing 31. JComponent revalidate(). revalidate() , , , , invokeLater() , revalidate() . : revalidate() Container invalidate(), , addInvalidComponent() RepaintManager. , , isValidateRoot() true. , 32, ( ), . RepaintManager , , isValidateRoot() true. . RepaintManager , . RepaintManager : ( ), . paintDirtyRegions() validateInvalidComponents(), , . , , , . Swing , : - . , , revalidate(), , , .

, , JSplitPane. , Swing , - - ? Swing , , . , . 32 , ( ) , revalidate() . , Swing , Swing AWT validate().31

30

73

, , . . , . . Swing , DebugGraphics. , Swing. DebugGraphics Graphics. , , Graphics, a DebugGraphics ( ) 33. DebugGraphics , Graphics, . , , setDebugGraphicsOptions(). JComponent DebugGraphics, Swing , , setDebugGraphicsOptions(), . DebugGraphics. setDebugGraphicsOptions() , , DebugGraphics, , ( 3.1). 3.1. setDebugGraphicsOptions() DebugGraphicsNONE_OPTION LOG_OPTION

DebugGraphics ( ). System.out, DebugGraphics setLogStream() , , , . DebugGraphics: setFlashColor() , , setFlashCount() , , setFlashTime() , . , , DebugGraphics.

FLASH_OPTION

BUFFERED_OPTION

, :// DebugPainting.java // Swing import java.awt.*; import javax.swing.*; public class DebugPainting extends JFrame { public DebugPainting() { super("DebugPainting"); // setDefaultCloseOperation(EXIT_ON_CLOSE); // PaintingComponent pc = new PaintingComponent(); getContentPane().add(pc); // RepaintManager.currentManager(null).setDoubleBufferingEnabled(false); pc.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION | DebugGraphics.FLASH_OPTION);33

DebugGraphics , .

74

DebugGraphics.setFlashTime(50); DebugGraphics.setFlashCount(3); // setSize(200, 200); setVisible(true); } // , - class PaintingComponent extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); // g.setColor(Color.orange); g.fillRect(10, 10, 100, 100); g.setColor(Color.green); g.drawOval(50, 50, 50, 50); g.setColor(Color.blue); g.fillOval(100, 20, 50, 50); } } public static void main(String[] args) { new DebugPainting(); } }

, , JPanel. , , JComponent, . , UI- , , UI- . , , UI-. : , , , UI, , ComponentUI, , UI-. : JPanel, ( ), UI-. , FLASH_OPTION, RepaintManager() ( , setDoubleBuffered(false) ). : , DebugGraphics . ( , ). . , ( ), :Graphics(Nl-N2) :

N1 DebugGraphics ( DebugGraphics ), N2 . N2 3 , setDebugGraphicsOptions() ( ). , . ( ) , . , Swing. , , . , , , PaintingComponent. Swing , . Swing, , , . , Swing, .

75

AWT JDK . AWT . Swing (keyboard bindings, keyboard shortcuts). , : - . JComponent processKeyEvent(), ( 2). JComponent , . , , Keystroke , .

Keystroke Keystroke , , . , ( ), , , , , Ctrl Alt. , : ( ), ( , , Enter Esc) , ( ). Keystroke , (private). Keystroke, , (, , , ). Keystroke getKeyStroke() 34. Keystroke, , , . Keystroke , , , .

JDK 1.3 , Hashtable, . . , (), - - . .

34

Keystroke .

76

, JComponent, (input map). InputMap; , , , . , , Swing , . , , , InputMap put(KeyStroke, Object). , , (, ). , getInputMap(). , setInputMap(). UI- , , , . , . , ( 3.2). 3.2. WHEN_FOCUSED WHEN_ANCESTOR_OF_FOCUSED_COMPONENT WHEN_IN_FOCUSED_WINDOW

, , . , - , , . , Swing

getInputMap(), , , . WHEN_FOCUSED. . WHEN_FOCUSED ( , : , ) WHEN_IN_FOCUSED_WINDOW ( - , ). . InputMap setParent(), . : , ( ) , , . , - , , . , . , , . , JComponent, (action map). - , Action ( ActionListener). Action , - . 7, , actionPerformed(), . , Action Map, , . , , , , , . , . get/set.

77

, ? . . -, , . -, : , , , , . , -, , InputMap ActionMap , . , : Swing .

, , JComponent, processKeyEvent() . . 1. . AWT ( ) processKeyEvent(). 2. Swing, processKeyEvent() JComponent. super.processKeyEvent(), (, , ). JComponent , ( - , consume()). , . , . , processKeyEvent() JComponent, , , ( 2). JComponent, ( enableEvents()). 3. , processComponentKeyEvent(). , , , JComponent . processKeyEvent() processKeyEvent(), processKeyEvent() JComponent, . (), JComponent . 4. , , , , , . KeyboardState, JComponent. , , . , . , processKeyBindings(). 5. processKeyBindings() , . Keystroke, . 6. processKeyBinding() , WHEN_FOCUSED . , , , . , , . (, processKeyBinding(), .) 7. , . processKeyBinding(), ,

78

8.

9.

WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, . , . KeyboardManager. Swing 35 , . WHEN_IN_FOCUSED_WINDOW, . , processKeyBindings() KeyboardManager.getCurrentManager() fireKeyboardAction(). fireKeyboardAction() . JComponent , , WHEN_IN_FOCUSED_WINDOW , KeyboardManager. KeyboardManager , 36 ( , ). processKeyBinding(), , WHEN_IN_FOCUSED_WINDOW. , JMenuBar. KeyboardManager processKeyBinding(), .

JComponent Swing . , . , , (, ), . .// KeyBindingTest.java // import javax.swing.*; import java.awt.event.*; public class KeyBindingTest extends JFrame { public KeyBindingTest() { super("KeyBindingTest"); // setDefaultCloseOperation(EXIT_ON_CLOSE); // InputMap im = getRootPane().getInputMap(); ActionMap am = getRootPane().getActionMap(); // Ctrl+A im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK, true), "Action"); // '' im.put(KeyStroke.getKeyStroke(''), "Action"); am.put("Action", new AnAction()); // setSize(200, 200); setVisible(true); } // class AnAction extends AbstractAction { public void actionPerformed(ActionEvent e) { System.out.println("OK"); } } public static void main(String[] args) { new KeyBindingTest(); } }

35 36

, RepaintManager, KeyboardManager. , . , Swing. KeyboardManager , Swing ( JComponent), -. , ( ).

79

( Swing ). : Ctrl+A ( , , , Keystroke), . , KEY_TYPED ( ), KEY_PRESSED KEY_RELEASED , KeyEvent, . Keystroke getKeyStroke(), KEY_TYPED. . , Swing, 37. 7, . , , . ( WHEN_FOCUSED, getInputMap() ), . , . , Swing . : , . , , . , . UI, , . , .

. , , . ( , , ), , , . : ( ) ( Tab ). , , , . , Java . , , AWT, , . JDK , , , - . , , , . Swing , JComponent, FocusManager, , . JComponent , , , , , .37

, , , .

80

Swing , , AWT , . , . , , , , , , . Java , JDK 1.4 . AWT KeyboardFocusManager. , , . Java Component, , , . Component ( , Swing) KeyboardFocusManager. , , , , .

, , . , Component, , , setFocusable() ( , , ), , . , , Set, setFocusTraversalKeys(). Tab Shift+Tab . KeyboardFocusManager, , , . :// FocusKeysTest.java // import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.util.HashSet; public class FocusKeysTest extends JFrame { public FocusKeysTest() { super("FocusKeysTest"); // setDefaultCloseOperation(EXIT_ON_CLOSE); // HashSet set = new HashSet(); set.add(AWTKeyStroke.getAWTKeyStroke('Q', KeyEvent.CTRL_MASK)); button.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set); // getContentPane().setLayout(new FlowLayout()); getContentPane().add(button); getContentPane().add(new JButton("")); // setSize(200, 200); setVisible(true); } // JButton button = new JButton(""); public static void main(String[] args) { new FocusKeysTest(); } }

, .

81

Set ( Set , , HashSet) , . AWTKeyStroke 38. Ctrl+Q, , FORWARD_TRAVERSAL_KEYS, ( ). , , , Tab. , , , , , , . JDK , , , , , , . , ( 5), , , . , . FocusTraversalPolicy. , , FocusTraversalPolicy , 39. , AWT Swing , ( 3.3). 3.3. ContainerOrderFocusTraversalPolicy

JDK . , , . . , . AWT. Swing Swing. , , , . Comparator, . , Comparator , , LayoutComparator. , . , . ( ). Swing

DefaultFocusTraversalPolicy

SortingFocusTraversalPolicy

LayoutFocusTraversalPolicy

, , , , , , , . , SortingFocusTraversalPolicy, . , :// FocusPolicyTest.java // import javax.swing.*; import java.awt.ContainerOrderFocusTraversalPolicy; import java.awt.event.*;

38 Keystroke. JDK 1.4 Keystroke AWTKeyStroke, a Keystroke . 39 FocusTraversalPolicy , , .

82

public class FocusPolicyTest extends JFrame { public FocusPolicyTest() { super("FocusPolicyTest"); // setDefaultCloseOperation(EXIT_ON_CLOSE); // getContentPane().add(new JButton(""), "West"); // , // JButton button = new JButton(""); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy()); } }); getContentPane().add(button, "South"); getContentPane().add(new JButton(""), "East"); // setSize(200, 200); setVisible(true); } public static void main(String[] args) { new FocusPolicyTest(); } }

, , . , , . , . , . LayoutFocusTraversalPolicy, , . ContainerOrderFocusTraversalPolicy, , -, , ( ), -, - . JDK, Swing , , , . . LayoutFocusTraversalPolicy , , , JComponent . ( , JDK) Swing AWT.

, , , Java. , (focus cycle) (focus cycle root). , . , . , , , , . , KeyboardFocusManager. , isFocusCycleRoot() , true. , , .

83

Java, Window. Java ( , , ), , , - -, , . , KeyboardFocusManager, Java , . setFocusableWindowState(false). , , . ( WindowFocusListener) ( , ) ( , ). , , , Java KeyboardFocusManager, . , Java, , . KeyboardFocusManager. , , , . FocusEvent, , , . KeyboardFocusManager Java.

KeyboardFocusManager , -, . , Component KeyboardFocusManager. . 1. EventQueue EventDispatchThread dispatchEvent() Component. , , retargetFocusEvent() KeyboardFocusManager. 2. retargetFocusEvent() , , , , . , . , , ( ) , , . dispatchEvent(). 3. dispatchEvent() KeyboardFocusManager. . WINDOW_ACTIVATED FOCUS_GAINED, . , KeyboardFocusManager . WINDOW_DEACTIVATED, FOCUS_LOST WINDOW_GAINED_FOCUS, . . 4. KeyboardFocusManager , Component. , . . KeyEvent processKeyEvent() KeyboardFocusManager. 5. processKeyEvent() , (, KEY_TYPED ) .

84

, , . , , FocusTraversalPolicy. , , . , , KeyboardFocusManager AWT, , , . AWT , , , , . KeyboardFocusManager , . . , KeyboardFocusManager , , processKeyEvent(), . , KeyboardFocusManager : KeyEventDispatcher , , KeyEventPostProcessor , . , , KeyboardFocusManager.

, Swing JComponent , . , Swing . Swing (tool tips) . Swing JComponent. setToolTipText(), . JComponent ToolTipManager. . , , , , , , . 6. , JComponent (client properties), Swing. JavaBeans, , , get/set. , putClientProperty() getClientProperty(). , JavaBeans, . , , , . Swing , , , ( ) JavaBeans, ( , , ). , , , . , ,