to fazendo uma calculadora para churrasco so pra treinar algumas coisas que aprendi e tal.
mais to com um problema eu to tentando soltar uma linha mais do geito que to tentando n ta funcionando.
alguem pode me ajuda?
Brother,
não sei exatamente o que vc queria fazer...
então tentei refazer a calculadora do churrasco, agora tenta dar uma melhorada.
o seu primeiro cálculo estava 'roubando' algumas gramas de carne, então fiz um pequeno ajuste.
ficou um pouco grande mas acho que isso vai te ajudar.
importjava.awt.Component;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjavax.swing.*;publicclassCalculadoraParaChurrascoextendsJFrameimplementsActionListener,KeyListener{JTextFieldtxNrPessoas;JTextFieldtxQtdRefrigerante;JTextFieldtxQtdCerveja;JTextFieldtxQtdCarne;JButtonbtnCalcular;JLabellbNrPessoas;JLabellbQtdRefrigerante;JLabellbQtdCerveja;JLabellbQtdCarne;publicCalculadoraParaChurrasco(){super("Calculadora para Churrasco");Containercontainer=getContentPane();JPanelpanel=newJPanel();container.setLayout(newGridLayout(9,1));txNrPessoas=newJTextField(5);txQtdRefrigerante=newJTextField(5);txQtdCerveja=newJTextField(5);txQtdCarne=newJTextField(5);lbNrPessoas=newJLabel("Numero de pessoas no Churrasco.");lbQtdRefrigerante=newJLabel("Quantidade de Refrigerante.(Litros)");lbQtdCerveja=newJLabel("Quantidade de Cerveja.");lbQtdCarne=newJLabel("Quantidade de Carne. (Kg)");btnCalcular=newJButton("Calcular");container.add(lbNrPessoas);panel=newJPanel();panel.setLayout(newFlowLayout());panel.add(txNrPessoas);txNrPessoas.addKeyListener(this);container.add(panel);txQtdRefrigerante.setEditable(false);container.add(lbQtdRefrigerante);panel=newJPanel();panel.setLayout(newFlowLayout());panel.add(txQtdRefrigerante);container.add(panel);txQtdCerveja.setEditable(false);container.add(lbQtdCerveja);panel=newJPanel();panel.setLayout(newFlowLayout());panel.add(txQtdCerveja);container.add(panel);txQtdCarne.setEditable(false);container.add(lbQtdCarne);panel=newJPanel();panel.setLayout(newFlowLayout());panel.add(txQtdCarne);container.add(panel);panel=newJPanel();panel.setLayout(newFlowLayout());panel.add(btnCalcular);container.add(panel);btnCalcular.addActionListener(this);setLocation(200,200);setSize(400,400);setResizable(false);pack();setVisible(true);}publicstaticvoidmain(String[]args){newCalculadoraParaChurrasco();}publicvoidactionPerformed(ActionEvente){calculaTotal();}publicvoidcalculaTotal(){doublecerveja=2;doublerefrigerante=500;doublecarne=300;//em gramasdoublepessoas=0;DoubletotCerveja=0.0;DoubletotGarrafasdeRefrigerante=0.0;DoubletotCarne=0.0;try{pessoas=Integer.parseInt(txNrPessoas.getText());//Integer.parseInt lança uma exceção quando se digita algo diferente de número}catch(Exceptionex){JOptionPane.showMessageDialog(null,"Digite somente números.");}totCerveja=(pessoas*cerveja);txQtdCerveja.setText(totCerveja.toString());totGarrafasdeRefrigerante=pessoas*refrigerante/1000;//em litrostxQtdRefrigerante.setText(totGarrafasdeRefrigerante.toString());totCarne=(pessoas*carne/1000);// em quilostxQtdCarne.setText(totCarne.toString());}publicvoidkeyPressed(KeyEvente){// nada}publicvoidkeyReleased(KeyEvente){if(txNrPessoas.getText().equals(""))limpaForm();elsecalculaTotal();}publicvoidkeyTyped(KeyEvente){// nada}publicvoidlimpaForm(){txNrPessoas.setText("");txNrPessoas.setText("");txQtdRefrigerante.setText("");txQtdCerveja.setText("");txQtdCarne.setText("");}}