urltest.java

来自「java练习程序」· Java 代码 · 共 92 行

JAVA
92
字号
//例子中,在选择控件里选择了一个网址后,将会打开该网址的网页。这是因为
//    我们把监视器注册到了控件的选项上了。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class URLTest extends Applet implements ItemListener
{
	Choice choice1;TextField text1, text2;
	URL url1, url2, url3, url4, url5, url6;
	String s1, s2, s3, s4, s5, s6;
	public void init ()
	{
		setLayout(new GridLayout(2, 2));
		choice1=new Choice ();
		text1=new TextField(10);
		text2=new TextField(10);
		choice1.add("清华大学");
		choice1.add("大连理工");
		choice1.add("yahoo");
		choice1.add("263");
		add(choice1);
		add(new Label("选择一个网址"));
		add(text1);
		add(text2);
		choice1.addItemListener( this);
		s1 ="http://www.tsinghua.edu.cn";
		s2 ="http://www.dlut.edu.cn";
		s3 ="http://www.yahoo.com";
		s4 ="http://freemail.263.net";
	}

	public void itemStateChanged(ItemEvent e)
	{
		if(e.getItemSelectable()==choice1)
		{
			if(choice1.getSelectedIndex()==0)
			{
				try
				{
					url1=new URL(s1);
				}
				catch(MalformedURLException g)
				{
					System.out.println("不正确的URL:"+url1);
				}
				text1.setText("清华大学");
				getAppletContext().showDocument(url1);
			}
			else if(choice1.getSelectedIndex()==1)
			{
				try
				{
					url2=new URL(s2);
				}
				catch(MalformedURLException g)
				{
					System.out.println("不正确的URL:"+url2);
				}
				text1.setText("大连理工");
				getAppletContext().showDocument(url2);
			}
			else if(choice1.getSelectedIndex()==2)
			{
				try
				{
					url3=new URL(s3);
				}
				catch(MalformedURLException g)
				{
					System.out.println("不正确的URL:"+url3);
				}
				text1.setText("yahoo");
				getAppletContext().showDocument(url3);
			}
			else if(choice1.getSelectedIndex()==3)
			{
				try
				{
					url4=new URL(s4);
				}
				catch(MalformedURLException g)
				{
					System.out.println("不正确的URL:"+url4);
				}
				text1.setText("263");
				getAppletContext().showDocument(url4);
			}
		}
		else {}
	}
}

⌨️ 快捷键说明

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