【実行画面キャプチャ】↓




【コード】↓
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
public class HTMLViewer implements ActionListener, HyperlinkListener{
JTextField addrField;
JEditorPane htmlPane;
JButton home;
JButton sfc;
JButton kobayashi;
public HTMLViewer(){
JFrame frame = new JFrame("Simple HTML Viewer");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(
new WindowAdapter() {
public void windowClosed(WindowEvent e){
System.exit(0);
}
}
);
addrField = new JTextField("http://");
addrField.addActionListener(this);
htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.setContentType("text/html");
htmlPane.addHyperlinkListener(this);
home = new JButton("HOME");
home.addActionListener(new HomeActionListener());
sfc = new JButton("SFC");
sfc.addActionListener(new sfcActionListener());
kobayashi = new JButton("Kobayashi");
kobayashi.addActionListener(new kobayashiActionListener());
JPanel topContainer = new JPanel();
topContainer.setLayout(new GridLayout(1,3));
topContainer.add(home);
topContainer.add(sfc);
topContainer.add(kobayashi);
JPanel bottomContainer = new JPanel();
bottomContainer.setLayout(new BorderLayout());
bottomContainer.add(addrField, BorderLayout.NORTH);
bottomContainer.add(new JScrollPane(htmlPane), BorderLayout.CENTER);
frame.getContentPane().add(topContainer, BorderLayout.NORTH );
frame.getContentPane().add(bottomContainer, BorderLayout.CENTER );
frame.setSize(640,480);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String url = addrField.getText();
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("無効なURLを指定していませんか?");
}
}
public class HomeActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String url = "http://yahoo.co.jp/";
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("ページジャンプに失敗しました。");
}
}
}
public class sfcActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String url = "http://www.sfc.keio.ac.jp/";
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("ページジャンプに失敗しました。");
}
}
}
public class kobayashiActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String url = "http://blog.ameba.jp/ucs/entry/srventrylist.do";
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("ページジャンプに失敗しました。");
}
}
}
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType() != HyperlinkEvent.EventType.ACTIVATED){
return;
}
String url = e.getURL().toString();
addrField.setText(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
}
}
public static void main(String[] args){
HTMLViewer my_viewer = new HTMLViewer();
}
}
【解説】↓
●インポートデータ
まず、ActionListenerのインポート、JFrameのインポート、HyperlinkListenerのインポート、JButtonのインポートを行う。
●HTMLViewerクラス
URLを入力してもらう部品、HTMLを表示してもらう部品、ホームボタン部品、SFCボタン部品、kobayashiボタン部品をつくる。
部品としてのウィンドウを作成:newする JFrameという部品のコンストラクタを呼び出す。
JFrameのオブジェクトを作成する。
●URL入力フォームについて
テキストフィールドオブジェクトの作成。
●Webページ表示画面について
JEditorPaneオブジェクトを作成。クリックされたら知らせるよう設定。
●ホームボタンとSFCボタンの作成と、ボタンを押した時に呼び出す仕事の設定
home,sfc,kobayashi分のJButtonオブジェクトの作成。
●オブジェクトの描画、レイアウトなど
ウィンドウのサイズや位置を設定する。
●URLを入力し、エンターキーが押されたら実行する仕事
エラー時には「無効なURLを指定していませんか?」と表示させる。
●ホームボタン、SFCボタンそれぞれが押されたら実行される仕事について
エラー時には「ページジャンプに失敗しました」と表示させる。
●Webページ内のリンクについて
どのリンクがクリックされたかを取得し、取得した文字列をurl変数に格納する。
●プログラムの実行処理
プログラムを実行させる。
【クラスとインスタンスの概念】
クラスとは物事の不変の形を表す概念であり、例えるなら「人間」「犬」「果物」等が挙げられる。一方インスタンスはクラスよりも詳細を表し、不変ではないもの、クラスに対応する例えを挙げるなら「ポチ」「太郎」「りんご」のようなものが挙げられる。
【イベントとイベントリスナーの概念】
イベントとは、ユーザーが操作することで発生する何かであり、種類がたくさんある。イベントリスナーとはイベント処理の機能を持つインターフェイスクラスであり、ActionListenerもその一つである。




【コード】↓
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
public class HTMLViewer implements ActionListener, HyperlinkListener{
JTextField addrField;
JEditorPane htmlPane;
JButton home;
JButton sfc;
JButton kobayashi;
public HTMLViewer(){
JFrame frame = new JFrame("Simple HTML Viewer");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(
new WindowAdapter() {
public void windowClosed(WindowEvent e){
System.exit(0);
}
}
);
addrField = new JTextField("http://");
addrField.addActionListener(this);
htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.setContentType("text/html");
htmlPane.addHyperlinkListener(this);
home = new JButton("HOME");
home.addActionListener(new HomeActionListener());
sfc = new JButton("SFC");
sfc.addActionListener(new sfcActionListener());
kobayashi = new JButton("Kobayashi");
kobayashi.addActionListener(new kobayashiActionListener());
JPanel topContainer = new JPanel();
topContainer.setLayout(new GridLayout(1,3));
topContainer.add(home);
topContainer.add(sfc);
topContainer.add(kobayashi);
JPanel bottomContainer = new JPanel();
bottomContainer.setLayout(new BorderLayout());
bottomContainer.add(addrField, BorderLayout.NORTH);
bottomContainer.add(new JScrollPane(htmlPane), BorderLayout.CENTER);
frame.getContentPane().add(topContainer, BorderLayout.NORTH );
frame.getContentPane().add(bottomContainer, BorderLayout.CENTER );
frame.setSize(640,480);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String url = addrField.getText();
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("無効なURLを指定していませんか?");
}
}
public class HomeActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String url = "http://yahoo.co.jp/";
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("ページジャンプに失敗しました。");
}
}
}
public class sfcActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String url = "http://www.sfc.keio.ac.jp/";
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("ページジャンプに失敗しました。");
}
}
}
public class kobayashiActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String url = "http://blog.ameba.jp/ucs/entry/srventrylist.do";
System.out.println(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
System.out.println("ページジャンプに失敗しました。");
}
}
}
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType() != HyperlinkEvent.EventType.ACTIVATED){
return;
}
String url = e.getURL().toString();
addrField.setText(url);
try{
htmlPane.setPage(url);
}catch(Exception err){
}
}
public static void main(String[] args){
HTMLViewer my_viewer = new HTMLViewer();
}
}
【解説】↓
●インポートデータ
まず、ActionListenerのインポート、JFrameのインポート、HyperlinkListenerのインポート、JButtonのインポートを行う。
●HTMLViewerクラス
URLを入力してもらう部品、HTMLを表示してもらう部品、ホームボタン部品、SFCボタン部品、kobayashiボタン部品をつくる。
部品としてのウィンドウを作成:newする JFrameという部品のコンストラクタを呼び出す。
JFrameのオブジェクトを作成する。
●URL入力フォームについて
テキストフィールドオブジェクトの作成。
●Webページ表示画面について
JEditorPaneオブジェクトを作成。クリックされたら知らせるよう設定。
●ホームボタンとSFCボタンの作成と、ボタンを押した時に呼び出す仕事の設定
home,sfc,kobayashi分のJButtonオブジェクトの作成。
●オブジェクトの描画、レイアウトなど
ウィンドウのサイズや位置を設定する。
●URLを入力し、エンターキーが押されたら実行する仕事
エラー時には「無効なURLを指定していませんか?」と表示させる。
●ホームボタン、SFCボタンそれぞれが押されたら実行される仕事について
エラー時には「ページジャンプに失敗しました」と表示させる。
●Webページ内のリンクについて
どのリンクがクリックされたかを取得し、取得した文字列をurl変数に格納する。
●プログラムの実行処理
プログラムを実行させる。
【クラスとインスタンスの概念】
クラスとは物事の不変の形を表す概念であり、例えるなら「人間」「犬」「果物」等が挙げられる。一方インスタンスはクラスよりも詳細を表し、不変ではないもの、クラスに対応する例えを挙げるなら「ポチ」「太郎」「りんご」のようなものが挙げられる。
【イベントとイベントリスナーの概念】
イベントとは、ユーザーが操作することで発生する何かであり、種類がたくさんある。イベントリスナーとはイベント処理の機能を持つインターフェイスクラスであり、ActionListenerもその一つである。