Problems with ActionListener JAVA

1

Good I have a problem for a Java application for the Uni I am new to Java and I would like to ask for some help, it is a simple button to operate the division of two numbers and display them on the screen with a small check to show if that the user enters letters instead of numbers, but the buttons do not initiate the functions to which they are associated, here my code:

package graphics;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
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 PpalWindow extends JFrame implements ActionListener
{

/**
 * 
 */
private static final long serialVersionUID = 1L;
//ATTR's 
private Container cont;
private JPanel flowPanel, gridPanel;
private JButton operateButton, clearText;
private JLabel text1, text2, text3;
private JTextField field1,field2,field3;
private PpalWindow fatherFrame;

public  PpalWindow()
{
    this.cont = null;
    this.flowPanel= null;
    this.gridPanel = null;
    this.operateButton = null;
    this.clearText = null;
    this.text1 = null;
    this.text2 = null;
    this.text3 = null;
    this.field1= null;
    this.field2 = null;
    this.field3 = null;
    this.fatherFrame = null;


    //Inicializamos los componentes 
    makeFlowPanel();
    makeGridPanel();
    initializeComps();

}


// Método para hacer un set del atributo que guardará a este JFrame como el padre.
    public void setFatherFrame(PpalWindow pw) { PpalWindow pf = null;
    this.fatherFrame = pf; }





public void makeGridPanel()
{
    this.gridPanel= new JPanel();
    this.text1 = new JLabel("Distancia: ");
    this.text1.setFont(new Font("Arial",0,24));
    this.field1 = new JTextField(2);
    this.text2 = new JLabel("Tiempo: ");
    this.text2.setFont(new Font("Arial",0,24));
    this.field2 = new JTextField(2);
    this.text3 = new JLabel("Resultado: ");
    this.text3.setFont(new Font("Arial",0,24));
    this.field3 = new JTextField(2);

    GridLayout gl = new GridLayout(3,2,0,5);
    this.gridPanel.setLayout(gl);
    this.gridPanel.add(text1);
    this.gridPanel.add(field1);
    this.gridPanel.add(text2);
    this.gridPanel.add(field2);
    this.gridPanel.add(text3);
    this.gridPanel.add(field3);     
}

public void makeFlowPanel()
{
    this.flowPanel = new JPanel();
    this.operateButton = new JButton("Calcular");
    this.operateButton.setFont(new Font("Arial",0,18));
    this.clearText = new JButton("Borrar");
    this.clearText.setFont(new Font("Arial",0,18));
    this.flowPanel.setLayout(new FlowLayout());
    this.flowPanel.add(operateButton);
    this.flowPanel.add(clearText);

}
public void initializeComps() 
{
    this.setTitle("Calculo de velocidad de un  projectil");
    this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.setSize(600, 200);
    cont= getContentPane();
    cont.setLayout(new BorderLayout());
    cont.add(this.gridPanel,BorderLayout.CENTER);
    cont.add(this.flowPanel,BorderLayout.SOUTH);

}

@Override
public void actionPerformed(ActionEvent e) 
{
    //Variables Locales 
     AlertWindow aw = null;
     double number1 = 0d;
     double number2 = 0d;
     int error = 0;

     // Exepcion para verificar si se ingresan numeros o letras en los campos 
     if (e.getSource()==this.clearText)
     {
         textClear();// Funcion que limpia los campos 
     }
     else if (e.getSource()==this.operateButton){
            try{
                    number1= Double.parseDouble(this.field1.getText());
                    number2 = Double.parseDouble(this.field2.getText());
               }
            catch(NumberFormatException nfe){ error = 1; }

            if(error !=0)
                {
                    // Instanciamos la ventana de alerta
                    aw = new AlertWindow(this.fatherFrame,true);
                    //asignamos el mensaje a mostrar 
                    if(error==1){aw.message.setText("Debe Ingresar Numero Reales"); }
                }
            else
                {
                    this.operate(number1,number2);
                }
         }

}

public void operate(double n1, double n2)
{
    // Variable local
    double result = 0d;
    result = n1/n2;
    this.field3.setText(" "+result);
}

public void textClear()
{
    this.field1.setText("");
    this.field2.setText("");
}
}

the AlertWindow window looks like this:

package graphics;

import java.awt.FlowLayout;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import graphics.PpalWindow;

public class AlertWindow extends JDialog 
{
/**
 * 
 */
private static final long serialVersionUID = 1L;
JPanel panel;
JLabel message;

public AlertWindow(PpalWindow pw, boolean isModal)
{
    super(pw, isModal);
    this.setTitle("CUIDADO!");
    this.setSize(200, 100);
    this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.panel = new JPanel();
    this.panel.setLayout(new FlowLayout());
    this.message = new JLabel();
}

public void AddComponents()
{
    this.panel.add(this.message);
    this.add(this.panel);
}
}

and the Main is like this:

package main;
import graphics.PpalWindow;
public class Projectil {

public static void main(String[] args) {

    PpalWindow pw = new PpalWindow();
    pw.setVisible(true);
}

}
    
asked by Alvaro Santafe 16.04.2017 в 01:04
source

1 answer

0

It seems that you are missing only a small detail:

public void makeFlowPanel()
{
    this.flowPanel = new JPanel();
    this.operateButton = new JButton("Calcular");
    this.operateButton.setFont(new Font("Arial",0,18));
    this.clearText = new JButton("Borrar");
    this.clearText.setFont(new Font("Arial",0,18));
    this.flowPanel.setLayout(new FlowLayout());
    this.flowPanel.add(operateButton);
    this.flowPanel.add(clearText);
    // aquí entra la mágia:
    operateButton.addActionListener(this);
    clearText.addActionListener(this);
}

It is not enough to implement ActionListener , you also have to add it in the component that should emit the event. You can do it in several parts of your code, it seemed to me how you organized your code, it made sense to place it where I showed it to you.

I made your dialogue, you never call AddComponents of your AlertWindow . As your alert is very simple, I recommend you not to overcomplicate things and use a simple JOptionPane -dialog:

    if(error !=0){
        JOptionPane.showMessageDialog(this,
            "Debe ingresar números reales.", // mensaje
            "error", // valor
            JOptionPane.ERROR_MESSAGE); // icono
    }
    
answered by 16.04.2017 / 01:28
source