⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webbrowser.java

📁 Examples From Java Examples in a Nutshell, 2nd Edition 书中的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    // If we can go back, then enable the Back action	    if (currentHistoryPage > 0) backAction.setEnabled(true);	}    }    /** Like displayPage(URL), but takes a string instead */    public void displayPage(String href) {	try {	    displayPage(new URL(href));	}	catch (MalformedURLException ex) {	    messageLine.setText("Bad URL: " + href);	}    }    /** Allow the user to choose a local file, and display it */    public void openPage() {	// Lazy creation: don't create the JFileChooser until it is needed	if (fileChooser == null) {	    fileChooser = new JFileChooser();	    // This javax.swing.filechooser.FileFilter displays only HTML files	    FileFilter filter = new FileFilter() {		    public boolean accept(File f) {			String fn = f.getName();			if (fn.endsWith(".html") || fn.endsWith(".htm"))			    return true;			else return false;		    }		    public String getDescription() { return "HTML Files"; }		};	    fileChooser.setFileFilter(filter);	    fileChooser.addChoosableFileFilter(filter);	}	// Ask the user to choose a file.	int result = fileChooser.showOpenDialog(this);	if (result == JFileChooser.APPROVE_OPTION) {	    // If they didn't click "Cancel", then try to display the file.	    File selectedFile = fileChooser.getSelectedFile();	    String url = "file://" + selectedFile.getAbsolutePath();	    displayPage(url);	}    }    /** Go back to the previously displayed page. */    public void back() {	if (currentHistoryPage > 0)  // go back, if we can	    visit((URL)history.get(--currentHistoryPage));	// Enable or disable actions as appropriate	backAction.setEnabled((currentHistoryPage > 0));	forwardAction.setEnabled((currentHistoryPage < history.size()-1));    }    /** Go forward to the next page in the history list */    public void forward() {	if (currentHistoryPage < history.size()-1)  // go forward, if we can	    visit((URL)history.get(++currentHistoryPage));	// Enable or disable actions as appropriate	backAction.setEnabled((currentHistoryPage > 0));	forwardAction.setEnabled((currentHistoryPage < history.size()-1));    }    /** Reload the current page in the history list */    public void reload() {	if (currentHistoryPage != -1)	    visit((URL)history.get(currentHistoryPage));    }    /** Display the page specified by the "home" property */    public void home() { displayPage(getHome()); }    /** Open a new browser window */    public void newBrowser() {	WebBrowser b = new WebBrowser();	b.setSize(this.getWidth(), this.getHeight());	b.setVisible(true);    }    /**     * Close this browser window.  If this was the only open window,     * and exitWhenLastBrowserClosed is true, then exit the VM     **/    public void close() {	this.setVisible(false);             // Hide the window	this.dispose();                     // Destroy the window	synchronized(WebBrowser.class) {    // Synchronize for thread-safety	    WebBrowser.numBrowserWindows--; // There is one window fewer now	    if ((numBrowserWindows==0) && exitWhenLastWindowClosed)		System.exit(0);             // Exit if it was the last one	}    }           /**     * Exit the VM.  If confirm is true, ask the user if they are sure.     * Note that showConfirmDialog() displays a dialog, waits for the user,     * and returns the user's response (i.e. the button the user selected).     **/    public void exit(boolean confirm) {	if (!confirm ||	    (JOptionPane.showConfirmDialog(this,  // dialog parent	         /* message to display */  "Are you sure you want to quit?",		 /* dialog title */	   "Really Quit?",		 /* dialog buttons */	   JOptionPane.YES_NO_OPTION) == 	     JOptionPane.YES_OPTION))  // If Yes button was clicked	    System.exit(0);    }    /**     * Print the contents of the text pane using the java.awt.print API     * Note that this API does not work efficiently in Java 1.2     * All the hard work is done by the PrintableDocument class.     **/    public void print() {	// Get a PrinterJob object from the system	PrinterJob job = PrinterJob.getPrinterJob();	// This is the object that we are going to print	PrintableDocument pd = new PrintableDocument(textPane);	// Tell the PrinterJob what we want to print	job.setPageable(pd);	// Display a print dialog, asking the user what pages to print, what	// printer to print to, and giving the user a chance to cancel.	if (job.printDialog()) {  // If the user did not cancel	    try { job.print(); }  // Start printing!	    catch(PrinterException ex) {  // display errors nicely		messageLine.setText("Couldn't print: " + ex.getMessage());	    }	}    }    /**      * This method implements HyperlinkListener.  It is invoked when the user     * clicks on a hyperlink, or move the mouse onto or off of a link     **/    public void hyperlinkUpdate(HyperlinkEvent e) {	HyperlinkEvent.EventType type = e.getEventType();  // what happened?	if (type == HyperlinkEvent.EventType.ACTIVATED) {     // Click!	    displayPage(e.getURL());   // Follow the link; display new page	}	else if (type == HyperlinkEvent.EventType.ENTERED) {  // Mouse over!	    // When mouse goes over a link, display it in the message line	    messageLine.setText(e.getURL().toString());  	}	else if (type == HyperlinkEvent.EventType.EXITED) {   // Mouse out!	    messageLine.setText(" ");  // Clear the message line	}    }    /**     * This method implements java.beans.PropertyChangeListener.  It is      * invoked whenever a bound property changes in the JEditorPane object.     * The property we are interested in is the "page" property, because it     * tells us when a page has finished loading.     **/    public void propertyChange(PropertyChangeEvent e) {	if (e.getPropertyName().equals("page")) // If the page property changed	    stopAnimation();              // Then stop the loading... animation    }    /**     * The fields and methods below implement a simple animation in the     * web browser message line; they are used to provide user feedback     * while web pages are loading.     **/    String animationMessage;  // The "loading..." message to display    int animationFrame = 0;   // What "frame" of the animation are we on    String[] animationFrames = new String[] {  // The content of each "frame"	"-", "\\", "|", "/", "-", "\\", "|", "/", 	",", ".", "o", "0", "O", "#", "*", "+"    };    /** This object calls the animate() method 8 times a second */    javax.swing.Timer animator =	new javax.swing.Timer(125, new ActionListener() {		public void actionPerformed(ActionEvent e) { animate(); }	    });    /** Display the next frame. Called by the animator timer */    void animate() {	String frame = animationFrames[animationFrame++];    // Get next frame	messageLine.setText(animationMessage + " " + frame); // Update msgline	animationFrame = animationFrame % animationFrames.length;    }    /** Start the animation.  Called by the visit() method. */    void startAnimation(String msg) {	animationMessage = msg;     // Save the message to display	animationFrame = 0;         // Start with frame 0 of the animation	animator.start();           // Tell the timer to start firing.    }    /** Stop the animation.  Called by propertyChanged() method. */    void stopAnimation() {       	animator.stop();            // Tell the timer to stop firing events	messageLine.setText(" ");   // Clear the message line    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -