Boa tarde, estou desenvolvendo uma calculadora em Java, mas estou tendo o seguinte problema:
Estou usando botões!!
Convertia o valor recebido do usuario para INT mas se o num fosse muito grande teria problemas, entao passei a converter para DOUBLE ou FLOAT mas tive o problema q qdo convertia por ex o 1 ficava 1.0 entao qdo eu clico novamente no botao para se fazer um "11"ele concatena assim: “1.01.0”.
como corrigir esse problema.
Obrigado;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class Calculadora extends JFrame {
JButton zero, um, dois, tres, quatro, cinco, seis, sete, oito, nove;
JButton soma, sub, mult, divi, limpa, resposta;
JTextField texto;
double valor, resultado;
String calc,somar;
float valor1;
public Calculadora() {
super("CALCULADORA");
setSize(380,500);
Container tela = getContentPane();
setLayout(null);
texto = new JTextField(15);
texto.setBounds(10,10, 350, 35);
tela.add(texto);
zero = new JButton("0");
um = new JButton("1");
dois = new JButton("2");
tres = new JButton("3");
quatro = new JButton("4");
cinco = new JButton("5");
seis = new JButton("6");
sete = new JButton("7");
oito = new JButton("8");
nove = new JButton("9");
zero.setBounds (10,410, 265,40);
um.setBounds (10,360, 85,40);
dois.setBounds (100,360, 85,40);
tres.setBounds (190,360, 85,40);
quatro.setBounds(10,310, 85,40);
cinco.setBounds (100,310, 85,40);
seis.setBounds (190,310, 85,40);
sete.setBounds (10,260, 85,40);
oito.setBounds (100,260, 85,40);
nove.setBounds (190,260, 85,40);
tela.add(zero );
tela.add(um );
tela.add(dois );
tela.add(tres );
tela.add(quatro);
tela.add(cinco );
tela.add(seis );
tela.add(sete );
tela.add(oito );
tela.add(nove );
/******************************/
soma = new JButton("+");
sub = new JButton("-");
mult = new JButton("*");
divi = new JButton("/");
limpa = new JButton("LIMPAR");
resposta = new JButton("ENTER");
soma.setBounds (10 ,210,85, 40);
sub.setBounds (100,210,85, 40);
mult.setBounds (190,210,85, 40);
divi.setBounds (280,210,85, 40);
limpa.setBounds (280,260,85, 40);
resposta.setBounds(280,310,85,135);
tela.add(soma);
tela.add(sub);
tela.add(mult);
tela.add(divi);
tela.add(limpa);
tela.add(resposta);
setLocationRelativeTo(null);
setVisible(true);
/*****************************/
um.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(1));
valor1 = Float.parseFloat(texto.getText());
}else{
if ( valor1 == 0 ){
texto.setText(String.valueOf(1));
valor1 = Float.parseFloat(texto.getText());
}else{
JOptionPane.showMessageDialog(null, ""+ valor1);
texto.setText("" + valor1 + String.valueOf(1) );
valor1 = Float.parseFloat(texto.getText());
JOptionPane.showMessageDialog(null,"CONCATENADO: "+ ""+ valor1);
}
}
}
}
);
dois.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(2));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(2));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(2) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
tres.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(3));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(3));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(3) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
quatro.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(4));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(4));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(4) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
cinco.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(5));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(5));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(5) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
seis.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(6));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(6));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(6) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
sete.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(7));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(7));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(7) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
oito.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(8));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(8));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(8) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
nove.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(9));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(9));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(9) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
zero.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if ( texto.getText().equals("") ){
texto.setText(String.valueOf(0));
valor = Double.parseDouble(texto.getText());
}else{
if ( valor == 0 ){
texto.setText(String.valueOf(0));
valor = Double.parseDouble(texto.getText());
}else{
texto.setText("" +valor + String.valueOf(0) );
valor = Double.parseDouble(texto.getText());
}
}
}
}
);
/**************************************************************/
soma.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
resultado += valor;
valor = 0 ;
texto.setText("" + resultado );
calc = "conta";
}
}
);
resposta.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
}
}
);
limpa.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
texto.setText(null);
valor = 0;
resultado = 0;
texto.requestFocus();
}
}
);
}
public static void main (String[] args){
new Calculadora();
}
}