欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Chapter 2 - Advanced Swing Graphical User Interface .ppt

    • 资源ID:379638       资源大小:1.15MB        全文页数:71页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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


    注意事项

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




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

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

    收起
    展开