1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
|
//<applet code="DecToBinApplet" width="200" height="40"></applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DecToBinApplet
extends Applet implements ActionListener{
private Button btTrance; //変換ボタン
private TextField txtDec; //10進数の入力欄
private Label lblBin; //2進数の表示
private Panel pl; //パネル
public void init(){
txtDec = new TextField();
lblBin = new Label("");
btTrance = new Button("実行");
pl = new Panel();
setBackground(new Color(255,255,255));
//レイアウトの設定
setLayout(null);
pl.setLayout(null);
//座標の設定
txtDec.setBounds(0, 0, 100, 20);
btTrance.setBounds (100, 0, 100, 20);
lblBin.setBounds (0, 20, 200, 20);
pl.setBounds(0, 0 ,500, 100);
//パネルにコントロールを配置
pl.add(txtDec);
pl.add(btTrance);
pl.add(lblBin);
add(pl); //パネルをアプレットに配置
btTrance.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == btTrance){
//変数宣言
int[] nisin = new int[16];
//入力
int jyu=Integer.parseInt(txtDec.getText());
//変換
for(int i=0; i<16; i++){
nisin[i] = jyu % 2;
jyu = jyu / 2;
}
//出力
lblBin.setText("");
for(int i=16-1; i>=0; i--){
lblBin.setText(lblBin.getText() + nisin[i]);
}
}
}
}
|