Java - duda!!

 
Vista:

duda!!

Publicado por Rexixtente (9 intervenciones) el 07/04/2007 20:55:23
buenas, estoy haciendo una calculadora y tengo un error..

apenas estoy intentando hacer la operacion de suma pero realmente no se bien como hacerla,
espero me den mejores opciones....
salu2

aqui el code:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.awt.*;
class calculadora extends Frame
{
  Panel pnl1,pnl2,pnlp;
  Label lb;
  TextField tfi,tff;
  Button bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,btig,btce,btdiv,btmul,btres,btsum;
 
  calculadora()
  {
    super("CALCULADORA POR REXIXTENTE");
    lb=new Label("datos",Label.LEFT);
    tfi=new TextField("",18);
    tff=new TextField("",10);
    bt0=new Button("0");
    bt1=new Button("1");
    bt2=new Button("2");
    bt3=new Button("3");
    bt4=new Button("4");
    bt5=new Button("5");
    bt6=new Button("6");
    bt7=new Button("7");
    bt8=new Button("8");
    bt9=new Button("9");
    btig=new Button("=");
    btce=new Button("CE");
    btdiv=new Button("/");
    btmul=new Button("*");
    btres=new Button("-");
    btsum=new Button("+");
 
    pnl1=new Panel();
    pnl2=new Panel();
    pnlp=new Panel();
 
    pnl1.setLayout(new FlowLayout());
    pnl2.setLayout(new GridLayout(4,4));
    pnlp.setLayout(new GridLayout(2,1));
 
    pnl1.add(tfi);
 
 
 
    pnl2.add(bt7); pnl2.add(bt8); pnl2.add(bt9); pnl2.add(btdiv);
    pnl2.add(bt4); pnl2.add(bt5); pnl2.add(bt6); pnl2.add(btmul);
    pnl2.add(bt1); pnl2.add(bt2); pnl2.add(bt3); pnl2.add(btres);
    pnl2.add(bt0); pnl2.add(btig); pnl2.add(btce); pnl2.add(btsum);
 
    pnlp.add(pnl1);
    pnlp.add(pnl2);
    diseño();
    add(pnlp);
    move(200,200);
 
    pack();
    show();
  }
 
  public static void main (String a[])
  {
    calculadora objc=new calculadora();
  }
 
  public boolean action (Event evt,Object obj)
  {
    String ant="",aux1="";
    int n1=0,n2=0,sw=0,res=0,aux2;
 
    //con esta variable capturo  el dato anterior para poder ingresar al TextField el dato correcto sin sobreescribir datos
    ant=tfi.getText();
    if(evt.target==bt0) tfi.setText(ant + "0"); ant=tfi.getText();
    if(evt.target==bt1) tfi.setText(ant + "1"); ant=tfi.getText();
    if(evt.target==bt2) tfi.setText(ant + "2"); ant=tfi.getText();
    if(evt.target==bt3) tfi.setText(ant + "3"); ant=tfi.getText();
    if(evt.target==bt4) tfi.setText(ant + "4"); ant=tfi.getText();
    if(evt.target==bt5) tfi.setText(ant + "5"); ant=tfi.getText();
    if(evt.target==bt6) tfi.setText(ant + "6"); ant=tfi.getText();
    if(evt.target==bt7) tfi.setText(ant + "7"); ant=tfi.getText();
    if(evt.target==bt8) tfi.setText(ant + "8"); ant=tfi.getText();
    if(evt.target==bt9) tfi.setText(ant + "9"); ant=tfi.getText();
 
    if(evt.target==btce) tfi.setText("");
 
    ///MI IDEA PARA HACER LA SUMA
    if(evt.target==btsum)
    {
      n1=(int)Integer.parseInt(tfi.getText());
      sw=1;
      tfi.setText("");
    }
    if(evt.target==btig)
    {
        n2=(int)Integer.parseInt(tfi.getText());
        res=n1+n2;
        tfi.setText("resultado: " + (res));
    }
 
    return true;
  }
 
  void suma()
  {
 
  }
 
 
  private void diseño()
  {
    Color c=new Color(169,169,169);
    bt0.setBackground(c);
    bt1.setBackground(c);
    bt2.setBackground(c);
    bt3.setBackground(c);
    bt4.setBackground(c);
    bt5.setBackground(c);
    bt6.setBackground(c);
    bt7.setBackground(c);
    bt8.setBackground(c);
    bt9.setBackground(c);
    btig.setBackground(c);
    btce.setBackground(c);
    btsum.setBackground(c);
    btres.setBackground(c);
    btmul.setBackground(c);
    btdiv.setBackground(c);
    bt0.setForeground(Color.blue);
    bt1.setForeground(Color.blue);
    bt2.setForeground(Color.blue);
    bt3.setForeground(Color.blue);
    bt4.setForeground(Color.blue);
    bt5.setForeground(Color.blue);
    bt6.setForeground(Color.blue);
    bt7.setForeground(Color.blue);
    bt8.setForeground(Color.blue);
    bt9.setForeground(Color.blue);
    btig.setForeground(Color.red);
    btce.setForeground(Color.red);
    btsum.setForeground(Color.red);
    btres.setForeground(Color.red);
    btmul.setForeground(Color.red);
    btdiv.setForeground(Color.red);
    bt0.setFont(new Font("tahoma",Font.BOLD,12));
    bt1.setFont(new Font("tahoma",Font.BOLD,12));
    bt2.setFont(new Font("tahoma",Font.BOLD,12));
    bt3.setFont(new Font("tahoma",Font.BOLD,12));
    bt4.setFont(new Font("tahoma",Font.BOLD,12));
    bt5.setFont(new Font("tahoma",Font.BOLD,12));
    bt6.setFont(new Font("tahoma",Font.BOLD,12));
    bt7.setFont(new Font("tahoma",Font.BOLD,12));
    bt8.setFont(new Font("tahoma",Font.BOLD,12));
    bt9.setFont(new Font("tahoma",Font.BOLD,12));
    btig.setFont(new Font("tahoma",Font.BOLD,12));
    btce.setFont(new Font("tahoma",Font.BOLD,12));
    btsum.setFont(new Font("tahoma",Font.BOLD,12));
    btres.setFont(new Font("tahoma",Font.BOLD,12));
    btmul.setFont(new Font("tahoma",Font.BOLD,12));
    btdiv.setFont(new Font("tahoma",Font.BOLD,12));
  }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:duda!!

Publicado por Mario (622 intervenciones) el 09/04/2007 16:20:03
En un foro debes de colocar dudas especificas, asi es dificil q t ayuden

Saludos!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:duda!!

Publicado por Rexixtente (9 intervenciones) el 09/04/2007 22:30:24
creo que explique mi duda...
y es como hago para hacer la operacion de suma.....
aqui esta el trozo de code en donde intento hacerlo pero obviamente no me funciona:

1
2
3
4
5
6
7
8
9
10
11
12
if(evt.target==btsum)
{
n1=(int)Integer.parseInt(tfi.getText());
sw=1;
tfi.setText("");
}
if(evt.target==btig)
{
n2=(int)Integer.parseInt(tfi.getText());
res=n1+n2;
tfi.setText("resultado: " + (res));
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar