Tentando soltar uma linha! HELP

4 respostas
FredRiller

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?

ai o codigo:

import javax.swing.*;

public class CalculadoraParaChurrasco

{

public static void main(String[] args)

{

String a = JOptionPane.showInputDialog(Numero de pessoas no Churrasco.);

int pessoas = Integer.parseInt(a);
String separador = System.getProperty("line.separator");
	int cerveja = 2;
	int refrigerante = 500;
	int carne = 300; //em gramas
	
	JFrame frame = new JFrame("Calculadora para Churrasco");
	JLabel label = new JLabel("Cerveja..........: " + (pessoas * cerveja) + "Garrafas" + separador + "Refrigerante.....: " + pessoas * refrigerante / 1000 + " L" + separador + "Carne.............: " + (pessoas * carne / 1000) + " KG" + separador);
	frame.getContentPane().add(label);    
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.pack();
	frame.setVisible(true);	
}

}

4 Respostas

Anderson_pqdt

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.

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class CalculadoraParaChurrasco extends JFrame implements ActionListener, KeyListener
{
	JTextField txNrPessoas;
	JTextField txQtdRefrigerante;
	JTextField txQtdCerveja;
	JTextField txQtdCarne;
	JButton    btnCalcular;
	
	JLabel     lbNrPessoas;
	JLabel     lbQtdRefrigerante;
	JLabel     lbQtdCerveja;
	JLabel     lbQtdCarne;
	
	public CalculadoraParaChurrasco()
	{
		super("Calculadora para Churrasco");
		
		Container container = getContentPane();		
		JPanel panel = new JPanel();
		container.setLayout(new GridLayout(9, 1));
		
		txNrPessoas       = new JTextField(5);
		txQtdRefrigerante = new JTextField(5);
		txQtdCerveja      = new JTextField(5);
		txQtdCarne        = new JTextField(5);

		lbNrPessoas       = new JLabel("Numero de pessoas no Churrasco.");
		lbQtdRefrigerante = new JLabel("Quantidade de Refrigerante.(Litros)");
		lbQtdCerveja      = new JLabel("Quantidade de Cerveja.");
		lbQtdCarne        = new JLabel("Quantidade de Carne. (Kg)");

		btnCalcular       = new JButton("Calcular");

		container.add(lbNrPessoas);
		
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.add( txNrPessoas );
		txNrPessoas.addKeyListener(this);  
		container.add(panel);
		
		txQtdRefrigerante.setEditable(false);
		container.add(lbQtdRefrigerante);
		
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.add( txQtdRefrigerante );
		container.add(panel);
				
		txQtdCerveja.setEditable(false);
		container.add(lbQtdCerveja);
		
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.add( txQtdCerveja );
		container.add(panel);
		
		txQtdCarne.setEditable(false);
		container.add(lbQtdCarne);
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.add( txQtdCarne );
		container.add(panel);
		
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.add( btnCalcular );
		
		container.add( panel );
		
		btnCalcular.addActionListener(this);
		
		setLocation(200, 200);
		setSize(400, 400);
		setResizable(false);
		pack();
		setVisible(true);
	}
	
	
	
	public static void main(String[] args)
	{
		new CalculadoraParaChurrasco();		
	}

	public void actionPerformed(ActionEvent e) {
		calculaTotal( );
	}
	
	public void calculaTotal( )
	{
		double cerveja = 2;
		double refrigerante = 500;
		double carne = 300; //em gramas
		double pessoas = 0;
		Double totCerveja = 0.0;
		Double totGarrafasdeRefrigerante = 0.0;
		Double totCarne = 0.0;
		
		try{
			pessoas = Integer.parseInt( txNrPessoas.getText() );//Integer.parseInt lança uma exceção quando se digita algo diferente de número
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, "Digite somente números.");
		}
		
		totCerveja = (pessoas * cerveja);
		txQtdCerveja.setText(totCerveja.toString());
		
		totGarrafasdeRefrigerante = pessoas * refrigerante / 1000; //em litros
		txQtdRefrigerante.setText(totGarrafasdeRefrigerante.toString());
		
		totCarne = (pessoas * carne / 1000); // em quilos
		txQtdCarne.setText(totCarne.toString());
	}

	
	public void keyPressed(KeyEvent e) {
		// nada
	}

	public void keyReleased(KeyEvent e) {
		if(txNrPessoas.getText().equals(""))
			limpaForm();
		else
			calculaTotal( );
	}

	public void keyTyped(KeyEvent e) {
		// nada
	}
	
	public void limpaForm()
	{
		txNrPessoas.setText("");
		txNrPessoas.setText("");
		txQtdRefrigerante.setText("");
		txQtdCerveja.setText("");
		txQtdCarne.setText("");
	}
}
ViniGodoy

Oi, sempre que postar código (aqui ou na Unidev), por favor use as tags code. Se você não sabe como fazer isso, veja o link:
http://www.guj.com.br/posts/list/50115.java

O seu problema é que o JLabel não suporta a quebra de linha com \n ou \r\n. No lugar, use HTML, como no exemplo abaixo:

JLabel label = new JLabel("&lt;html&gt;Cerveja..........: " + (pessoas * cerveja) + "<br>" + "Refrigerante.....: " + (pessoas * refrigerante / 1000) + "<br>" + "Copos............: " + (pessoas * copo) + "<br>" + "Talheres.........: " + (pessoas * talher));

Você ainda tem a vantagem dele suportar outras tags como negrito, itálico e sublinhado.

FredRiller

ai vlw!
consegui vinigodoy

maior_abandonado

public class CalculadoraParaChurrasco
{

eu posso estar enganado, mais acho que você também teria que usar herança de JFrame…

Criado 6 de janeiro de 2008
Ultima resposta 7 de jan. de 2008
Respostas 4
Participantes 4