Ajuda com evento de botao

1 resposta
D

Ola a todos
to com um problema
tenho q fazer um programa q calcula media usando botoes e jlabel
o codigo esta assim

package primeiroframe;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PrimeiroFrame extends JFrame {
    
    private JLabel lbl01,lbl02,lbl03,lbl04,lbltitulo,tftr;
    private JTextField tftn01;
    private JTextField tftn02;
    private JTextField tftn03;
    private JTextField tftn04;
    private JButton btcalcular;
    private JButton btlinpar;
        
    public PrimeiroFrame() {

    Container container = getContentPane ();
    container.setLayout(new FlowLayout());
    
            setTitle("Meu primeiro programa Visual"); 
            setBounds(0,0,350,400);
            setLayout(null);
            setVisible(true);
//Adcionando os JLabel    
        lbltitulo = new JLabel();
        lbltitulo.setBounds(40,35,210,25);
        lbltitulo.setText("Calculando media com JAVA.");
        add(lbltitulo);
        
        lbl01 = new JLabel("PRIMEIRO LABEL");
        lbl01.setToolTipText("Nota de n°01");
        lbl01.setBounds(40,75,60,25);
        lbl01.setText("Nota n°01"); 
        container.add( lbl01 );
        
        lbl02 = new JLabel();
        lbl02.setToolTipText("Nota n°2");
        lbl02.setBounds(40,125,60,25);
        lbl02.setText("Nota n°02");
        container.add(lbl02);
        
        lbl03 = new JLabel();
        lbl03.setToolTipText("Nota de n°03");
        lbl03.setBounds(40,175,60,25);
        lbl03.setText("Nota n°03");
        add(lbl03);
        
        lbl04 = new JLabel();
        lbl04.setToolTipText("Nota de n°04");
        lbl04.setBounds(40,225,60,25);
        lbl04.setText("Nota n°04");
        add(lbl04);
        
        tftr = new JLabel();
        tftr.setToolTipText("Resultado Da media");
        tftr.setBounds(40,325,62,25);
        tftr.setText("Resultado");
        add(tftr);
//AQUI TERMIMA OS JLABELS
        
        
        
//AQUI COMEÇA OS JTEXTFIELD
        
        tftn01 = new JTextField("Digite nota 01");
        tftn01.setToolTipText("Aqui sera digitado a nota de n°01");
        tftn01.setBounds(110,75,65,25);
        tftn01.setText("Nota n°01");
        add(tftn01);
        
        tftn02 = new JTextField("Digite nota 02");
        tftn02.setToolTipText("Aqui sera digitado a nota de n°02");
        tftn02.setBounds(110,125,65,25);
        tftn02.setText("Nota n°02");
        add(tftn02);
        
        tftn03 = new JTextField("Digite nota 03");
        tftn03.setToolTipText("Aqui sera digitado a nota de n°03");
        tftn03.setBounds(110,175,65,25);
        tftn03.setText("Nota n°03");
        add(tftn03);
        
        tftn04 = new JTextField("Digite nota 04");
        tftn04.setToolTipText("Aqui sera digitado a nota de n°04");
        tftn04.setBounds(110,225,65,25);
        tftn04.setText("Nota n°04");
        add(tftn04);
        
//AQUI TERMINA O JTEXTFIELD
        
        
        //AQUI COMEÇA OS JBUTTONS
        btcalcular = new JButton();
        btcalcular.setToolTipText("Clique para fazer o calculo da media");
        btcalcular.setBounds(40,275,85,25);
        btcalcular.setText("Calcular");
        add(btcalcular);
        
        btlinpar = new JButton();
        btlinpar.setToolTipText("Clique para fazer um novo calculo da media");
        btlinpar.setBounds(130,275,85,25);
        btlinpar.setText("Novo");
        add(btlinpar);
        
//AQUI TERMINA OS JBUTTONS
      
    }
 
private class EventodBotao implements ActionListener {
        double n1,n2,n3,n4,media;

    public void ActionPerformed( ActionEvent e) {
    btcalcular.addActionListener(e);
        double n1,n2,n3,n4,media;
        
        String r = "";
                n1 = Double.parseDouble(tftn01.getText());
		n2 = Double.parseDouble(tftn02.getText());
		n3 = Double.parseDouble(tftn03.getText());
		n4 = Double.parseDouble(tftn04.getText());

                   media = (n1 + n2 + n3 + n4)/4;
                	
                     String R  = String.valueOf(media);
                     tftr.setText("Resultado.: "+R);
         }                  
}    
    public static void main(String[] args) {
       PrimeiroFrame application = new PrimeiroFrame();
   
       
       application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
}

estou com problemas nessa parte

private class EventodBotao implements ActionListener {
        double n1,n2,n3,n4,media;

    public void ActionPerformed( ActionEvent e) {
    btcalcular.addActionListener(e);
        double n1,n2,n3,n4,media;
        
        String r = "";
                n1 = Double.parseDouble(tftn01.getText());
		n2 = Double.parseDouble(tftn02.getText());
		n3 = Double.parseDouble(tftn03.getText());
		n4 = Double.parseDouble(tftn04.getText());

                   media = (n1 + n2 + n3 + n4)/4;
                	
                     String R  = String.valueOf(media);
                     tftr.setText("Resultado.: "+R);
         }                  
}    
    public static void main(String[] args) {
       PrimeiroFrame application = new PrimeiroFrame();
   
       
       application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
}

1 Resposta

Rafael_Costa_Teixeir

Ola !!! ,

dnllll ,

Fiz algumas modificações na sua classe da uma olhada abaixo:

// package primeiroframe;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PrimeiroFrame extends JFrame {
    
    private JLabel lbl01,lbl02,lbl03,lbl04,lbltitulo,tftr;
    private JTextField tftn01;
    private JTextField tftn02;
    private JTextField tftn03;
    private JTextField tftn04;
    private JButton btcalcular;
    private JButton btlinpar;
        
    public PrimeiroFrame() {

    Container container = getContentPane ();
    container.setLayout(new FlowLayout());
    
            setTitle("Meu primeiro programa Visual"); 
            setBounds(0,0,350,400);
            setLayout(null);
          
//Adcionando os JLabel    
        lbltitulo = new JLabel();
        lbltitulo.setBounds(40,35,210,25);
        lbltitulo.setText("Calculando media com JAVA.");
        add(lbltitulo);
        
        lbl01 = new JLabel("PRIMEIRO LABEL");
        lbl01.setToolTipText("Nota de n°01");
        lbl01.setBounds(40,75,60,25);
        lbl01.setText("Nota n°01"); 
        container.add( lbl01 );
        
        lbl02 = new JLabel();
        lbl02.setToolTipText("Nota n°2");
        lbl02.setBounds(40,125,60,25);
        lbl02.setText("Nota n°02");
        container.add(lbl02);
        
        lbl03 = new JLabel();
        lbl03.setToolTipText("Nota de n°03");
        lbl03.setBounds(40,175,60,25);
        lbl03.setText("Nota n°03");
        add(lbl03);
        
        lbl04 = new JLabel();
        lbl04.setToolTipText("Nota de n°04");
        lbl04.setBounds(40,225,60,25);
        lbl04.setText("Nota n°04");
        add(lbl04);
        
        tftr = new JLabel();
        tftr.setToolTipText("Resultado Da media");
        tftr.setBounds(40,325,62,25);
        tftr.setText("Resultado");
        add(tftr);
//AQUI TERMIMA OS JLABELS
        
        
        
//AQUI COMEÇA OS JTEXTFIELD
        
        tftn01 = new JTextField("Digite nota 01");
        tftn01.setToolTipText("Aqui sera digitado a nota de n°01");
        tftn01.setBounds(110,75,65,25);
        tftn01.setText("Nota n°01");
        add(tftn01);
        
        tftn02 = new JTextField("Digite nota 02");
        tftn02.setToolTipText("Aqui sera digitado a nota de n°02");
        tftn02.setBounds(110,125,65,25);
        tftn02.setText("Nota n°02");
        add(tftn02);
        
        tftn03 = new JTextField("Digite nota 03");
        tftn03.setToolTipText("Aqui sera digitado a nota de n°03");
        tftn03.setBounds(110,175,65,25);
        tftn03.setText("Nota n°03");
        add(tftn03);
        
        tftn04 = new JTextField("Digite nota 04");
        tftn04.setToolTipText("Aqui sera digitado a nota de n°04");
        tftn04.setBounds(110,225,65,25);
        tftn04.setText("Nota n°04");
        add(tftn04);
        
//AQUI TERMINA O JTEXTFIELD
        
        
        //AQUI COMEÇA OS JBUTTONS
        btcalcular = new JButton();
        btcalcular.setToolTipText("Clique para fazer o calculo da media");
        btcalcular.setBounds(40,275,85,25);
        btcalcular.setText("Calcular");
        add(btcalcular);
        
        btlinpar = new JButton();
        btlinpar.setToolTipText("Clique para fazer um novo calculo da media");
        btlinpar.setBounds(130,275,85,25);
        btlinpar.setText("Novo");
        add(btlinpar);
        
//AQUI TERMINA OS JBUTTONS
      
    
 // DNLL AQUI ABAIXO FIZ ALGUMAS MODIFICAÇÕES

btcalcular.addActionListener( new ActionListener() {
        //double n1,n2,n3,n4,media;

    public void actionPerformed( ActionEvent e) {
    // btcalcular.addActionListener(e);
        double n1,n2,n3,n4,media;
        
        String r = "";
                n1 = Double.parseDouble(tftn01.getText());
		n2 = Double.parseDouble(tftn02.getText());
		n3 = Double.parseDouble(tftn03.getText());
		n4 = Double.parseDouble(tftn04.getText());

                   media = (n1 + n2 + n3 + n4)/4;
                	
                     String R  = String.valueOf(media);
                     tftr.setText("Resultado.: "+R);
         }                  
	
	});

// TAMBEM RETIREI O SetVisible(TRUE) que estava la em cima e coloquei aqui em baixo.
	  setVisible(true);
}    
    public static void main(String[] args) {
       PrimeiroFrame application = new PrimeiroFrame();
   
       
       application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
}

[EDIT]

E compilei assim:

C:\Documents and Settings\Rafael\Desktop>javac PrimeiroFrame.java

C:\Documents and Settings\Rafael\Desktop>java PrimeiroFrame

C:\Documents and Settings\Rafael\Desktop>
Criado 8 de julho de 2011
Ultima resposta 8 de jul. de 2011
Respostas 1
Participantes 2