電卓アプリを作成すると基礎がだいぶ身につくということで、復習しながら作成中です。
やっと、外見を作成完了?
ここまで。
次は処理を考えます。
package swingsam;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.BackingStoreException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calctest extends JFrame implements ActionListener {
public Calctest(String s) {
super(s);
// フレームの構成
this.setSize(320, 240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
// ボタンなど、画面の構成
JPanel pane = new JPanel(new GridLayout(1, 1)); // 結果表示のためのパネル
JTextField display = new JTextField(10); // 結果表示のためのテキストエリア
this.add(pane); //結果表示のためのパネルをフレームに組み込む
pane.add(display);// 結果表示のためのテキストエリアをパネルにはめ込んで見えるようにする
display.setHorizontalAlignment(JTextField.RIGHT);//テキストエリアのカーソルを右側に出す
JPanel pane1 = new JPanel(new GridLayout(5, 4));// ボタン配置のためのパネル
String btnface[] = { "BS", "CL", "AC", "1", "2", "3","+", "4", "5", "6","-",
"7", "8", "9", "/","0" ,"=","+/-","*"}; //ボタンの表示を作る
JLabel dummy = new JLabel();//無記入のラベルをひとつ作る
pane1.add(dummy);//pane1の1番始めのグリッドを空白にするためにひとつ付け足す
JButton btn;
for (int i = 0; i < btnface.length; i++) {
btn = new JButton(btnface[i]);
pane1.add(btn);
}
this.add(pane, BorderLayout.NORTH);
this.add(pane1, BorderLayout.CENTER);
pane.setBackground(new Color(00, 00, 00));
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO 自動生成されたメソッド・スタブ
}
public static void main(String[] args) {
Calctest frame = new Calctest("電卓");
}
}