Foros del Web » Programación para mayores de 30 ;) » Java »

¿Como dar evento a un JButton?

Estas en el tema de ¿Como dar evento a un JButton? en el foro de Java en Foros del Web. LO QUE HACE ESTE CODIGO ES MOSTRAR UNA INTERFAZ PARA INGRESAR LOGIN Y PASSWORD *Lo que quiero es hacer funcionar a mi JButton al hacer ...
  #1 (permalink)  
Antiguo 10/10/2009, 19:21
 
Fecha de Ingreso: octubre-2009
Mensajes: 3
Antigüedad: 14 años, 6 meses
Puntos: 0
¿Como dar evento a un JButton?

LO QUE HACE ESTE CODIGO ES MOSTRAR UNA INTERFAZ PARA INGRESAR LOGIN Y PASSWORD
*Lo que quiero es hacer funcionar a mi JButton al hacer clic en aceptar me salga un mensaje ... si el login y password son correctos o no....gracias necesito ayuda urgente

Código HTML:
package user;
import javax.swing.*;

public class username extends JFrame{
	JPanel panel= new JPanel();
	JLabel label1 = new JLabel("Ingresar Usuario ");
	JTextField texto = new JTextField(10);
	JLabel label2 = new JLabel("Ingresar Password");
	JTextField texto2 = new JTextField(10);
	JButton miboton = new JButton("Aceptar");
	String user = texto.getText();
	String password = texto2.getText();	
	String a;
	public void mypanel(){
		//panel.add(panel);
		panel.add(label1);
		panel.add(texto);
		panel.add(label2);
		panel.add(texto2);
		panel.add(miboton);
		add(panel);
		super.setTitle("USUARIO");
		super.setVisible(true);
		super.setSize(300, 140);
		ActionListener listener = new ActionListener();
		JButton miboton = new JButton();
		//miboton.addActionListener(listener); 
		//texto.setText("login");
		//user=texto.getText();
		//texto2.setText("password");
		//user=texto.getText();
	}
	
	
	public static void main(String[] args) {
		username obj=new username();
				obj.mypanel();
			
	
	}
	
	
	
	//public void validarUsuario(){
		//if (user.equals("admin")&& password.equals("1234")){
		//JOptionPane.showMessageDialog(null, "OK");}
		//else{
			//JOptionPane.showMessageDialog(null, "El username o password no es correcto");
		//}
		
	//}

}
  #2 (permalink)  
Antiguo 10/10/2009, 20:32
 
Fecha de Ingreso: mayo-2007
Mensajes: 210
Antigüedad: 17 años
Puntos: 8
Respuesta: ¿Como dar evento a un JButton?

Te faltaron algunos detalles ahi te dejo el programa funcionando

Código PHP:
package user;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class 
username extends JFrame{
    
JPanel panel= new JPanel();
    
JLabel label1 = new JLabel("Ingresar Usuario ");
    
JTextField texto = new JTextField(10);
    
JLabel label2 = new JLabel("Ingresar Password");
    
JTextField texto2 = new JTextField(10);
    
JButton miboton = new JButton("Aceptar");
    
String user texto.getText();
    
String password texto2.getText();    
    
String a;
    public 
void mypanel(){
        
//panel.add(panel);
        
panel.add(label1);
        
panel.add(texto);
        
panel.add(label2);
        
panel.add(texto2);
        
panel.add(miboton);
        
add(panel);
        
super.setTitle("USUARIO");
        
super.setVisible(true);
        
super.setSize(300140);
        
ActionListener listener = new ActionListener(){

            public 
void actionPerformed(ActionEvent e) {
                
                
validarUsuario();
            }
            
        };
        
//miboton = new JButton();
        
miboton.addActionListener(listener); 
        
texto.setText("login");
        
//user=texto.getText();
        
texto2.setText("password");
        
//user=texto.getText();
    
}
    
    
    public static 
void main(String[] args) {
        
username obj=new username();
                
obj.mypanel();
            
    
    }
    
    
    
    public 
void validarUsuario(){
        if (
texto.getText().equals("admin")&& texto2.getText().equals("1234")){
            
JOptionPane.showMessageDialog(null"OK");}
        else{
            
JOptionPane.showMessageDialog(null"El username o password no es correcto");
        }
        
    }



salud2
  #3 (permalink)  
Antiguo 11/10/2009, 19:17
 
Fecha de Ingreso: octubre-2009
Mensajes: 3
Antigüedad: 14 años, 6 meses
Puntos: 0
Respuesta: ¿Como dar evento a un JButton?

gracias...me llego a funcionar. Pero ahora te mando otro x aqui xq no puedo enviar como un nuevo tema ya q me sale error....Porfavor ayudenme con esto, al escribir 2 numeros y hacer clic en sumar, tiene que salir la respuesta, al igual para restar, dividir y multiplicar....pero no me sale...AYUDENMEEEE PLEASE....aqui les dejo mi avance
Código:
package prueba;
import javax.swing.*;

public class oper extends JFrame{
	public oper(){
		super();
	}
	private static final long serialVersionUID = 1L;
	JPanel panel = new JPanel();
	JLabel label = new JLabel("Ingresar primer número");
	JTextField texto = new JTextField(10);
	JLabel label2 = new JLabel("Ingresar segundo número");
	JTextField texto2 = new JTextField(10);
	JButton boton = new JButton ("Sumar");
	JButton boton2 = new JButton ("Restar");
	JButton boton3 = new JButton ("Multiplicar");
	JButton boton4 = new JButton ("Dividir");
	public void mipanel(){
		add(panel);
		panel.add(label);
		panel.add(texto);
		panel.add(label2);
		panel.add(texto2);
		panel.add(boton);
		panel.add(boton2);
		panel.add(boton3);
		panel.add(boton4);
		super.setTitle("Operaciones Aritméticas");
		super.setVisible(true);
		super.setSize(300, 170);

		OyenteExternoAccion oyenteBotonUno = new OyenteExternoAccion(texto,texto2);
		boton.addActionListener(oyenteBotonUno);
		
	}
	public static void main(String[] args) {
		oper obj=new oper();
		obj.mipanel();

	}

}
Código:
package prueba;

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

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class OyenteExternoAccion implements ActionListener{
String x;
String y;
int num1;
int num2;
int suma;
private JTextField texto;
private JTextField texto2;

public OyenteExternoAccion (JTextField texto, JTextField texto2)
{
	this.texto = texto;
	this.texto2 = texto2;
		
	}

	@Override
	public void actionPerformed(ActionEvent evento) {
		JButton boton=(JButton) evento.getSource();
		x=texto.getText();
		y=texto2.getText();
		num1=Integer.parseInt(x);
		num2=Integer.parseInt(y);
		if (x.equals(num1)&& y.equals(num2)){
			suma = suma + num1;}
		
		
	}

}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:16.