dÚvida projeto

2 respostas
O

Pessoal, tou criando um programinha básico, to fazendo o curso e aprendi hj a fazer os botões, mais não tou conseguindo fazer o BOTÂO Calcular INTERAGIR com os resultados informados nos JLabell

Ao final, deveria calcular o Valor Montante e dar o resultado, já fiz td e já sei onde eu to errando, deu branco e não tou conseguindo mostrar o Resultado nakelas janelinhas de AVISO, queria mostar resultado na TELA WARNING se conseguirem me ajudar, me ajudem onde ta o erro ??? O Erro provavelmente ta onde eu "empaquei" na linha 74 há 95. Obrigado

// Importa os pacotes necessários
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Hipoteca extends JFrame {

    // Bloco 1 - Dados dos Objetos da Janela
     // Bloco 1 - Objetos da Janela
    private JLabel montante;
    private JLabel juros;
    private JLabel tempo;
    private JTextField val1;
    private JTextField val2;
    private JTextField val3;
    private JButton calcula;

    public Hipoteca() {
        // Bloco 2 - Dados da Criação da Janela
         // Bloco 2 - Definição dos dados da Janela
        this.getContentPane().setLayout(null);
        this.getContentPane().setBackground(new Color(238, 238, 238));
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(new java.awt.Dimension(336, 343));
        this.setLocation((screenSize.width-336)/2,(screenSize.height-343)/2);
        this.setTitle("Hipoteca");
        this.setResizable(false);

        // Bloco 3 - Dados da Criação dos Controles na Janela
        
        // Bloco 3 - Criação dos Objetos na Janela
        montante = new JLabel("Montante");
        montante.setBounds(new Rectangle(14, 56, 57, 13));
        this.getContentPane().add(montante, null);
        juros = new JLabel("Juros (Anual)");
        juros.setBounds(new Rectangle(16, 92, 132, 13));
        this.getContentPane().add(juros, null);
        tempo = new JLabel("Tempo (anos)");
        tempo.setBounds(new Rectangle(16, 129, 89, 13));
        this.getContentPane().add(tempo, null);
        val1 = new JTextField();
        val1.setBounds(new Rectangle(108, 51, 100, 21));
        this.getContentPane().add(val1, null);
        val2 = new JTextField();
        val2.setBounds(new Rectangle(109, 89, 100, 21));
        this.getContentPane().add(val2, null);
        val3 = new JTextField();
        val3.setBounds(new Rectangle(111, 128, 100, 21));
        this.getContentPane().add(val3, null);
        calcula = new JButton("Calcular");
        calcula.setBounds(new Rectangle(88, 174, 143, 30));
        this.getContentPane().add(calcula, null);
        calcula.addActionListener (new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Chamada a um método
            }
        });

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                aoFechar();
            }
        });
        this.setVisible(true);
    }

    private void aoFechar() {
        System.exit(0);
    }

    // Insira aqui os métodos para os eventos
    
    public void calcular() {
    
   
    double montante= Double.parseDouble(val1.getText()) ;
    double juros= Double.parseDouble(val2.getText()) ;
    double tempo= Double.parseDouble(val3.getText()) ;
    
    double p1 = montante * juros;
    double p2 = juros/12;
    double p3 = (1-(1+p2));
    

    

               // ERRO ESTÁ POR AQUI NESTE CAMPO, COMO FAÇO PARA ACIONAR O BOTÃO "OK" E CALCULAR O QUE FOI INFORMADO NA CAIXA DE TEXTO DO JUROS, MONTANTE E TEMPO, O CÁLCULO É O DE CIMA P1,P2 E P3 ? DANDO RESULTADO NA JANELA DE WARNING MESSAGE COM O RESULTADO ESPERADO ?
              
               
                  //WARNING_MESSAGE - refere-se a uma mensagem de aviso
                  JOptionPane.showMessageDialog(this,"Hipoteca:","AVISO!!!"
                  ,JOptionPane.WARNING_MESSAGE);
                  
                   calcula= (p1/p3);

                   
                  

    
                  
    }


    public static void main(String args[]) {
        new Hipoteca();
    }

2 Respostas

orobsonpires

Olá,

o método ou código responsável pela ação do botão deve ser chamado dentro do método actionPerformed

calcula.addActionListener (new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                // código ou método aqui
            }  
        });
ViniGodoy

Não escreva títulos com letras maiúsculas.
E não duplique tópicos.

Deixe os tópicos sobre interface gráfica apenas no fórum de interface gráfica.

Criado 23 de maio de 2011
Ultima resposta 23 de mai. de 2011
Respostas 2
Participantes 3