ImageVerifierCode 换一换
格式:PPT , 页数:71 ,大小:1.15MB ,
资源ID:379638      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-379638.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Chapter 2 - Advanced Swing Graphical User Interface .ppt)为本站会员(arrownail386)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

Chapter 2 - Advanced Swing Graphical User Interface .ppt

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();,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1