Interface Gráfica - Java - Não estou conseguindo pegar o valor na label...

Java

Interface

Eclipse

05/12/2019

Não estou conseguindo fazer esse exercício de interface gráfica pelo java - Ecplise, não consigo receber o valor na label e calcular e depois aparecer um alert com o valor total
No próximo exemplo vamos construir um aplicativo para o cálculo dos ganhos com aplicações em poupança. Para o cálculo, necessitamos saber o período (em anos) da aplicação, o valor dos juros mensais e a quantidade que é depositada mensalmente na poupança.

Para esta tarefa, utilizaremos três classes:

o Poupança: que possui os atributos desejados e os métodos de cálculo;

o JanelaPoupanca: que possui a interface gráfica;

o Main: que possui o método main, no qual é criada uma instância de AplicacaoPoupanca.

package Pacote;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;

import javax.swing.JFormattedTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Poupanca {
	private int anos;

    private double juros;

    private double depositoMensal;

   


   

  

   

    public String toString() {

             return "Anos: " + anos + "\nJuros: " + juros

                 + "\nDepósito mensal: " + depositoMensal;

    }



	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Poupanca window = new Poupanca();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Poupanca() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 377, 235);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel Label_Nome = new JLabel("Nome:");
		Label_Nome.setFont(new Font("Arial", Font.PLAIN, 12));
		Label_Nome.setBounds(79, 13, 46, 14);
		frame.getContentPane().add(Label_Nome);
		
		JLabel Label_Juros = new JLabel("Juros ao m\u00EAs % :");
		Label_Juros.setFont(new Font("Arial", Font.PLAIN, 12));
		Label_Juros.setBounds(21, 38, 104, 32);
		frame.getContentPane().add(Label_Juros);
		
		JLabel lblNumDeAnos = new JLabel("Num. de Anos :");
		lblNumDeAnos.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNumDeAnos.setBounds(31, 81, 91, 14);
		frame.getContentPane().add(lblNumDeAnos);
		
		JLabel lblDepsitoMensalR = new JLabel("Dep\u00F3sito mensal R$:");
		lblDepsitoMensalR.setFont(new Font("Arial", Font.PLAIN, 12));
		lblDepsitoMensalR.setBounds(1, 108, 124, 14);
		frame.getContentPane().add(lblDepsitoMensalR);
		
		JLabel Label_Valor_Total = new JLabel("Nome:");
		Label_Valor_Total.setFont(new Font("Arial", Font.PLAIN, 12));
		Label_Valor_Total.setBounds(85, 131, 46, 22);
		frame.getContentPane().add(Label_Valor_Total);
		
		JFormattedTextField TxtNome = new JFormattedTextField();
		TxtNome.setBounds(122, 11, 136, 20);
		frame.getContentPane().add(TxtNome);
		
		JButton BtnCalcular = new JButton("Calcular");
		BtnCalcular.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int a=0;
				double j = 0;
				double dm = 0;
	            
			     anos = a;

			        juros = j/100;

			        depositoMensal = dm;
			        int num_pagamentos = anos * 12;

			        double total = 0;

			        for (int i = 0; i < num_pagamentos; i++) {

			            total = total + depositoMensal;

			            total = total + total * juros;
			        }
				JOptionPane.showMessageDialog(null, "Valor Total:" + total);
			}
		});
		BtnCalcular.setBounds(87, 164, 89, 23);
		frame.getContentPane().add(BtnCalcular);
		
		JButton BtnSalvar = new JButton("Salvar");
		BtnSalvar.setBounds(229, 164, 89, 23);
		frame.getContentPane().add(BtnSalvar);
		
		JFormattedTextField TxtJuros = new JFormattedTextField();
		TxtJuros.setBounds(122, 45, 136, 20);
		frame.getContentPane().add(TxtJuros);
		
		JFormattedTextField TxtAnos = new JFormattedTextField();
		TxtAnos.setBounds(122, 76, 136, 20);
		frame.getContentPane().add(TxtAnos);
		
		JFormattedTextField TxtDPMensal = new JFormattedTextField();
		TxtDPMensal.setBounds(122, 106, 136, 20);
		frame.getContentPane().add(TxtDPMensal);
		
		JFormattedTextField TxtTotal = new JFormattedTextField();
		TxtTotal.setBounds(122, 133, 136, 20);
		frame.getContentPane().add(TxtTotal);
	}
}
Luciano

Luciano

Curtidas 0

Respostas

Manoel Junior

Manoel Junior

05/12/2019

Bom dia.
Para atribuir um texto à Label:
jLabel1.setText("Olá mundo");

Para mostrar o valor da Label:
JOptionPane.showMessageDialog(null, jLabel1.getText());
GOSTEI 0
POSTAR