当前位置:文档之家› 实验5 子类与继承

实验5 子类与继承

{
public void actionPerformed(ActionEvent event)
{
if (urlStack.size() <= 1) return;
try
{
// get URL from back button
urlStack.pop();
// show URL in text field
3.用URL对象作为参数,调用setPage()方法,将HTML文件读到JeditorPane容器中,在该容器中的HTML文件,会被解释执行,即可看到网页的运行效果。
4.窗口中按BorderLayout页面布局各组件。
二、实验题目:
用户在JTextField中输入URL,
按回车或单击访问键(JButton),程序利用JEditorPane来显示网页内容
editorPane.setPage(url.getText());
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
}
};
JButton loadButton = new JButton("Load");
loadButton.addActionListener(listener);
*/
class EditorPaneFrame extends JFrame
{
public EditorPaneFrame()
{
setTitle("EditorPaneTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
final Stack<String> urlStack = new Stack<String>();
frame.setVisible(true);
}
}
/**
This frame contains an editor pane, a text field and button
to enter a URL and load a document, and a Back button to
return to a previously loaded document.
class EditorPaneTest
{
public static void main(String[] args)
{
JFrame frame = new EditorPaneFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener listener = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
// remember URL for back button
urlStack.push(url.getText());
panel.add(url);
panel.add(loadButton);
panel.add(backButton);
panel.add(new JLabel("Editable"));
panel.add(editable);
add(panel, BorderLayout.SOUTH);
}
private static final int DEFAULT_WIDTH = 600;
三、实验步骤(详细的基本操作步骤)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import .*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
final JEditorPane editorPane = new JEditorPane();
final JTextField url = new JTextField(30);
// set up hyperlink listener
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new
{
editorPane.setText("Exception: " & set up checkbox for toggling edit mode
final JCheckBox editable = new JCheckBox();
editable.addActionListener(new
HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent event)
{
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
{
try
{
// remember URL for back button
【JAVA程序设计】实验报告
班级:计12本2学号:1212210222姓名:张浩
实验5子类与继承
实验类型
设计
实验日期
2014-12-12
实验地点
2s-417
实验时间(星期节次)
星期五三四节
指导教师
刘红霞
成绩
一、实验目的及要求:
1.创建URL类的对象,将URL对象指向要获取的网站的HTML文件。
2.定义JEditorPane对象,对象调用setEditable()方法将编辑属性设为false。
}
});
add(new JScrollPane(editorPane), BorderLayout.CENTER);
// put all control components in a panel
JPanel panel = new JPanel();
panel.add(new JLabel("URL"));
private static final int DEFAULT_HEIGHT = 400;
}
四、实验总结:
本次试验让我们对交互界面JTextField、JButton、JEditorPane的基本操作有了初步的了解与掌握,对于URL类的创建也得到了练习。
url.addActionListener(listener);
// set up back button and button action
JButton backButton = new JButton("Back");
backButton.addActionListener(new
ActionListener()
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
editorPane.setEditable(editable.isSelected());
}
});
// set up load button for loading URL
urlStack.push(event.getURL().toString());
// show URL in text field
url.setText(event.getURL().toString());
editorPane.setPage(event.getURL());
}
catch (IOException e)
String urlString = urlStack.peek();
url.setText(urlString);
editorPane.setPage(urlString);
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
相关主题