Seguinte, quero implementar um botão que ao clicar ele faça a ação de calcular as respectivas porcentagens e exibir o valor no final no label, beleza, eu não sei muito como funciona o Listener, gostaria de que através deste exemplo, pudessem me ajudar a entender.
Grato!
package telas;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Toolkit;
public class CalculadoraImposto extends JFrame{
//Declarando os componentes
public JLabel lblTituloPrograma;
public JLabel lblValorTotalNota;
public JLabel lblIrrf;
public JLabel lblPis;
public JLabel lblCofins;
public JLabel lblCsll;
public JLabel lbltotal;
public JLabel lblPorcentagem;
public JButton btnCalcular;
public JTextField txtValorTotalNota;
public JTextField txtIrff;
public JTextField txtPis;
public JTextField txtCofins;
public JTextField txtCsll;
//construtor
public CalculadoraImposto(){
//look and feel pega do OS atual
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
this.setResizable(false);
this.setTitle("Calculadora");
this.setBounds(50,50,300,310);
this.getContentPane().setLayout(null);
//label - Titulo do Programa
lblTituloPrograma = new JLabel();
lblTituloPrograma.setText("Calculadora de Impostos");
lblTituloPrograma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/calculadora.png")));
lblTituloPrograma.setBounds(50,2,190,50);
this.add(lblTituloPrograma);
//label - valor total da nota
lblValorTotalNota = new JLabel();
lblValorTotalNota.setText("Valor total da Nota:");
lblValorTotalNota.setBounds(10,40,108,50);
this.add(lblValorTotalNota);
//textfield - valor total nota
txtValorTotalNota = new JTextField();
txtValorTotalNota.setBounds(110,55,70,20);
this.add(txtValorTotalNota);
//label - porcentagem taxa
lblPorcentagem = new JLabel();
lblPorcentagem.setText("Insira a taxa %");
lblPorcentagem.setBounds(70,75,108,50);
this.add(lblPorcentagem);
//label - lblIrrf
lblIrrf = new JLabel();
lblIrrf.setText("IRRF:");
lblIrrf.setBounds(40,95,108,50);
this.add(lblIrrf);
//textfield - irff
txtIrff = new JTextField();
txtIrff.setBounds(70,110,70,20);
this.add(txtIrff);
//label - lblPis
lblPis = new JLabel();
lblPis.setText("PIS:");
lblPis.setBounds(48,125,108,50);
this.add(lblPis);
//textfield - pis
txtPis = new JTextField();
txtPis.setBounds(70,140,70,20);
this.add(txtPis);
//label - lblCofins
lblCofins = new JLabel();
lblCofins.setText("COFINS:");
lblCofins.setBounds(27,150,108,50);
this.add(lblCofins);
//textfield - cofins
txtCofins = new JTextField();
txtCofins.setBounds(70,165,70,20);
this.add(txtCofins);
//label - lblCsll
lblCsll = new JLabel();
lblCsll.setText("CSLL:");
lblCsll.setBounds(42,180,108,50);
this.add(lblCsll);
//textfield - csll
txtCsll = new JTextField();
txtCsll.setBounds(70,195,70,20);
this.add(txtCsll);
//botao calcular
btnCalcular = new JButton();
btnCalcular.setText("Calcular");
btnCalcular.setBounds(100, 240, 90, 25);
this.add(btnCalcular);
//acao do botao calcular
btnCalcular.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
}
});
}
public static void main(String[] args) {
CalculadoraImposto exemplo = new CalculadoraImposto();
exemplo.setVisible(true);
}
}