📄 htmllinkfinder.java~
字号:
import java.awt.*;import java.net.*;import java.io.*;import java.awt.event.*;import javax.swing.*;// A class that allows one to fetch an HTML file from the web and obtain// a list of all the links contained within the HTML.public class HTMLLinkFinder extends JFrame implements ActionListener, ItemListener { // dimensions of the window private static final int WINDOW_WIDTH = 800; private static final int WINDOW_HEIGHT = 550; // Width of area for URL entry private static final int URLFIELDWIDTH = 60; // Dimensions for HTML display area private static final int HTMLWIDTH = 60; private static final int HTMLHEIGHT = 14; // Dimensions for links display area private static final int LINKSWIDTH = 60; private static final int LINKSHEIGHT = 10; // Large font for GUI controls private Font bigFont = new Font("Times", Font.PLAIN, 16); // Area in which HTML is displayed private JTextArea html; // Link display area private JTextArea links; // The "load" button private JButton doit; // Url controls private JTextField urlName; private JComboBox favorites; // Set up all GUI controls and their listeners public void begin() { Container pane = getContentPane(); pane.setLayout(new FlowLayout()); // Create a "bookmarks" menu pane.add(new JLabel("Favorites:")); favorites = new JComboBox(); favorites.setFont(bigFont); favorites.addItem("http://wso.williams.edu"); favorites.addItem("http://www.williams.edu"); favorites.addItem("http://www.cs.williams.edu"); favorites.addItem( "http://www.williams.edu/admin-depts/registrar/exams/index.html"); favorites.addItem( "http://www.williams.edu/admin-depts/registrar/catalog/springhours.html"); favorites.addItem( "http://cortland.cs.williams.edu/~cs134/s04/index.html"); favorites.addItemListener(this); pane.add(favorites); // Create a text area into which a URL may be entered urlName = new JTextField("Enter URL", URLFIELDWIDTH); urlName.setFont(bigFont); pane.add(urlName); // Create the button used to start fetching a web page doit = new JButton("Fetch HTML"); doit.setFont(bigFont); doit.addActionListener(this); pane.add(doit); // Create the text areas in which HTML and links will be displayed. html = new JTextArea(HTMLHEIGHT, HTMLWIDTH); html.setFont(bigFont); pane.add(new JScrollPane(html)); links = new JTextArea(LINKSHEIGHT, LINKSWIDTH); links.setFont(bigFont); pane.add(new JScrollPane(links)); validate(); } // If item is selected from "favorites" menu, enter it as current URL public void itemStateChanged(ItemEvent e) { urlName.setText(favorites.getSelectedItem().toString()); } // Fetch the web page at "urlString" and return its contents as a String private String getWebPage(String urlString) { String page = ""; try { URL url = new URL(urlString); BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream())); String line = input.readLine(); while (line != null) { page = page + line + "\n"; line = input.readLine(); } input.close(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex); } return page; } // Extract all the links from a web page private String findLinks(String fullpage) { int tagPos, // Start of <A tag specification tagEnd; // Position of first ">" after tag start // A lower case only version of the page for searching String lowerpage = fullpage.toLowerCase(); // Text of one A tag String tag; // The A tags found so far String links = ""; // Paste stuff on end of page to ensure searches succeed fullpage = fullpage + " >"; tagPos = lowerpage.indexOf("<a ", 0); while (tagPos >= 0) { tagEnd = fullpage.indexOf(">", tagPos + 1); tag = fullpage.substring(tagPos, tagEnd + 1); links = links + tag + "\n"; tagPos = lowerpage.indexOf("<a ", tagEnd); } return links; } // When the "Fetch" button is pressed, try to get the HTML, display and search it. public void actionPerformed(ActionEvent e) { String fullpage; String url; if (links.getSelectedText() != null) { urlName.setText(links.getSelectedText().trim()); } url = urlName.getText(); fullpage = getWebPage(url); html.setText(fullpage); links.setText(findLinks(fullpage)); } public static void main ( String[] args ) { HTMLLinkFinder self = new HTMLLinkFinder(); self.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); self.setTitle("HTML Link Finder"); self.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); self.setVisible(true); self.begin(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -