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

📄 appletbrowser.java.bak

📁 Java就业的培训教程书籍
💻 BAK
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
/**类AppletBrowser是Applet的子类,并实现了ActionListener接口*/
public class AppletBrowser extends Applet implements ActionListener{
	TextField tfObj;
	Choice choiceObj;
	/**方法init()用于布置组件的大小和位置*/
	public void init(){
		 GridBagLayout gridBag = new GridBagLayout();
		 GridBagConstraints c = new GridBagConstraints();
		 setLayout(gridBag);
		 
		 Label labelObj1 = new Label("需要显示对象的URL:",Label.RIGHT);
		 gridBag.setConstraints(labelObj1,c);
		 add(labelObj1);
		 //tfObj用于用户输入需要显示的URL对象
		 tfObj = new TextField("http://www.tsinghua.edu.cn/",35);
		 tfObj.addActionListener(this);
		 c.gridwidth = GridBagConstraints.REMAINDER;
		 c.fill = GridBagConstraints.HORIZONTAL;
		 c.weightx = 1.0;
		 gridBag.setConstraints(tfObj,c);
		 add(tfObj);
		 
		 Label labelObj2 = new Label("Window/frame to show it in:",Label.RIGHT);
		 
		 c.gridwidth = 1;
		 c.weightx   = 0.0;
		 gridBag.setConstraints(labelObj2,c);
		 add(labelObj2);
		 /*choiceObj对象用于选择显示URL对象的窗口类型*/
		 choiceObj = new Choice();
		 choiceObj.addItem("(选择浏览器窗口类型)");
		 choiceObj.addItem("_blank");
		 choiceObj.addItem("_self");
		 choiceObj.addItem("_parent");
		 choiceObj.addItem("_top");
		 c.fill = GridBagConstraints.NONE;
		 c.gridwidth = GridBagConstraints.REMAINDER;
		 c.anchor = GridBagConstraints.WEST;
		 gridBag.setConstraints(choiceObj,c);
		 add(choiceObj);
		 /*按钮button用于执行页面的显示操作*/
		 Button button = new Button("显示页面");
		 button.addActionListener(this);
		 c.weighty = 1.0;
		 c.ipadx = 10;
		 c.ipady = 10;
		 c.insets =new Insets(5,0,0,0);
		 c.anchor = GridBagConstraints.SOUTH;
		 gridBag.setConstraints(button,c);
		 add(button);
	}
	/**方法actionPerformed用于处理界面事件*/
	public void actionPerformed(ActionEvent event){
		String urlString = tfObj.getText();
		URL url = null;
		
		try{
			url = new URL(urlString);
		}catch(MalformedURLException e){
			System.err.println("Malformed URL:"+urlString);
		}
		/*下面的语句中通过showDocument方法显示url对象对应的页面*/
		if(url != null){
			if(choiceObj.getSelectedIndex()==0){
				getAppletContext().showDocument(url);
			}else{
				getAppletContext().showDocument(url,choiceObj.getSelectedItem());
			}
		}
	}
}

⌨️ 快捷键说明

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