Eclipseというソフトで電卓を作成しています。
電卓の見た目は作成できたのですが、インスタンス化して四則演算の処理ができません。
インスタンス化の方法はあってますか?
どなたか作成できる方はお力添えをお願いします。
↓電卓の表示
package dentaku;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class GUI extends JFrame implements ActionListener {
public JTextField jt = new JTextField(); public void create_gui(String s, int w, int h) { //メインのフレームを作成 this.setTitle(s); this.setSize(w, h); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(null); //ここからボタンとかを貼る JButton[] jb = new JButton[9]; int button_x_init = 20; int button_y_init = 80; int button_size = 50; int button_space = 10; int button_number = 1; int a = button_x_init; int b = button_y_init; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { jb[i] = new JButton(String.valueOf(button_number)); jb[i].setBounds(a, b, button_size, button_size); this.add(jb[i]);//ボックスの中に入ってる文字 jb[i].addActionListener(this); //ここまでボタンとかを貼る a = a + button_size + button_space; button_number++; } a = button_x_init; b = b + button_size + button_space; } a = button_x_init; JButton button_zero = new JButton("0"); button_zero.setBounds(a, b, button_size + button_size + button_space, button_size); this.add(button_zero); button_zero.addActionListener(this); a += button_size + button_size + button_space + button_space; JButton button_equals = new JButton("="); button_equals.setBounds(a, b, button_size, button_size); this.add(button_equals); button_equals.addActionListener(this); JButton button_c = new JButton("c"); button_c.setBounds(20, 40, button_size + button_size + button_size + button_size + button_space + button_space + button_space, button_space + button_space + button_space); this.add(button_c); button_c.addActionListener(this); jt.setBounds(20, 0, button_size + button_size + button_size + button_size + button_space + button_space + button_space, button_space + button_space + button_space); this.add(jt); this.setVisible(true); a = button_x_init + (button_size * 3) + (button_space * 3); b = button_y_init; JButton button_plus = new JButton("+"); JButton button_subtraction = new JButton("-"); JButton button_multiplication = new JButton("*"); JButton button_division = new JButton("÷"); button_plus.setBounds(a, b, button_size, button_size); b = b + button_size + button_space; button_subtraction.setBounds(a, b, button_size, button_size); b = b + button_size + button_space; button_multiplication.setBounds(a, b, button_size, button_size); b = b + button_size + button_space; button_division.setBounds(a, b, button_size, button_size); this.add(button_plus); this.add(button_subtraction); this.add(button_multiplication); this.add(button_division); button_plus.addActionListener(this); ; button_subtraction.addActionListener(this); button_multiplication.addActionListener(this); this.setVisible(true); } /** * @param args */ public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ //for(int i = 0 ; i < 10000 ; i++){ GUI gui = new GUI(); gui.create_gui("電卓", 400, 500); // } } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); System.out.println(s); }
}
↓インスタンス化
この方法でGUIがきちんとインスタンス化できていますか?
package dentaku;
public class Calculator {
public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ GUI a =new GUI(); a.setVisible(true);
}
}
あなたの回答
tips
プレビュー