0

Tengo una consulta. Tengo el siguiente código java y quiero que el botón saludar me capture el nombre que digite el usuario y cuando le de en el botón saludar me lo muestre en el Jframe.

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 Ventanita extends JFrame implements ActionListener {

public Ventanita() {
    jPanel.add(label);
    jPanel.add(jTextField);
    jPanel.add(jButton);
    jPanel.add(label1);
    jPanel.add(label2);
    add(jPanel);

}

public static void main(String[] args) {

    JFrame x = new Ventanita();
    x.setSize(400, 400);
    x.setVisible(true);        
    x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

JLabel label = new JLabel("Nombre");
JTextField jTextField = new JTextField("10");
JButton jButton = new JButton("Saluda");

JLabel label1 = new JLabel("Hola");
JLabel label2 = new JLabel(" ");
JPanel jPanel = new JPanel();

@Override
public void actionPerformed(ActionEvent ae) {

}

}
2
  • Pero el ejemplo es funcional, al menos se muestra el formulario??
    – Dacoso
    el 4 sep. 2017 a las 13:12
  • Si claro muestra el formulario. el 4 sep. 2017 a las 13:17

1 respuesta 1

1

Con base al ejemplo que diste, ya tu editas las posiciones de los objetos, te recomiendo que repases.

package pruebasdemetodos;

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

/**
 *
 * @author HANSELC2
 */
public class Ventanita extends JFrame implements ActionListener {

    JLabel label = new JLabel("Nombre");
    JTextField jTextField = new JTextField("10");
    JButton jButton = new JButton("Saluda");

    JLabel label1 = new JLabel("Hola");
    JLabel label2 = new JLabel("Cambio");
    JPanel jPanel = new JPanel();
public Ventanita() {
    jButton.addActionListener(this);
    jPanel.add(label);
    jPanel.add(jTextField);
    jPanel.add(jButton);
    jPanel.add(label1);
    jPanel.add(label2);
    add(jPanel);

}

public static void main(String[] args) {

    JFrame x = new Ventanita();
    x.setSize(400, 400);
    x.setVisible(true);        
    x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}



@Override
public void actionPerformed(ActionEvent ae) {
    label2.setVisible(true);
    label2.setText(jTextField.getText());
}


}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.