1、Chapter 2 - Advanced Swing Graphical User Interface Components,Outline 2.1 Introduction 2.2 WebBrowser Using JEditorPane and JToolBar 2.2.1 Swing Text Components and HTML Rendering 2.2.2 Swing Toolbars 2.3 Swing Actions 2.4 JSplitPane and JTabbedPane 2.5 Multiple-Document Interfaces 2.6 Drag and Dro
2、p 2.7 Internationalization 2.8 Accessibility 2.9 Internet and World Wide Web Resources,2.1 Introduction,Graphical user interface components Swing components Abstract Windowing Toolkit (AWT) Sample swing components JEditorPane JSplitPane JTabbedPane Swing Actions Using swing components to build appli
3、cations for users with disabilities,2.2 WebBrowser Using JEditorPane and JToolBar,Web browser application Swing text components Swing container components,2.2.1 Swing Text Components and HTML Rendering,Swing Text Components Base class JTextComponent JTextField single-line text component JTextArea mu
4、ltiple lines text component JEditorPane rendering HTML documents and Rich Text Format documents,Fig. 2.1 WebBrowserPane subclass of JEditorPane for viewing Web sites and maintaining URL history. Line 14 Line 23,1 / WebBrowserPane.java 2 / WebBrowserPane is a simple Web-browsing component that 3 / ex
5、tends JEditorPane and maintains a history of visited URLs. 4 package com.deitel.advjhtp1.gui.webbrowser; 5 6 / Java core packages 7 import java.util.*; 8 import .*; 9 import java.io.*; 10 11 / Java extension packages 12 import javax.swing.*; 13 14 public class WebBrowserPane extends JEditorPane 15 1
6、6 private List history = new ArrayList(); 17 private int historyIndex; 18 19 / WebBrowserPane constructor 20 public WebBrowserPane() 21 22 / disable editing to enable hyperlinks 23 setEditable( false ); 24 25 26 / display given URL and add it to history 27 public void goToURL( URL url ) 28 29 displa
7、yPage( url ); 30 history.add( url ); 31 historyIndex = history.size() - 1; 32 33,Fig. 2.1 WebBrowserPane subclass of JEditorPane for viewing Web sites and maintaining URL history. Line 43-44 and 59-60 Line 56,34 / display next history URL in editorPane 35 public URL forward() 36 37 historyIndex+; 38
8、 39 / do not go past end of history 40 if ( historyIndex = history.size() ) 41 historyIndex = history.size() - 1; 42 43 URL url = ( URL ) history.get( historyIndex ); 44 displayPage( url ); 45 46 return url; 47 48 49 / display previous history URL in editorPane 50 public URL back() 51 52 historyInde
9、x-; 53 54 / do not go past beginning of history 55 if ( historyIndex 0 ) 56 historyIndex = 0; 57 58 / display previous URL 59 URL url = ( URL ) history.get( historyIndex ); 60 displayPage( url ); 61 62 return url; 63 64 65 / display given URL in JEditorPane 66 private void displayPage( URL pageURL )
10、 67 ,Fig. 2.1 WebBrowserPane subclass of JEditorPane for viewing Web sites and maintaining URL history. Line 70,68 / display URL 69 try 70 setPage( pageURL ); 71 72 73 / handle exception reading from URL 74 catch ( IOException ioException ) 75 ioException.printStackTrace(); 76 77 78 ,2.2.2 Swing Too
11、lbars,Toolbar GUI container Buttons Other GUI components Adding toolbars with class JToolBar,Fig. 2.2 Toolbars for navigating the Web in Internet Explorer and Mozilla.,Fig. 2.2 Toolbars for navigating the Web in Internet Explorer and Mozilla.,Toolbar buttons,Toolbar,Fig. 2.3 WebToolBar JToolBar subc
12、lass for navigating URLs in a WebBrowserPane. Line 16 Line 25-113,1 / WebToolBar.java 2 / WebToolBar is a JToolBar subclass that contains components 3 / for navigating a WebBrowserPane. WebToolBar includes back 4 / and forward buttons and a text field for entering URLs. 5 package com.deitel.advjhtp1
13、.gui.webbrowser; 6 7 / Java core packages 8 import java.awt.*; 9 import java.awt.event.*; 10 import .*; 11 12 / Java extension packages 13 import javax.swing.*; 14 import javax.swing.event.*; 15 16 public class WebToolBar extends JToolBar 17 implements HyperlinkListener 18 19 private WebBrowserPane
14、webBrowserPane; 20 private JButton backButton; 21 private JButton forwardButton; 22 private JTextField urlTextField; 23 24 / WebToolBar constructor 25 public WebToolBar( WebBrowserPane browser ) 26 27 super( “Web Navigation“ ); 28 29 / register for HyperlinkEvents 30 webBrowserPane = browser; 31 web
15、BrowserPane.addHyperlinkListener( this ); 32,Fig. 2.3 WebToolBar JToolBar subclass for navigating URLs in a WebBrowserPane. Lines 34-53 Lines 56-71,33 / create JTextField for entering URLs 34 urlTextField = new JTextField( 25 ); 35 urlTextField.addActionListener( 36 new ActionListener() 37 38 / navi
16、gate webBrowser to user-entered URL 39 public void actionPerformed( ActionEvent event ) 40 41 / attempt to load URL in webBrowserPane 42 try 43 URL url = new URL( urlTextField.getText() ); 44 webBrowserPane.goToURL( url ); 45 46 47 / handle invalid URL 48 catch ( MalformedURLException urlException )
17、 49 urlException.printStackTrace(); 50 51 52 53 ); 54 55 / create JButton for navigating to previous history URL 56 backButton = new JButton( new ImageIcon( 57 getClass().getResource( “images/back.gif“ ) ) ); 58 59 backButton.addActionListener( 60 new ActionListener() 61,Fig. 2.3 WebToolBar JToolBar
18、 subclass for navigating URLs in a WebBrowserPane. Lines 74-89 Lines 92-94,62 public void actionPerformed( ActionEvent event ) 63 64 / navigate to previous URL 65 URL url = webBrowserPane.back(); 66 67 / display URL in urlTextField 68 urlTextField.setText( url.toString() ); 69 70 71 ); 72 73 / creat
19、e JButton for navigating to next history URL 74 forwardButton = new JButton( new ImageIcon( 75 getClass().getResource( “images/forward.gif“ ) ) ); 76 77 forwardButton.addActionListener( 78 new ActionListener() 79 80 public void actionPerformed( ActionEvent event ) 81 82 / navigate to next URL 83 URL
20、 url = webBrowserPane.forward(); 84 85 / display new URL in urlTextField 86 urlTextField.setText( url.toString() ); 87 88 89 ); 90 91 / add JButtons and JTextField to WebToolBar 92 add( backButton ); 93 add( forwardButton ); 94 add( urlTextField ); 95 96 / end WebToolBar constructor,Fig. 2.3 WebTool
21、Bar JToolBar subclass for navigating URLs in a WebBrowserPane. Lines 99-112,97 98 / listen for HyperlinkEvents in WebBrowserPane 99 public void hyperlinkUpdate( HyperlinkEvent event ) 100 101 / if hyperlink was activated, go to hyperlinks URL 102 if ( event.getEventType() = 103 HyperlinkEvent.EventT
22、ype.ACTIVATED ) 104 105 / get URL from HyperlinkEvent 106 URL url = event.getURL(); 107 108 / navigate to URL and display URL in urlTextField 109 webBrowserPane.goToURL( url ); 110 urlTextField.setText( url.toString() ); 111 112 113 ,Method hyperlinkUpdate invokes method getEventType of class Hyperl
23、inkEvent to check the event type and retrieves the HyperlinkEvents URL.,Fig. 2.4 WebBrowser application for browsing Web sites using WebBrowserPane and WebToolBar. Lines 26-27 Lines 30-33,1 / WebBrowser.java 2 / WebBrowser is an application for browsing Web sites using 3 / a WebToolBar and WebBrowse
24、rPane. 4 package com.deitel.advjhtp1.gui.webbrowser; 5 6 / Java core packages 7 import java.awt.*; 8 import java.awt.event.*; 9 import .*; 10 11 / Java extension packages 12 import javax.swing.*; 13 import javax.swing.event.*; 14 15 public class WebBrowser extends JFrame 16 17 private WebToolBar too
25、lBar; 18 private WebBrowserPane browserPane; 19 20 / WebBrowser constructor 21 public WebBrowser() 22 23 super( “Deitel Web Browser“ ); 24 25 / create WebBrowserPane and WebToolBar for navigation 26 browserPane = new WebBrowserPane(); 27 toolBar = new WebToolBar( browserPane ); 28 29 / lay out WebBr
26、owser components 30 Container contentPane = getContentPane(); 31 contentPane.add( toolBar, BorderLayout.NORTH ); 32 contentPane.add( new JScrollPane( browserPane ), 33 BorderLayout.CENTER ); 34 35,Fig. 2.4 WebBrowser application for browsing Web sites using WebBrowserPane and WebToolBar. output,36 /
27、 execute application 37 public static void main( String args ) 38 39 WebBrowser browser = new WebBrowser(); 40 browser.setDefaultCloseOperation( EXIT_ON_CLOSE ); 41 browser.setSize( 640, 480 ); 42 browser.setVisible( true ); 43 44 ,2.3 Swing Actions,Command design pattern Define functionality once i
28、n a reusable object Action interface Required method of the Command design pattern Process ActionEvents generated by GUI components Easy to enable or disable actions,Fig. 2.5 ActionSample application demonstrating the Command design pattern with Swing Actions. Lines 15-16 Lines 24-35 Lines 26-34 Lin
29、es 29-30 Line 33,1 / ActionSample.java 2 / Demonstrating the Command design pattern with Swing Actions. 3 package com.deitel.advjhtp1.gui.actions; 4 5 / Java core packages 6 import java.awt.*; 7 import java.awt.event.*; 8 9 / Java extension packages 10 import javax.swing.*; 11 12 public class Action
30、Sample extends JFrame 13 14 / Swing Actions 15 private Action sampleAction; 16 private Action exitAction; 17 18 / ActionSample constructor 19 public ActionSample() 20 21 super( “Using Actions“ ); 22 23 / create AbstractAction subclass for sampleAction 24 sampleAction = new AbstractAction() 25 26 pub
31、lic void actionPerformed( ActionEvent event ) 27 28 / display message indicating sampleAction invoked 29 JOptionPane.showMessageDialog( ActionSample.this, 30 “The sampleAction was invoked“ ); 31 32 / enable exitAction and associated GUI components 33 exitAction.setEnabled( true ); 34 35 ;,Fig. 2.5 A
32、ctionSample application demonstrating the Command design pattern with Swing Actions. Lines 38-50 Lines 53-62 Lines 55-61 Lines 65-77,36 37 / set Action name 38 sampleAction.putValue( Action.NAME, “Sample Action“ ); 39 40 / set Action Icon 41 sampleAction.putValue( Action.SMALL_ICON, new ImageIcon( 4
33、2 getClass().getResource( “images/Help24.gif“ ) ) ); 43 44 / set Action short description (tooltip text) 45 sampleAction.putValue( Action.SHORT_DESCRIPTION, 46 “A Sample Action“ ); 47 48 / set Action mnemonic key 49 sampleAction.putValue( Action.MNEMONIC_KEY, 50 new Integer( S ) ); 51 52 / create Ab
34、stractAction subclass for exitAction 53 exitAction = new AbstractAction() 54 55 public void actionPerformed( ActionEvent event ) 56 57 / display message indicating exitAction invoked 58 JOptionPane.showMessageDialog( ActionSample.this, 59 “The exitAction was invoked“ ); 60 System.exit( 0 ); 61 62 ;
35、63 64 / set Action name 65 exitAction.putValue( Action.NAME, “Exit“ ); 66 67 / set Action icon 68 exitAction.putValue( Action.SMALL_ICON, new ImageIcon( 69 getClass().getResource( “images/EXIT.gif“ ) ) ); 70,Fig. 2.5 ActionSample application demonstrating the Command design pattern with Swing Action
36、s. Lines 72-77 Line 80 Lines 83-90 Lines 93-95 Lines 98-103,71 / set Action short description (tooltip text) 72 exitAction.putValue( Action.SHORT_DESCRIPTION, 73 “Exit Application“ ); 74 75 / set Action mnemonic key 76 exitAction.putValue( Action.MNEMONIC_KEY, 77 new Integer( x ) ); 78 79 / disable
37、exitAction and associated GUI components 80 exitAction.setEnabled( false ); 81 82 / create File menu 83 JMenu fileMenu = new JMenu( “File“ ); 84 85 / add sampleAction and exitAction to File menu to 86 / create a JMenuItem for each Action 87 fileMenu.add( sampleAction ); 88 fileMenu.add( exitAction )
38、; 89 90 fileMenu.setMnemonic( F ); 91 92 / create JMenuBar and add File menu 93 JMenuBar menuBar = new JMenuBar(); 94 menuBar.add( fileMenu ); 95 setJMenuBar( menuBar ); 96 97 / create JToolBar 98 JToolBar toolBar = new JToolBar(); 99 100 / add sampleAction and exitAction to JToolBar to create 101 /
39、 JButtons for each Action 102 toolBar.add( sampleAction ); 103 toolBar.add( exitAction ); 104,Fig. 2.5 ActionSample application demonstrating the Command design pattern with Swing Actions. Lines 106-110 Lines 113-115 Lines 118-120,105 / create JButton and set its Action to sampleAction 106 JButton s
40、ampleButton = new JButton(); 107 sampleButton.setAction( sampleAction ); 108 109 / create JButton and set its Action to exitAction 110 JButton exitButton = new JButton( exitAction ); 111 112 / lay out JButtons in JPanel 113 JPanel buttonPanel = new JPanel(); 114 buttonPanel.add( sampleButton ); 115
41、buttonPanel.add( exitButton ); 116 117 / add toolBar and buttonPanel to JFrames content pane 118 Container container = getContentPane(); 119 container.add( toolBar, BorderLayout.NORTH ); 120 container.add( buttonPanel, BorderLayout.CENTER ); 121 122 123 / execute application 124 public static void m
42、ain( String args ) 125 126 ActionSample sample = new ActionSample(); 127 sample.setDefaultCloseOperation( EXIT_ON_CLOSE ); 128 sample.pack(); 129 sample.setVisible( true ); 130 131 ,Fig. 2.5 ActionSample application demonstrating the Command design pattern with Swing Actions. program output,2.3 Swin
43、g Actions (Cont.),2.4 JSplitPane and JTabbedPane,Container components Display information in a small area JSplitPane Divide two components with a divider JTabbedPane Separate components with file-folder-style tabs,Fig. 2.7 FavoritesWebBrowser application for displaying two Web pages side-by-side usi
44、ng JSplitPane. Lines 31-32 Line 35,1 / FavoritesWebBrowser.java 2 / FavoritesWebBrowser is an application for browsing Web sites 3 / using a WebToolBar and WebBrowserPane and displaying an HTML 4 / page containing links to favorite Web sites. 5 package com.deitel.advjhtp1.gui.splitpane; 6 7 / Java c
45、ore packages 8 import java.awt.*; 9 import java.awt.event.*; 10 import .*; 11 12 / Java extension packages 13 import javax.swing.*; 14 import javax.swing.event.*; 15 16 / Deitel packages 17 import com.deitel.advjhtp1.gui.webbrowser.*; 18 19 public class FavoritesWebBrowser extends JFrame 20 21 priva
46、te WebToolBar toolBar; 22 private WebBrowserPane browserPane; 23 private WebBrowserPane favoritesBrowserPane; 24 25 / WebBrowser constructor 26 public FavoritesWebBrowser() 27 28 super( “Deitel Web Browser“ ); 29 30 / create WebBrowserPane and WebToolBar for navigation 31 browserPane = new WebBrowserPane(); 32 toolBar = new WebToolBar( browserPane ); 33 34 / create WebBrowserPane for displaying favorite sites 35 favoritesBrowserPane = new WebBrowserPane();,