Witam, w celu zapoznania i poćwiczenia trochę Javy zabrałem się za stworzenie kalkulatora, ale utknąłem i nie wiem jak teraz sprawić żeby działało :D. Mam ramkę i wszystkie potrzebne guziki na swoich miejscach, pole tekstowe też jest, jedyne co mi brakuje to wykonywanie akcji po wciśnięciu guzika :/. Przejrzałem sporo tutoriali i gotowych kalkulatorów ale nie potrafię sobie z tym poradzić, może jestem ułomny, ale potrzebuje pomocy :).
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class moje implements ActionListener{
JFrame f;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bd,bo,bdz,bm,bp,bpi,bw,C;
JTextField p;
public static void main(String[] args)
{
moje kalkulator = new moje();
kalkulator.Kamehame();
}
public void Kamehame(){
//Dodawanie obiektów
f=new JFrame("Kalkulator");
p=new JTextField(" ");
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bd=new JButton("+");
bo=new JButton("-");
bdz=new JButton("/");
bm=new JButton("*");
bp=new JButton("^");
bpi=new JButton("\u221a");
bw=new JButton("=");
C=new JButton("C");
//Ustawienia okna
f.setLayout(null);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setVisible(true);
f.setBounds( 70, 70, 275, 440);
f.setTitle( "Kalkulator" );
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b0);
f.add(bd);
f.add(bo);
f.add(bdz);
f.add(bm);
f.add(bp);
f.add(bpi);
f.add(bw);
f.add(p);
f.add(C);
//Pozycje obiektów
b1.setBounds(10, 70, 60, 60);
b2.setBounds(70, 70, 60, 60);
b3.setBounds(130, 70, 60, 60);
b4.setBounds(10, 130, 60, 60);
b5.setBounds(70, 130, 60, 60);
b6.setBounds(130, 130, 60, 60);
b7.setBounds(10, 190, 60, 60);
b8.setBounds(70, 190, 60, 60);
b9.setBounds(130, 190, 60, 60);
b0.setBounds(10, 250, 60, 60);
bd.setBounds(190, 70, 60, 60);
bo.setBounds(190, 130, 60, 60);
bm.setBounds(190, 190, 60, 60);
bdz.setBounds(190, 250, 60, 60);
bp.setBounds(130, 250, 60, 60);
bpi.setBounds(70, 250, 60, 60);
bw.setBounds(10, 310, 120, 60);
C.setBounds(130, 310, 120, 60);
p.setBounds(10, 10, 240, 60);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bd.addActionListener(this);
bo.addActionListener(this);
bm.addActionListener(this);
bdz.addActionListener(this);
bp.addActionListener(this);
bpi.addActionListener(this);
bw.addActionListener(this);
C.addActionListener(this);
}
public void actionPerformed(ActionEvent klik) {
String cmd = klik.getActionCommand();
switch(cmd){
case "1":
p.setText("1");
break;
case "2":
p.setText("2");
break;
case "3":
p.setText("3");
break;
case "4":
p.setText("4");
break;
case "5":
p.setText("5");
break;
case "6":
p.setText("6");
break;
case "7":
p.setText("7");
break;
case "8":
p.setText("8");
break;
case "9":
p.setText("9");
break;
case "0":
p.setText("0");
break;
case "bd":
break;
case "bo":
break;
case "bdz":
break;
case "bm":
break;
case "bp":
break;
case "bpi":
break;
case "bw":
break;
case "C":
p.setText("");
}
}
}
Sorka za nazwy ale nie miałem weny wymyślać jakiś wyszukanych określeń :).