Ligar button

3 respostas
Dindi

Bom dia!

Galera preciso de uma ideia para clacular este botão, pois não consigo realizar um calculo por tras!
Seria bem simples n1*n2/10…porem nao sei o comando.
Alguem poderia me ajudar!!

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

public class button extends JPanel {
    private JButton cal;
    private JTextField n1;
    private JLabel jcomp3;
    private JLabel jcomp4;
    private JTextField n2;
    private JLabel res;
    private JTextField resul;

    public button() {
        //construct components
        cal = new JButton ("Calcular");
        n1 = new JTextField (5);
        jcomp3 = new JLabel ("nota1");
        jcomp4 = new JLabel ("nota2");
        n2 = new JTextField (5);
        res = new JLabel ("Resultado");
        resul = new JTextField (5);

        //adjust size and set layout
        setPreferredSize (new Dimension (624, 531));
        setLayout (null);

        //add components
        add (cal);
        add (n1);
        add (jcomp3);
        add (jcomp4);
        add (n2);
        add (res);
        add (resul);

        //set component bounds (only needed by Absolute Positioning)
        cal.setBounds (155, 125, 140, 20);
        n1.setBounds (40, 75, 100, 25);
        jcomp3.setBounds (70, 40, 100, 25);
        jcomp4.setBounds (195, 40, 100, 25);
        n2.setBounds (160, 75, 100, 25);
        res.setBounds (290, 40, 100, 25);
        resul.setBounds (280, 75, 100, 25);
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("Botao");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new button());
        frame.pack();
        frame.setVisible (true);
    }
}

3 Respostas

Dindi

Sera que alguem tem um ideia???

ou sera q eh necessario importa alguma biblioteca?

R

Está faltando o ActionListener no botão de calcular :)

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class button extends JPanel {
	private JButton cal;
	private JTextField n1;
	private JLabel jcomp3;
	private JLabel jcomp4;
	private JTextField n2;
	private JLabel res;
	private JTextField resul;
	private ActionListener calcular;

	public button() {
		
		calcular = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				double resultado = (Double.parseDouble(n1.getText()) * Double.parseDouble(n2.getText())) / 10;
				resul.setText(String.valueOf(resultado));
			}
		};
		
		// construct components
		cal = new JButton("Calcular");
		cal.addActionListener(calcular);

		n1 = new JTextField(5);
		jcomp3 = new JLabel("nota1");
		jcomp4 = new JLabel("nota2");
		n2 = new JTextField(5);
		res = new JLabel("Resultado");
		resul = new JTextField(5);

		// adjust size and set layout
		setPreferredSize(new Dimension(624, 531));
		setLayout(null);

		// add components
		add(cal);
		add(n1);
		add(jcomp3);
		add(jcomp4);
		add(n2);
		add(res);
		add(resul);

		// set component bounds (only needed by Absolute Positioning)
		cal.setBounds(155, 125, 140, 20);
		n1.setBounds(40, 75, 100, 25);
		jcomp3.setBounds(70, 40, 100, 25);
		jcomp4.setBounds(195, 40, 100, 25);
		n2.setBounds(160, 75, 100, 25);
		res.setBounds(290, 40, 100, 25);
		resul.setBounds(280, 75, 100, 25);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Botao");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new button());
		frame.pack();
		frame.setVisible(true);
	}
}

desta maneira irá funcionar!

Dindi

mUITO OBRIGADA

deu certo, e ai ja vai da pra terminar o trabalho!!!

vlw :lol:

Criado 5 de março de 2008
Ultima resposta 5 de mar. de 2008
Respostas 3
Participantes 2