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

📄 exercise28_9.java

📁 java程序设计 机械工业出版社 书籍代码
💻 JAVA
字号:
// Exercise28_9.java: Display HTML file in JEditorPane// This program can display a local or a remote resourceimport java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.net.URL;import javax.swing.event.*;import java.io.*;public class Exercise28_9 extends JApplet implements ActionListener, HyperlinkListener {  // JEditor pane to view HTML files  JEditorPane jep = new JEditorPane();  // Label for URL  JLabel jlblURL = new JLabel("URL");  // Text field for entering URL  JTextField jtfURL = new JTextField();  // Initialize the applet  public void init() {    // Create a panel jpURL to hold the label and text field    JPanel jpURL = new JPanel();    jpURL.setLayout(new BorderLayout());    jpURL.add(jlblURL, BorderLayout.WEST);    jpURL.add(jtfURL, BorderLayout.CENTER);    // Create a scroll pane to hold JEditorPane    JScrollPane jspViewer = new JScrollPane();    jspViewer.getViewport().add(jep, null);    // Place jpURL and jspViewer in the applet    this.getContentPane().add(jspViewer, BorderLayout.CENTER);    this.getContentPane().add(jpURL, BorderLayout.NORTH);    // Set jep noneditable    jep.setEditable(false);    // Register listener    jep.addHyperlinkListener(this);    jtfURL.addActionListener(this);  }  public void actionPerformed(ActionEvent e) {    // TODO: Implement this java.awt.event.ActionListener method    String urlString = jtfURL.getText().trim();    if (isSubstring("www", urlString)) {      urlString = "http://" + urlString;    }    URL url;    try {      if (isSubstring("http", urlString)) {        // Get the URL from remote Web server        url = new URL(urlString);      }      else {        // Get the URL from local host        url = this.getClass().getResource(urlString);      }      System.out.println("urlString " + urlString);      // Display the HTML file      jep.setPage(url);    }    catch (IOException ex) {      System.out.println(ex);    }  }  public void hyperlinkUpdate(HyperlinkEvent e) {    // TODO: Implement HyperlinkListener method    try {      jep.setPage(e.getURL());    }    catch (IOException ex) {      System.out.println(ex);    }  }  // Main method  public static void main(String[] args) {    // Create a frame    JFrame frame = new JFrame("Exercise28_9");    // Create an instance of the applet    Exercise28_9 applet = new Exercise28_9();    // Add the applet instance to the frame    frame.getContentPane().add(applet, BorderLayout.CENTER);    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    // Invoke init() and start()    applet.init();    applet.start();    // Display the frame    frame.setSize(600, 600);    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setVisible(true);  }  // Check if the first string is a substring of the second string  public static boolean isSubstring(String first, String second) {    int remainingLength = second.length();    int startingIndex = 0;    // Note toWhile is a label. You can use break with a label    // attached.    toWhile: while (first.length() <= remainingLength) {      // What is wrong if the following line is used      // for (int i=startingIndex; i<=first.length(); i++)      for (int i=0; i<first.length(); i++) {        if (first.charAt(i) != second.charAt(startingIndex+i)) {          startingIndex++;          remainingLength--;          continue toWhile;        }      }      return true;    }    return false;  }}

⌨️ 快捷键说明

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