Como Somar Valores de um JTextField?

4 respostas
S

:oops: Estou com um grande, meu programa por enquanto não faz nada, só apresenta uma janela, o que devo fazer para ele somar os valores que serão incluídos nos JTextField (campo[0], campo[1], campo[2], campo[3]).

:?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!: :?: :!:

OBS: O programa está bem confuso, ao menos para uma iniciante em programação, pois foi feito praticamente com Ctrl+C, Ctrl+V e algumas dicas....

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenorCaminho extends JFrame
{
   JLabel lValor1 = new JLabel("Linha de Prod A");
   JLabel lValor2 = new JLabel("Linha de Prod B");
   JLabel lValor3 = new JLabel("De A Para B");
   JLabel lValor4 = new JLabel("De B Para A");
   
   private GridBagLayout gbLayout;
   
   public MenorCaminho()
   {
   super( "Menor Caminho" );
   
   String estacao;
      int i_estacao;
       
      estacao = JOptionPane.showInputDialog("Entre com a QUANTIDADE DE ESTAÇÕES");
      i_estacao = Integer.parseInt(estacao);    
          
      final int SIZE = i_estacao;
      
      GridBagConstraints gbConstraints = new GridBagConstraints();
      Container c = getContentPane();
      gbLayout = new GridBagLayout();
      c.setLayout( gbLayout );
   gbConstraints.fill = GridBagConstraints.NONE;
     
      Box boxes[] = new Box[9];
      Box campo[] = new Box[9];
            
      Box c1 = Box.createVerticalBox();
      Box c2 = Box.createVerticalBox();
      Box c3 = Box.createVerticalBox();
      Box c4 = Box.createVerticalBox();
      Box c5 = Box.createVerticalBox();
      Box c6 = Box.createVerticalBox();
      Box c7 = Box.createVerticalBox();
      Box c8 = Box.createVerticalBox();
      boxes[0] = Box.createVerticalBox();
      boxes[1] = Box.createVerticalBox();
      boxes[2] = Box.createVerticalBox();
      boxes[3] = Box.createVerticalBox();
      campo[0] = Box.createVerticalBox();
      campo[1] = Box.createVerticalBox();
      campo[2] = Box.createVerticalBox();
      campo[3] = Box.createVerticalBox();
           
      for (int i=0; i<(SIZE + 2); i++)
      {
       boxes[0].add(Box.createRigidArea (new Dimension(38,8)));
       boxes[0].add(new JButton("Est.A, "+i));
      } 
       
      for (int i=0; i<(SIZE + 2); i++)
      {
       boxes[1].add(Box.createRigidArea (new Dimension(38,8)));
       boxes[1].add(new JButton("Est.B, "+i));
      }
      
      for (int i=1; i<SIZE ; i++)
      {
       boxes[2].add(Box.createRigidArea (new Dimension(38,8)));
       boxes[2].add(new JButton("A"+i+ " p/ B"+(i+1)));
      } 
       
      for (int i=1; i<SIZE; i++)
      {
       boxes[3].add(Box.createRigidArea (new Dimension(38,8)));
       boxes[3].add(new JButton("B"+i+ " p/ A"+(i+1)));
      }
      
      for (int i=0; i<(SIZE + 2); i++)
      {
       campo[0].add(Box.createRigidArea (new Dimension(62,14)));
       campo[0].add(new JTextField("0"));
      } 
      
      for (int i=0; i<(SIZE + 2); i++)
      {
       campo[1].add(Box.createRigidArea (new Dimension(62,14)));
       campo[1].add(new JTextField("0"));
      } 
      
      for (int i=1; i<SIZE; i++)
      {
       campo[2].add(Box.createRigidArea (new Dimension(62,14)));
       campo[2].add(new JTextField("0"));
      } 
      
      for (int i=1; i<SIZE; i++)
      {
       campo[3].add(Box.createRigidArea (new Dimension(62,14)));
       campo[3].add(new JTextField("0"));
      } 
      
      c1.add(lValor1);
      c1.add(boxes[0]);
      c.add(c1);
      c2.add(campo[0]);
      c.add(c2);
      c3.add(lValor2);
      c3.add(boxes[1]);
      c.add(c3);
      c4.add(campo[1]);
      c.add(c4);
      c5.add(lValor3);
      c5.add(boxes[2]);
      c.add(c5);
      c6.add(campo[2]);
      c.add(c6);
      c7.add(lValor4);
      c7.add(boxes[3]);
      c.add(c7);
      c8.add(campo[3]);
      c.add(c8);
      
     
      setSize( 750, 400 );
      show();
   }
   public static void main( String args[] )
   {
     
      MenorCaminho app = new MenorCaminho();
      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }
}

4 Respostas

M

passa os valores das caixas para o tipo q vc qr … integer … float … e so somar

D

Tenta assim Sheyla…
Ex.:

jTextField3.setText(Integer.parseInt(jTextField2.getText())+Integer.parseInt(jTextField1.getText()))

se for soma de inteiros…
senaum tenta Double.parseDouble…

[]'s
Qualquer coisa, eh soh perguntar…

S

Olá Sheyla,

só pra constar… todos os tipos primitivos de Java tem um Wrapper que é uma classe, digamos, equivalente ao tipo primitivo. ex:

int -> Integer
short -> Short
boolean -> Boolean
char -> Character

nesses Wrappers vc tem métodos utilitários para trabalhar com conversões, especialmente métodos para conversão de Strings em tipos primitivos.

S

Muito Obrigada pelas dicas…

:fucando: Sheyla_DF

Criado 28 de maio de 2004
Ultima resposta 28 de mai. de 2004
Respostas 4
Participantes 4