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

📄 urltest.java

📁 用java编写的一个URL测试程序
💻 JAVA
字号:
//Listing 21-1 -- URLTest.java 
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*; 
import java.net.*;
public class URLTest extends Applet implements ActionListener {
	private URL url_site;		// URL for connection
	// Add buttons
	private Button bu_microsoft = new Button("Microsoft");
	private Button bu_sun = new Button("Sun");
	private Button bu_connect = new Button("Connect");
	// Add text fields
	private Label lbl_file;
	private Label lbl_host;
	private Label lbl_port;
	private Label lbl_protocol;
	private Label lbl_ref;
	private Label lbl_externalForm;
	private Label lbl_string;
	public void init() {		// init called when applet starts
		Panel northPanel = new Panel();
		Panel centerPanel = new Panel();
		Panel fieldPanel = new Panel();
		Panel labelPanel = new Panel();
		Panel southPanel = new Panel();
		// Set Panel and Applet layouts
		setLayout(new BorderLayout());
		centerPanel.setLayout(new BorderLayout());
		labelPanel.setLayout(new GridLayout(7,1));
		fieldPanel.setLayout(new GridLayout(7,1));
		northPanel.setLayout(new FlowLayout());
		southPanel.setLayout(new FlowLayout());
		//Add buttons to the appropriate Panels
		northPanel.add(bu_microsoft);
		northPanel.add(bu_sun);
		southPanel.add(bu_connect);
		//Add labels to the labelPanel
		labelPanel.add(new Label("File Name:  ", Label.RIGHT));
		labelPanel.add(new Label("Host:  ", Label.RIGHT));
		labelPanel.add(new Label("Port:  ", Label.RIGHT));
		labelPanel.add(new Label("Protocol:  ", Label.RIGHT));
		labelPanel.add(new Label("Reference:  ", Label.RIGHT));
		labelPanel.add(new Label("External Form:  ", Label.RIGHT));
		labelPanel.add(new Label("String:  ", Label.RIGHT));
		//Add label "fields" to the fieldPanel
		fieldPanel.add(lbl_file = new Label());
		fieldPanel.add(lbl_host = new Label());
		fieldPanel.add(lbl_port = new Label());
		fieldPanel.add(lbl_protocol = new Label());
		fieldPanel.add(lbl_ref = new Label());
		fieldPanel.add(lbl_externalForm = new Label());
		fieldPanel.add(lbl_string = new Label());
	//Add ActionListeners for all buttons
		bu_connect.addActionListener(this);
		bu_microsoft.addActionListener(this);
		bu_sun.addActionListener(this);
	//Arrange Panels
		centerPanel.add("West", labelPanel);
		centerPanel.add("Center", fieldPanel);
		add("North", northPanel);
		add("South", southPanel);
		add("Center", centerPanel);
	}
	public void showWeb(String s) {	//Gets a new URL from a string
		try {
			url_site = null;		//Force garbage collection
			url_site = new URL(s);	//Define a new URL
			webInfo();				//Call webInfo method
		}
		catch (MalformedURLException mue) {
			showStatus ("A MalformedURLException has occurred");
			url_site = null;
		}
	}
	public void webInfo() {					//Displays the web info
		lbl_file.setText(url_site.getFile());				// URL file name
		lbl_host.setText(url_site.getHost());				// URL host name
		lbl_port.setText(String.valueOf(url_site.getPort()));		// URL port
		lbl_protocol.setText(url_site.getProtocol());			// URL protocol
		lbl_ref.setText(url_site.getRef());				// URL reference
		lbl_externalForm.setText(url_site.toExternalForm());	// URL external form
		lbl_string.setText(url_site.toString());			// URL string
	}
//The actionPerformed method is required 
//by the ActionListener interface
	public void actionPerformed(ActionEvent e) {
		// Get the text of the button
		String buttonText = e.getActionCommand();
		//Test for buttons
		if (buttonText.equals("Microsoft")) {
			showWeb("http://www.microsoft.com/visualj");
		}		
		else if (buttonText.equals("Sun")) {
			showWeb("http://www.sun.com/java");
		}
		else if (buttonText.equals("Connect")) {
			showStatus("Connecting to web site...Please wait.");
			getAppletContext().showDocument(url_site);	//Connect to the URL
			showStatus("");
		}		
	}
}

⌨️ 快捷键说明

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