Olá pessoal ! Só começei a estudar java por conta própria faz apenas 2 meses que começei a estudar . Faço ciências da computação, mas na grade do curso vou começar a aprender java somente este ano e estou tentando desenvolver uma calculadora, mas estou encontrando muitas dificuldades .
Já fiz varias pesquisas no google, mas elas não me ajudaram muito.
Será que alguem se habilitar a me ajudar ?!
Abaixo está o código que consegui desenvolver até agora !
import java.awt.;
import java.awt.event.;
public class GridLayoutDemo extends Frame implements ActionListener
{
private TextField tf1;
private Button but1 , but2, but3, but4, but5, but6, but7, but8, but9, but0, butMais, butMenos;
private Button butDiv, butMult, butIgual, butOff, butMaisMenos, butC , butM, butPonto;
private boolean test = false;
public static void main(String args[])
{
GridLayoutDemo f = new GridLayoutDemo();
f.addWindowListener(new CloseWindowAndExit() );
f.show();
}
public GridLayoutDemo()
{
super(" Calculadora ");
setSize(150 , 220);
setLocation(300 , 175);
setResizable(false);
tf1 = new TextField("" , 13);
tf1.addActionListener(this);
but0 = new Button("0");
but0.addActionListener(this);
but1 = new Button("1");
but1.addActionListener(this);
but2 = new Button("2");
but2.addActionListener(this);
but3 = new Button("3");
but3.addActionListener(this);
but4 = new Button("4");
but4.addActionListener(this);
but5 = new Button("5");
but5.addActionListener(this);
but6 = new Button("6");
but6.addActionListener(this);
but7 = new Button("7");
but7.addActionListener(this);
but8 = new Button("8");
but8.addActionListener(this);
but9 = new Button("9");
but9.addActionListener(this);
butDiv = new Button(" / ");
butDiv.addActionListener(this);
butMult = new Button(" x ");
butMult.addActionListener(this);
butMais = new Button(" + ");
butMais.addActionListener(this);
butMenos = new Button(" - ");
butMenos.addActionListener(this);
butIgual = new Button(" = ");
butIgual.addActionListener(this);
butMaisMenos = new Button("+/-");
butMaisMenos.addActionListener(this);
butC = new Button("C");
butC.addActionListener(this);
butM = new Button("M");
butM.addActionListener(this);
butPonto = new Button(",");
butPonto.addActionListener(this);
butOff = new Button("Off");
butOff.addActionListener(this);
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT) );
p1.setBackground(SystemColor.control);
add("Center" , p1);
Panel p4 = new Panel();
p4.setBackground(SystemColor.control);
p4.setLayout(new FlowLayout(FlowLayout.CENTER) );
p4.add(tf1);
add("North" , p4);
p1.add(p4);
Panel p2 = new Panel();
p2.setBackground(SystemColor.control);
p2.setLayout(new GridLayout(5 , 1 , 5 , 5) );
//p2.add(new TextField() );
p2.add(butOff);
p2.add(butM);
p2.add(butC);
p2.add(but7);
p2.add(but8);
p2.add(but9);
p2.add(but4);
p2.add(but5);
p2.add(but6);
p2.add(but1);
p2.add(but2);
p2.add(but3);
p2.add(but0);
p2.add(butMaisMenos);
p2.add(butPonto);
add("South" , p2);
p1.add(p2);
Panel p3 = new Panel();
p3.setBackground(SystemColor.control);
p3.setLayout(new GridLayout(5 , 1 , 5 , 5) );
p3.add(butDiv);
p3.add(butMult);
p3.add(butMenos);
p3.add(butMais);
p3.add(butIgual);
add("East" , p3);
p1.add(p3);
}
public void actionPerformed(ActionEvent e)
{
int aux = 0;
String a = "";
if(e.getSource() == but0) {
a = tf1.getText();
a += "0";
tf1.setText(a);
}
if(e.getSource() == but1) {
a = tf1.getText();
a += "1";
tf1.setText(a);
}
if(e.getSource() == but2) {
a = tf1.getText();
a += "2";
tf1.setText(a);
}
if(e.getSource() == but3) {
a = tf1.getText();
a += "3";
tf1.setText(a);
}
if(e.getSource() == but4) {
a = tf1.getText();
a += "4";
tf1.setText(a);
}
if(e.getSource() == but5) {
a = tf1.getText();
a += "5";
tf1.setText(a);
}
if(e.getSource() == but6) {
a = tf1.getText();
a += "6";
tf1.setText(a);
}
if(e.getSource() == but7) {
a = tf1.getText();
a += "7";
tf1.setText(a);
}
if(e.getSource() == but8) {
a = tf1.getText();
a += "8";
tf1.setText(a);
}
if(e.getSource() == but9) {
a = tf1.getText();
a += "9";
tf1.setText(a);
}
if(e.getSource() == butMais || e.getSource() == butMenos || e.getSource() == butMult || e.getSource() == butDiv) {
while(e.getSource() != butIgual) {
if(e.getSource() == butMais) {
//aux = tf1.getText() + tf1.setText(a);
//tf1.setText();
}
if(e.getSource() == butMenos) {
}
if(e.getSource() == butMult) {
}
if(e.getSource() == butDiv) {
}
}
}
if(e.getSource() == butPonto){
if(test != true) {
a = tf1.getText();
a += ",";
tf1.setText(a);
test = true;
}
}
if(e.getSource() == butMaisMenos){
/*a = tf1.getText();
a = a * -1;
tf1.setText(a);*/
}
if(e.getSource() == butC) {
}
if(e.getSource() == butOff)
System.exit(0);
}
}
Desde já muito Obrigada !