[戻る]

f421---前のframe42に『閉じるボックス』のプログラミングを追加

/*
    
    
   */

import java.awt.*;
import java.awt.event.*;

public class frame421 extends Frame  implements WindowListener{
	
	public static void main(String argv[]){
		frame421 p = new frame421();
		p.init();	//アプレットのように自動実行するものでなく、単なるメソッドなので呼び出し記述が必要
		p.show();	//表示
		p.setSize(250,100);//サイズ変更
		p.addWindowListener(p);
	}

	public void init() {
		setBackground(new Color(100,100,200));
		setLayout(new FlowLayout());//レイアウト変更
		add(new Button("button1"));
		add(new Button("button2"));
		add(new Button("button3"));
		add(new Button("button4"));
		add(new Button("button5"));
	}
	public void windowOpened(WindowEvent e) {	//ウィンドウが最初に可視になった
		System.out.println(e.toString());
	}
	
	public void windowClosed(WindowEvent e) { //ウィンドウがクローズされた
		System.out.println(e.toString());
	}
	
	public void windowClosing(WindowEvent e) {//ユーザが、ステムメニュー(クローズボックスなど)でウィンドウを閉じた 
		System.out.println(e.toString());
		//this.dispose();	//すべてのネイティブスクリーンリソースを解放(ウインドウが無くなる)
		System.exit(0);//仮想マシンを終了
	}
	public void windowIconified(WindowEvent e) {//ウィンドウが最小化された
		System.out.println(e.toString());
	}
	public void windowDeiconified(WindowEvent e) {//他の状態から通常の状態になった
		System.out.println(e.toString());
	}
	public void windowActivated(WindowEvent e) {//Window がアクティブ化 
		System.out.println(e.toString());
	}
	public void windowDeactivated(WindowEvent e) { //Window がアクティブでなくなった 
		System.out.println(e.toString());
	}

}




[戻る]

f07---applet071.をFrameプログラムに変更する。

//フレームを生成するだけのアプレット
applet0711.html

import java.applet.*;
public class applet0711 extends Applet {
	
	public void init(){
		new frame0711();
	}
}
──────────────────────────
import java.awt.*; //Graphics使用
import java.awt.event.*;//WindowListenerなどの使用のため

public class frame071 extends Frame implements  GUIOperator,WindowListener{
	TextField txtName;	//名前入力用
	Label lbl_idx;
	
	Aggregation 集合体 = new Aggregation();	//GUIOperatorを使う集合体
	
	class MemoMember0 {//個人を記憶する
		String name;	//名前
	}

	public static void main(String argv[]){
		frame071 p = new frame071();//インスタンス生成
		p.setSize(430,70);
		p.setLayout(new FlowLayout()); //
		p.init();
		p.show();
		p.setTitle("カードデータベースの骨組み");
		
		p.addWindowListener(p);//リスナー登録
	}
	
	public void set_idx(int i){	//表示中データをi番目へ記憶する命令
		MemoMember0 obj = new MemoMember0();
		obj.name = txtName.getText();
		集合体.v.set(i,obj);//iの位置にある要素を、objで置き換え!
	}

	public void get_idx(int i){	//i番目の記憶内容を表示する命令
		MemoMember0 obj = (MemoMember0) 集合体.v.get(i);
		if(obj != null){
			txtName.setText(obj.name);
		} else {
			txtName.setText("xxxx");
		}
		lbl_idx.setText("idx:"+ i);
	}

	public void init(){//アプレットがロードされた時に呼ばれる。
		Button btnBack,btnNext;
		setBackground(new Color(200,200,200));
		add(btnBack = new Button("←戻る"));
		add(txtName = new TextField("xxxx",20));
		add( lbl_idx = new Label("idx: 0"));
		add(btnNext = new Button("次へ→"));
		
		btnNext.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
						集合体.goNext();
				}
		});
		btnBack.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
						集合体.goBack();
				}
		});
		
		集合体.addOperator(this);// addActionListenerに対応する
	}
	public void windowOpened(WindowEvent e){ System.out.println(e.toString());}
	public void windowClosing(WindowEvent e){ 
		System.out.println(e.toString());
		System.exit(0);
	}
	public void windowClosed(WindowEvent e){ System.out.println(e.toString());}
	public void windowIconified(WindowEvent e){ System.out.println(e.toString());}
	public void windowDeiconified(WindowEvent e){ System.out.println(e.toString());}
	public void windowActivated(WindowEvent e){ System.out.println(e.toString());}
	public void windowDeactivated(WindowEvent e){ System.out.println(e.toString());}
}


[戻る]

frame54---URLClassLoaderでクラスをロードし、mainメソッドを実行させる。

//sun.applet.AppletClassLoader アプリケーションクラスのロードには非公開の AppClassLoader が使われている
import java.awt.*;
import java.awt.event.*;//ActionListener利用
import java.applet.*;
import java.net.*; //URL使用のため

public class frame54 extends Frame implements ActionListener ,WindowListener {
	Button btn;			//ボタン
	String parm;
	
	public static void main(String arg[]){
		frame54 frm = new frame54();
	}
	
	public frame54(){
		init();
		pack();
		show();
	}
	
	public void init(){//アプレットがロードされた時に呼ばれる。
		add(btn = new Button("frame51の実行"));
		btn.addActionListener(this);
		this.addWindowListener( this);
	}
	public void actionPerformed(ActionEvent e){ //ボタン処理
		java.lang.reflect.Method methods[];
		java.lang.reflect.Method main_method  = null;
		try {
			URL urls[] = new URL[1];
			urls[0] = new URL("http://192.168.0.10/so-net_same/");
			
			URLClassLoader urlLoader = new URLClassLoader(urls);//←appletでは不可
			Class cls = urlLoader.loadClass("frame54");
			//Class cls =  Class.forName("frame54"); //←の代わりにURLClassLoaderでクラスを読み取る。


			methods = cls.getDeclaredMethods();
			
			System.out.println("メソッド一覧(" + methods.length + "個)" );
			for(int i = 0; i < methods.length ; i++){
				if(methods[i].getName().equals("main")) {
					main_method = methods[i];
				}
				System.out.println("\t" + methods[i].toString());
			}
			Object args[] = new Object[1];
			args[0] = null;
			main_method.invoke(null,args);

		} catch (Exception err) {
			System.err.println("Error " + err.toString());
		}
	}
	
	public void windowOpened(WindowEvent e) {	//ウィンドウが最初に可視になった
	}
	
	public void windowClosed(WindowEvent e) { //ウィンドウがクローズされた
	}
	public void windowClosing(WindowEvent e) {//ユーザが、ステムメニュー(クローズボックスなど)でウィンドウを閉じた 
		dispose();
	}
	public void windowIconified(WindowEvent e) {//ウィンドウが最小化された
	}
	public void windowDeiconified(WindowEvent e) {//他の状態から通常の状態になった
	}
	public void windowActivated(WindowEvent e) {//Window がアクティブ化 
	}
	public void windowDeactivated(WindowEvent e) { //Window がアクティブでなくなった 
	}
}


[戻る]

m281---アプレットとJavaの通常アプリケーション両方で扱えるクラスframe281を作り、それをmain281から呼び出す。

//アプレットとJavaの通常アプリケーション両方で扱えるクラスframe281を作り、それをmain281から呼び出す。
// frame281のクラスが先行して作られている必要がある。
/*マニフェストファイルからmain281.jarファイルを作成し、単独のダブルクリック起動アプリも作成してみる。
●main281.txtの内容
Main-Class: frame32
Class-Path: .
●以下のコマンドで作成
%JAVA_HOME%\bin\jar cmvf main281.txt main281.jar frame281.class
*/右クリックメニューよりmain281.jarをダウンロード

public class main281  {
	
	public static void main(String argv[]){
		new frame281();
	}
}


[戻る]

m29---IPアドレス表示用パネル(IpPanel)利用例(applet29をフレームで利用した例)

/*
    
    
   
// IPアドレス表示用パネル(IpPanel)利用例(applet29をフレームで利用した例)
/*マニフェストファイルからmain281.jarファイルを作成し、単独のダブルクリック起動アプリも作成してみる。
●main281.txtの内容
Main-Class: frame29
Class-Path: .
●以下のコマンドで作成(IpPanelのクラスが先行して作られている必要がある。)
%JAVA_HOME%\bin\jar cmvf frame29.txt frame29.jar frame29.class IpPanel.class
 frame29.jarダウンロード
*/
import java.awt.*;
import java.net.*;//InetAddress利用のため
import java.awt.event.*; //ActionListener利用のため

public class frame29 extends Frame {
	static frame29 frm = null;
	public static void main(String argv[]){
		if(frame29.frm == null){
			frame29.frm = new frame29();
		}
		frame29.frm.setSize(640,100);
		IpPanel panel = new IpPanel();
		frame29.frm.add(panel,BorderLayout.CENTER);
		Button btn = new Button("終了");
		frame29.frm.add(btn,BorderLayout.SOUTH);
		frame29.frm.show();
		btn.addActionListener( new ActionListener(){	//ActionLintener継承の無名クラス生成
			public void actionPerformed(ActionEvent e ){
				frame29.frm.dispose();
				System.exit(0);
			}
		});
		try {
			InetAddress local = InetAddress.getLocalHost(); 
			panel.setAddress(local);
		} 
		catch (UnknownHostException e){
			panel.hostName.setText(e.toString());
		}
	}
}




[戻る]

m01---ファイル操作の基本  バイナリーファイル出力操作 (FileOutputStreamで出力)

// "ABCDEFG\r\nXYZ\r\n12345\r\n"が記憶される文字列(String型)をdata01.txtのファイルに出力する
//  ここで使用するFileOutputStreamは、OutputStreamクラスの継承クラスである
// OutputStream
//    ↑
// FileOutputStream
import java.io.*;

public class main01 {
	public static void main(String argv[]){
		
		// FileOutputStreamはOutputStreamを継承した、ファイル書き込み操作の基本クラスである。
		// 指定のバイト列を書き込む機能になっている。
		try {
			FileOutputStream file = new FileOutputStream("data01.txt");//成功すればファイルが作成される。
			String s = "ABCDEFG\r\nXYZ\r\n12345\r\n"; //ファイル書き込み対象
			
			byte ba[] = s.getBytes();//書き込む文字列からバイト列を取得。
			
			file.write(ba); //バイト列書き込み
			file.close();	//ファイルを閉じる
		}
		catch(Exception e){
			System.out.println(e.toString());//エラー表示
		}
	}
}

[戻る]

m011--- ファイル操作の基本 バイナリーファイル出力操作2 charset 指定でJISコードで出力

// "ABCDEFG\r\nXYZ\r\n12345\r\n"が記憶される文字列(String型)をdata01.txtのファイルに出力する

import java.io.*;

public class main01 {
	public static void main(String argv[]){
		
		// OutputStreamWriterはFileOutputStream を使って生成することで、ファイル操作をキャラクタ単位にできる。
		
		try {
			FileOutputStream file = new FileOutputStream("data01.txt");//成功すればファイルが作成される。
			OutputStreamWriter w = OutputStreamWriter(file,"Shift_JIS"); 
			
			String s = "ABCDEFG\r\nXYZ\r\n12345\r\n"; //ファイル書き込み対象
			
			byte ba[] = s.getBytes();//書き込む文字列からバイト列を取得。
			
			file.write(ba); //バイト列書き込み
			file.close();	//ファイルを閉じる
		}
		catch(Exception e){
			System.out.println(e.toString());//エラー表示
		}
	}
}

[戻る]

m02 ---ファイル操作の基本 バイナリーファイル入力操作 (FileInputStreamを使用)

// InputStream
//    ↑
// FileInputStream
import java.io.*;

public class main02 {
	public static void main(String argv[]){
		
		// FileInputStreamはInputStreamを継承した、ファイル書き込み操作の基本クラスである。
		// 指定のバイト列を書き込む機能になっている。
		try {
			FileInputStream file = new FileInputStream("data01.txt");//成功すればファイル読める
			byte ba[] = new byte[100];
			String s;
			int i = 0;
			while((ba[i] = (byte)file.read()) != -1){
				System.out.println(i + "番目入力データ(16進):" + Integer.toString(ba[i],16));
				if(ba[i] == '\n' && ba[i-1] == '\r'){
					s = new String(ba,0,i-2);	// 配列の0からi個の内容に対応する文字列を生成
					System.out.println("文字列『"  + s + "』");
					i = 0;//先頭から記憶し直す。
				} else {
					i++;
				}
			}
			file.close();
		}
		catch(Exception e){
			System.out.println(e.toString());//エラー表示
		}
	}
}

[戻る]

m03--- キャラクタ単位のファイル操作例(InputStreamReaderと、OutputStreamWriter)

// 指定のhtmlの
タグ個数を数え、別途指定の生成htmlにコピーする。その時、
直前に行番号の数を埋め込む。 import java.io.*; import java.lang.*; //Characterクラスメソッド使用 public class main03 { public static void main(String argv[]){ try { FileInputStream fis = new FileInputStream("allclasses-frame.html");//成功すればファイル読める InputStreamReader reader = new InputStreamReader(fis);//キャラクタ単位に処理するストリーム生成 FileOutputStream fos = new FileOutputStream("クラスの数.html");//成功すれば書ける OutputStreamWriter writer = new OutputStreamWriter(fos);//キャラクタ単位に処理するストリーム生成 String br = "<BR>";//htmlの改行タブ識別用文字列 char c[] = new char[4]; //上記文字列を探すので、最大この文字数分のバッファリングを行う。 int idx = 0; //上記の記憶位置 int count = 0; //行カウント用 while(reader.ready()){//ファイルから入力できる間の繰り返し c[idx] = (char) reader.read();//1文字分入力 System.out.print(c[idx]); if(Character.toUpperCase(c[idx]) == br.charAt(idx)){ //改行タグと一致が続く状態か? idx++; } else { writer.write(c,0,idx+1); //一致しないなら、バッファの内容を書き出す。 idx = 0; } if(idx == 4){//改行タグと一致した。 idx = 0; count++; //数字幅を5桁にし空欄を0で埋めるオブジェクト生成 java.text.DecimalFormat frm = new java.text.DecimalFormat("00000"); String s = "  " + frm.format(count) + "<br>\r\n"; System.out.print(s); writer.write(s,0,s.length());//書き込み } } reader.close(); writer.close(); } catch(Exception e){ System.out.println(e.toString());//エラー表示 } } }

[戻る]

m04--- 行単位のファイル操作 BufferedReaderとBufferedWriterを使う。

//行単位で読み込み、その行に記憶される最初のコンマで分割した文字列を順次記憶する。
//ファイル全体をVectorに記憶後に、記憶した内容を順番にファイルに書き出す。
//ただし行の中で、読み込んだ時の順番と逆に並びになるようにコンマで区切った文字列にしてから書き出す。
// 行単位のファイル操作例
import java.io.*;
import java.util.*;
public class main04 {
	public static void main(String argv[]){
		
		try {
			FileInputStream fis = new FileInputStream("test.txt");//成功すればファイル読める
			InputStreamReader reader = new InputStreamReader(fis);//キャラクタ単位に処理するストリーム生成
			BufferedReader buffread = new BufferedReader(reader);//バッファリングストリーム生成
			Vector v = new Vector();
			while(buffread.ready()){
				String s = buffread.readLine();//1行読み込み
				System.out.println(s); //確認
				int i = s.indexOf(','); //コンマがある所の位置を取得
				if(i != -1) { //コンマが存在する
					v.add(s.substring(0,i));	//コンマの前の文字列を記憶
					v.add(s.substring(i+1)); //コンマ後ろの文字列を記憶
				} else {//コンマが見つからない
					v.add(s);	
					v.add("");	//長さが0の文字列
				}
			}
			buffread.close();
			
			System.out.println("------ここより出力-------");
			FileOutputStream fos = new FileOutputStream("test.txt");//成功すれば書ける
			OutputStreamWriter writer = new OutputStreamWriter(fos);//キャラクタ単位に処理するストリーム生成
			BufferedWriter buffwrite = new  BufferedWriter(writer);//バッファリングストリーム生成
			for(int i= 0; i < v.size(); i+=2){
				String s1 = (String) v.get(i);
				String s2 = (String) v.get(i+1);
				String s = s2 + ',' + s1;
				System.out.println(s); //確認
				buffwrite.write(s);//1行書き込み
				buffwrite.newLine(); //改行
			}
			buffwrite.close();	
		}
		catch(Exception e){
			System.out.println(e.toString());//エラー表示
		}
	}
}

[戻る]

m41---ファイル書き込み操作にprintlnが使えるPrintWriterクラスを利用する。

import java.io.*;
public class main041 {
	public static void main(String argv[]){
		try {
			String path = "testHello.html";
			FileOutputStream fos = new FileOutputStream(path);//成功すればファイルに書ける。
			PrintWriter writer = new  PrintWriter(fos);//バッファリングストリーム生成
			writer.println("<html><body>");
			writer.println("<p> Hello World </p>");
			writer.println("</body></html>");
			writer.close();
		}
		catch(Exception e){
			System.err.println(e.toString());
		}
	}
}


[戻る]

---m05 2次配列の生成方法と確認。

public class main05 {
	public static void main(String argv[]){
		// C言語の a[5][8] のイメージを作るプログラム
		String a[][];
		a = new String[5][];
		System.out.println("a.length:" + a.length);
		for(int i = 0; i < a.length; i++){
			a[i] = new String[8];
			System.out.println("a[" + i + "].length:" + a[i].length);
			for(int k = 0; k < a[i].length; k++){
				a[i][k] = "" + i + k;
			}
		}
		for(int i = 0; i < a.length; i++){
			for(int k = 0; k < a[i].length; k++){
				System.out.print(a[i][k] + " ");
			}
			System.out.println();
		}
	}
}

[戻る]

CGIと通信するプログラム


//以下は、Text1 と、Check1 という名前のフォームタグ名の値に、それぞれ“あいう”と“ABC”を設定し、POSTメソッドで送った場合に対する応答のHTML を表示するプログラムである。
import java.net.*;
import java.io.*;

class Test {
	public static void main(String argv[]){
		try {
			String proxy = "";	//"192.168.253.3"
			String strUrl = "http://ysuzuki7.hp.infoseek.co.jp/cgi-bin/w_test4.cgi";//CGI
			java.net.URL url;
			if(proxy.equals("")){
				// プロキシを使わない場合
				url = new java.net.URL(strUrl);
			} else {
				// プロキシを使う場合
				url = new java.net.URL("http", proxy, 80, strUrl);
			}

			String sndData = "Text1=" + java.net.URLEncoder.encode("あいう","Shift_JIS");//送信する文字列を設定
			sndData += "&Check1=" + java.net.URLEncoder.encode("ABC","Shift_JIS");//送信する文字列を設定

			java.net.URLConnection conn = url.openConnection();
			conn.setDoOutput(true);

			OutputStream os = conn.getOutputStream();
			os.write(sndData.getBytes());
			os.flush();
			
			InputStream is = conn.getInputStream();//バイト入力ストリーム;
			InputStreamReader isr = new InputStreamReader(is);//バイトストリームから文字ストリーム
			BufferedReader reader = new BufferedReader(isr);//バッファリング読み取り用

			while(reader.ready()){ //読み取り可能の間
				String s = reader.readLine();//一行読み取る。
				System.out.println(s);
			}
			is.close();
			os.close();
		}
		catch(Exception e){
			System.out.println(e.toString());
		}
	}
}

[戻る]

---

---
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送