Chapter 2 - Advanced Swing Graphical User Interface .ppt
《Chapter 2 - Advanced Swing Graphical User Interface .ppt》由会员分享,可在线阅读,更多相关《Chapter 2 - Advanced Swing Graphical User Interface .ppt(71页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER2ADVANCEDSWINGGRAPHICALUSERINTERFACEPPT

链接地址:http://www.mydoc123.com/p-379638.html