等などがある。Javaのクラスライブラリが備えるメソッドには、例外を発生
させるものが多くあり、それらのメソッドを使用する際は、try,catch,finally
文を適切に記述しなければならない。
例外処理の構文
try {
(例外を発生する可能性のある文をここに書く)
}
catch (例外のタイプ2 変数名){
(例外のタイプ2が発生した時に処理する文をここに書く)
}
catch (例外のタイプ2 変数名){
(例外のタイプ2発生した時に処理する文をここに書く)
}
:
:
catch (例外のタイプn 変数名){
(例外のタイプnが発生した時に処理する文をここに書く)
}
finally {
(例外発生の有無にかかあらず必ず実行する文をここに書く)
}
3.上述のサンプルプログラムに出れきたGUI部品を、全て使ったプログラムを作
成せよ。
作成プログラムのソース
import java.awt.*;
import java.awt.event.*;
public class report4 extends Frame{
Button bt1 = new Button("計算開始");
Button bt2 = new Button("CLEAR");
Label l1 = new Label("縦の長さ");
Label l2 = new Label("横の長さ");
Label l3 = new Label("高さ");
Label l4 = new Label("結果");
TextField t1 = new TextField();
TextField t2 = new TextField();
TextField t3 = new TextField();
TextField t4 = new TextField();
public report7(){
setLayout(null);
add(l1); l1.setBounds(20, 50, 60, 30);
add(l2); l2.setBounds(20, 100, 60, 30);
add(l3); l3.setBounds(20, 150, 60, 30);
add(l4); l4.setBounds(20, 250, 60, 30);
add(t1); t1.setBounds(150, 50, 100, 30);
add(t2); t2.setBounds(150, 100, 100, 30);
add(t3); t3.setBounds(150, 150, 100, 30);
add(t4); t4.setBounds(150, 250, 150, 30);
add(bt1);bt1.setBounds( 20, 200, 100, 40);
add(bt2);bt2.setBounds( 140, 200, 100, 40);
bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
try{
double length1 = (new
Double(t1.getText())).doubleValue();
double length2 = (new
Double(t2.getText())).doubleValue();
double length3 = (new
Double(t3.getText())).doubleValue();
double answer = (length1 * length2 * length3);
t4.setText(Double.toString(answer));
}catch(Exception ex){
t4.setText("整数を入れて下さい");
}
}
});
bt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
});
}
public static void main(String[] args) {
Frame win = new report7();
win.setTitle("立体の体積");
win.setSize(350, 300);
win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
考察
- このプログラムはサンプルプログラムで使用されている
Button,Label,TextFieldのコンポーネントを使用している。
- mainメソッドはまずreport4クラスをインスタンス化しコンストラクタを呼
び出す。
- クラスreport4は2つのButtonと4つのLabel、TextFieldのオブジェクトを
作成。
- コンストラクタ内でFrameコンテナに作成した全てのコンポーネントを追加。
- レイアウトマネージャは不使用。
4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
作成プログラムのソース
import java.awt.*;
import java.awt.event.*;
public class report4_onndokannsann extends Frame{
Button b0 = new Button("華氏に変換する");
Button b1 = new Button("摂氏に変換する");
Label x0 = new Label("変換前");
Label x1 = new Label("変換後");
Label x2 = new Label("数字をいれて下さい");
TextField t0 = new TextField();
TextField t1 = new TextField();
public henkan(){
setLayout(null);
add(t0); t0.setBounds(95,65,55,25);
add(b0); b0.setBounds(65,85,155,25);
add(x0); x0.setBounds(45,65,55,25);
add(b1); b1.setBounds(225,85,155,25);
add(t1); t1.setBounds(45,115,55,25);
add(x1); x1.setBounds(40,145,165,25);
add(x2); x2.setBounds(165,145,160,25);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int i = (new Integer(t0.getText())).intValue();
i = (int)Math.rint(1.8*i+32);
t1.setText("" + i);
x2.setText("摂氏" + t0.getText() + "は華氏" + i
);
}catch(Exception ex){
t1.setText("error");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int i = (new Integer(t0.getText())).intValue();
i = (int)Math.rint((i-32)/1.8);
t1.setText("" + i);
x2.setText("華氏"+ t0.getText() +"は摂氏"+ i);
}catch(Exception ex){
t1.setText("error");
}
}
});
}
public static void main(String[] args) {
Frame win = new report7_henkan();
win.setSize(300, 270);
win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
考察
- mainメソッド内はreport4_onndohennkanコンストラクタを呼び出す。
- Frameにタイトルを付け、ウィンドウサイズを設定してFrameコンテナを表示
する。
- 次にウィンドウを閉じる動作を記述している。
- さらに使用するコンポーネントのオブジェクトを作成。
- 使用するコンポーネントのオブジェクトをコンストラクタ内でFraameコンテ
ナに追加しレイアウトマネージャを使わずに配置。
- 次にボタンの処理を記述。
- あとは華氏変換計算と摂氏変換計算を行っている。
5.「電卓」プログラム。中身は自分の思うように。
作成プログラムのソース
import java.awt.*;
import java.awt.event.*;
public class electronic_calculator extends Frame implements ActionListener{
Button button00 = new Button("00");
Button button0 = new Button("0");
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
Button button5 = new Button("5");
Button button6 = new Button("6");
Button button7 = new Button("7");
Button button8 = new Button("8");
Button button9 = new Button("9");
Button btn_AC = new Button("AC");
Button btn_tasu = new Button("+");
Button btn_hiku = new Button("―");
Button btn_kake = new Button("×");
Button btn_waru = new Button("÷");
Button btn_wa = new Button("=");
Label l_display = new Label("", Label.RIGHT);
long num1 = 0;
long num2 = 0;
int enzansi = 0;
// コンストラクタ
public Dentaku() {
final int BW = 80; // ボタンの幅
final int BH = 50; // ボタンの高さ
final int M = 10; // マージン
setLayout(null);
add(l_display);
l_display.setBounds(M, M+20, 400, 52);
l_display.setBackground(Color.gray);
l_display.setFont(new Font("Monospaced",Font.BOLD,42));
add(button0);
button0.setBounds(M+BW*0, M+BH*6, BW,BH);
button0.addActionListener(this);
add(button00);
button00.setBounds(M+BW*1, M+BH*6, BW, BH);
button00.addActionListener(this);
add(button1);
button1.setBounds(M+BW*0, M+BH*5, BW, BH);
button1.addActionListener(this);
add(button2);
button2.setBounds(M+BW*1, M+BH*5, BW, BH);
button2.addActionListener(this);
add(button3);
button3.setBounds(M+BW*2, M+BH*5, BW, BH);
button3.addActionListener(this);
add(button4);
button4.setBounds(M+BW*0, M+BH*4, BW, BH);
button4.addActionListener(this);
add(button5);
button5.setBounds(M+BW*1, M+BH*4, BW, BH);
button5.addActionListener(this);
add(button6);
button6.setBounds(M+BW*2, M+BH*4, BW, BH);
button6.addActionListener(this);
add(button7);
button7.setBounds(M+BW*0, M+BH*3, BW, BH);
button7.addActionListener(this);
add(button8);
button8.setBounds(M+BW*1, M+BH*3, BW, BH);
button8.addActionListener(this);
add(button9);
button9.setBounds(M+BW*2, M+BH*3, BW, BH);
button9.addActionListener(this);
add(btn_back);
btn_back.setBounds(M+BW*0, M+BH*2, BW*2, BH);
btn_back.addActionListener(this);
add(btn_AC);
btn_AC.setBounds(M+BW*2, M+BH*2, BW, BH);
btn_AC.addActionListener(this);
add(btn_tasu);
btn_tasu.setBounds(M+BW*3, M+BH*4, BW, BH*2);
btn_tasu.addActionListener(this);
add(btn_hiku);
btn_hiku.setBounds(M+BW*3, M+BH*2, BW, BH*2);
btn_hiku.addActionListener(this);
add(btn_kake);
btn_kake.setBounds(M+BW*4, M+BH*4, BW, BH*2);
btn_kake.addActionListener(this);
add(btn_waru);
btn_waru.setBounds(M+BW*4, M+BH*2, BW, BH*2);
btn_waru.addActionListener(this);
add(btn_wa);
btn_wa.setBounds(M+BW*3, M+BH*6, BW*2, BH);
btn_wa.addActionListener(this);
}
// ActionListener インタフェースの実装
public void actionPerformed(ActionEvent e) {
if (num1 >= 100000000000000L) { num1 /= 10; } // 限界値の設定
if (e.getSource() == button0) { num1 *= 10; }
if (e.getSource() == button00) { num1 *= 100; }
if (e.getSource() == button1) { num1 = num1 * 10 + 1; }
if (e.getSource() == button2) { num1 = num1 * 10 + 2; }
if (e.getSource() == button3) { num1 = num1 * 10 + 3; }
if (e.getSource() == button4) { num1 = num1 * 10 + 4; }
if (e.getSource() == button5) { num1 = num1 * 10 + 5; }
if (e.getSource() == button6) { num1 = num1 * 10 + 6; }
if (e.getSource() == button7) { num1 = num1 * 10 + 7; }
if (e.getSource() == button8) { num1 = num1 * 10 + 8; }
if (e.getSource() == button9) { num1 = num1 * 10 + 9; }
if (e.getSource() == btn_back) { num1 /= 10; }
if (e.getSource() == btn_AC) { num1 = 0; num2 = 0; enzansi =
0;}
l_display.setText("" + num1); // 入力途中の数字を画面に出
力
if (e.getSource() == btn_tasu) { keisan(); enzansi = 1; }
if (e.getSource() == btn_hiku) { keisan(); enzansi = 2; }
if (e.getSource() == btn_kake) { keisan(); enzansi = 3; }
if (e.getSource() == btn_waru) { keisan(); enzansi = 4; }
if (e.getSource() == btn_wa) { keisan(); enzansi = 0; }
}
// 演算子の種類を判別して計算をするメソッド
public void keisan() {
if (enzansi == 1) { num1 = num2 + num1; }
else if (enzansi == 2) { num1 = num2 - num1; }
else if (enzansi == 3) { num1 = num2 * num1; }
else if (enzansi == 4) { num1 = num2 / num1; }
l_display.setText("" + num1);
num2 = num1;
num1 = 0;
}
// mainメソッド
public static void main(String[] args) {
Frame win = new Dentaku();
win.setSize(420,360);
win.setBackground(Color.black);
win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
}
}