📄 webtoolbar.java
字号:
// WebToolBar.java
// WebToolBar is a JToolBar subclass that contains components
// for navigating a WebBrowserPane. WebToolBar includes back
// and forward buttons and a text field for entering URLs.
package com.deitel.advjhtp1.gui.webbrowser;
// Java core packages
import java.awt.*;
import java.awt.event.*;
import java.net.*;
// Java extension packages
import javax.swing.*;
import javax.swing.event.*;
public class WebToolBar extends JToolBar
implements HyperlinkListener {
private WebBrowserPane webBrowserPane;
private JButton backButton;
private JButton forwardButton;
private JTextField urlTextField;
// WebToolBar constructor
public WebToolBar( WebBrowserPane browser )
{
super( "Web Navigation" );
// register for HyperlinkEvents
webBrowserPane = browser;
webBrowserPane.addHyperlinkListener( this );
// create JTextField for entering URLs
urlTextField = new JTextField( 25 );
urlTextField.addActionListener(
new ActionListener() {
// navigate webBrowser to user-entered URL
public void actionPerformed( ActionEvent event )
{
// attempt to load URL in webBrowserPane
try {
URL url = new URL( urlTextField.getText() );
webBrowserPane.goToURL( url );
}
// handle invalid URL
catch ( MalformedURLException urlException ) {
urlException.printStackTrace();
}
}
}
);
// create JButton for navigating to previous history URL
backButton = new JButton( new ImageIcon(
getClass().getResource( "images/back.gif" ) ) );
backButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
// navigate to previous URL
URL url = webBrowserPane.back();
// display URL in urlTextField
urlTextField.setText( url.toString() );
}
}
);
// create JButton for navigating to next history URL
forwardButton = new JButton( new ImageIcon(
getClass().getResource( "images/forward.gif" ) ) );
forwardButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
// navigate to next URL
URL url = webBrowserPane.forward();
// display new URL in urlTextField
urlTextField.setText( url.toString() );
}
}
);
// add JButtons and JTextField to WebToolBar
add( backButton );
add( forwardButton );
add( urlTextField );
} // end WebToolBar constructor
// listen for HyperlinkEvents in WebBrowserPane
public void hyperlinkUpdate( HyperlinkEvent event )
{
// if hyperlink was activated, go to hyperlink's URL
if ( event.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED ) {
// get URL from HyperlinkEvent
URL url = event.getURL();
// navigate to URL and display URL in urlTextField
webBrowserPane.goToURL( url );
urlTextField.setText( url.toString() );
}
}
}
/***************************************************************
* (C) Copyright 2002 by Deitel & Associates, Inc. and *
* Prentice Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have *
* used their best efforts in preparing the book. These *
* efforts include the development, research, and testing of *
* the theories and programs to determine their effectiveness. *
* The authors and publisher make no warranty of any kind, *
* expressed or implied, with regard to these programs or to *
* the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for *
* incidental or consequential damages in connection with, or *
* arising out of, the furnishing, performance, or use of *
* these programs. *
***************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -