Olá pessoal, esse é meu primeiro tópico aqui no Guj, sou iniciante java e qualquer dica é de grande importância pra mim.
Quando eu executo este programa e utilizo o varlor 3 na quantidade do item bola de gude aparece o número 00000001 no total do mesmo, não sei o que pode ser. x_x
Estou usando o WindowBuilder.
desde já agradeço.
package Visual;
import java.awt.EventQueue;
public class teste {
private JFrame frame;
private JTextField textNome;
private JLabel lblBolaDeGude;
private JTextField textQuantGude;
private JTextField textValorGude;
private JTextField textTotal_Gude;
private JLabel lblQuant;
private JLabel lblValorUnitrio;
private JLabel lblTotal;
private JTextField textQuantQuete;
private JTextField textValorQuete;
private JTextField textTotal_Quete;
private JTextField textQuantLiche;
private JTextField textValorLiche;
private JTextField textTotal_Liche;
private JLabel lblBolaDeBasquete;
private JLabel lblBolaDeBoliche;
private JTextField textTotal_Total;
private JButton btnCalculando;
private JLabel lblTotal_1;
private JLabel lblR;
private JLabel label;
private JLabel label_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
teste window = new teste();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public teste() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 464, 269);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setTitle("EXERCICIO 3.2");
JLabel lblNome = new JLabel("Nome");
lblNome.setBounds(10, 11, 46, 14);
frame.getContentPane().add(lblNome);
textNome = new JTextField();
textNome.setBounds(66, 8, 139, 20);
frame.getContentPane().add(textNome);
textNome.setColumns(10);
JLabel lblItensComprados = new JLabel("Itens comprados");
lblItensComprados.setFont(new Font("Tahoma", Font.BOLD, 13));
lblItensComprados.setBounds(66, 41, 119, 14);
frame.getContentPane().add(lblItensComprados);
lblBolaDeGude = new JLabel("Bola de gude");
lblBolaDeGude.setBounds(66, 68, 119, 14);
frame.getContentPane().add(lblBolaDeGude);
textQuantGude = new JTextField();
textQuantGude.setBounds(195, 62, 41, 20);
frame.getContentPane().add(textQuantGude);
textQuantGude.setColumns(10);
textValorGude = new JTextField();
textValorGude.setBackground(UIManager.getColor("Button.background"));
textValorGude.setEditable(false);
textValorGude.setBounds(255, 62, 70, 20);
frame.getContentPane().add(textValorGude);
textValorGude.setColumns(10);
textTotal_Gude = new JTextField();
textTotal_Gude.setBackground(UIManager.getColor("Button.background"));
textTotal_Gude.setEditable(false);
textTotal_Gude.setBounds(335, 62, 59, 20);
frame.getContentPane().add(textTotal_Gude);
textTotal_Gude.setColumns(10);
lblQuant = new JLabel("Quant");
lblQuant.setFont(new Font("Tahoma", Font.BOLD, 11));
lblQuant.setBounds(201, 41, 35, 14);
frame.getContentPane().add(lblQuant);
lblValorUnitrio = new JLabel("Valor Unit\u00E1rio");
lblValorUnitrio.setFont(new Font("Tahoma", Font.BOLD, 11));
lblValorUnitrio.setBounds(250, 41, 87, 14);
frame.getContentPane().add(lblValorUnitrio);
lblTotal = new JLabel("Total");
lblTotal.setFont(new Font("Tahoma", Font.BOLD, 11));
lblTotal.setBounds(344, 41, 46, 14);
frame.getContentPane().add(lblTotal);
textQuantQuete = new JTextField();
textQuantQuete.setColumns(10);
textQuantQuete.setBounds(195, 93, 41, 20);
frame.getContentPane().add(textQuantQuete);
textValorQuete = new JTextField();
textValorQuete.setBackground(UIManager.getColor("Button.background"));
textValorQuete.setEditable(false);
textValorQuete.setColumns(10);
textValorQuete.setBounds(255, 93, 70, 20);
frame.getContentPane().add(textValorQuete);
textTotal_Quete = new JTextField();
textTotal_Quete.setBackground(UIManager.getColor("Button.background"));
textTotal_Quete.setEditable(false);
textTotal_Quete.setColumns(10);
textTotal_Quete.setBounds(335, 93, 59, 20);
frame.getContentPane().add(textTotal_Quete);
textQuantLiche = new JTextField();
textQuantLiche.setColumns(10);
textQuantLiche.setBounds(195, 124, 41, 20);
frame.getContentPane().add(textQuantLiche);
textValorLiche = new JTextField();
textValorLiche.setBackground(UIManager.getColor("Button.background"));
textValorLiche.setEditable(false);
textValorLiche.setColumns(10);
textValorLiche.setBounds(255, 124, 70, 20);
frame.getContentPane().add(textValorLiche);
textTotal_Liche = new JTextField();
textTotal_Liche.setBackground(UIManager.getColor("Button.background"));
textTotal_Liche.setEditable(false);
textTotal_Liche.setColumns(10);
textTotal_Liche.setBounds(335, 122, 59, 20);
frame.getContentPane().add(textTotal_Liche);
lblBolaDeBasquete = new JLabel("Bola de basquete");
lblBolaDeBasquete.setBounds(66, 99, 119, 14);
frame.getContentPane().add(lblBolaDeBasquete);
lblBolaDeBoliche = new JLabel("Bola de boliche");
lblBolaDeBoliche.setBounds(66, 130, 119, 14);
frame.getContentPane().add(lblBolaDeBoliche);
textTotal_Total = new JTextField();
textTotal_Total.setEditable(false);
textTotal_Total.setColumns(10);
textTotal_Total.setBounds(335, 153, 59, 20);
frame.getContentPane().add(textTotal_Total);
final JTextArea txtMensagem = new JTextArea();
txtMensagem.setWrapStyleWord(true);
txtMensagem.setFont(new Font("Monospaced", Font.BOLD | Font.ITALIC, 13));
txtMensagem.setBackground(UIManager.getColor("Button.background"));
txtMensagem.setLineWrap(true);
txtMensagem.setEditable(false);
txtMensagem.setBounds(10, 150, 315, 67);
frame.getContentPane().add(txtMensagem);
// Valores unitários
final double gude = 0.20;
final double quete = 60.00;
final double liche = 120.00;
textValorGude.setText(String.valueOf(gude));
textValorQuete.setText(String.valueOf(quete));
textValorLiche.setText(String.valueOf(liche));
// iniciar quantidade com 0
textQuantGude.setText("0");
textQuantQuete.setText("0");
textQuantLiche.setText("0");
btnCalculando = new JButton("Calculando");
btnCalculando.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String nome = textNome.getText();
int quantGude = Integer.parseInt(textQuantGude.getText());
int quantQuete = Integer.parseInt(textQuantQuete.getText());
int quantLiche = Integer.parseInt(textQuantLiche.getText());
if ( quantGude <= 0 )
quantGude = 0;
if ( quantQuete <= 0 )
quantQuete = 0;
if ( quantLiche <= 0 )
quantLiche = 0;
// total parcial
double totalGude = quantGude * gude;
double totalQuete = quantQuete * quete;
double totalLiche = quantLiche * liche;
textTotal_Gude.setText("R$ " + totalGude);
textTotal_Quete.setText("R$ " + totalQuete);
textTotal_Liche.setText("R$ " + totalLiche);
// total final
Double total_total = totalGude + totalQuete + totalLiche;
textTotal_Total.setText("R$ " + String.valueOf(total_total));
// mensagem final
String mensagem = nome + " comprou os itens: ";
if (quantGude <= 0 && quantQuete <= 0 && quantLiche <= 0){
mensagem = nome + " Não comprou nenhum dos itens.";
} else{
if ( quantGude == 1 )
mensagem += "Bola de gude, ";
else if ( quantGude > 1)
mensagem += "Bolas de gude, ";
if ( quantQuete == 1 )
mensagem += "Bola de Basquete, ";
else if ( quantQuete > 1)
mensagem += "Bolas de Basquete, ";
if ( quantLiche == 1 )
mensagem += "Bola de Boliche, ";
else if ( quantLiche > 1)
mensagem += "Bolas de Boliche, ";
}// fim else
// terminar mensagem com "." final
int cortar = mensagem.length();
mensagem = mensagem.substring(0,(cortar - 2));
mensagem += ".";
txtMensagem.setText(mensagem);
}
});
btnCalculando.setBounds(215, 7, 110, 23);
frame.getContentPane().add(btnCalculando);
lblTotal_1 = new JLabel("Total");
lblTotal_1.setFont(new Font("Tahoma", Font.BOLD, 11));
lblTotal_1.setBounds(399, 156, 46, 14);
frame.getContentPane().add(lblTotal_1);
lblR = new JLabel("R$");
lblR.setFont(new Font("Tahoma", Font.PLAIN, 11));
lblR.setBounds(238, 62, 19, 20);
frame.getContentPane().add(lblR);
label = new JLabel("R$");
label.setFont(new Font("Tahoma", Font.PLAIN, 11));
label.setBounds(238, 93, 19, 20);
frame.getContentPane().add(label);
label_1 = new JLabel("R$");
label_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
label_1.setBounds(238, 124, 19, 20);
frame.getContentPane().add(label_1);
}
}