Está en la página 1de 6

TRABAJO EN CLASE (16 DE MARZO DE 2023)

Clase 1
Este código es de la clase que nos esta indicando la ejecución de la clase
Label401 para que nos ayude a crear un podo de interfaz gráfica.
import javax.swing.JFrame;
public class PruebaLabel {
public static void main(String[] args) {

Label401 plabel=new Label401();


plabel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
plabel.setSize(300, 400);//tamaño de la
plabel.setVisible(true);//true si se visualiza, false no se visualiza

}
}
Clase 2
import java.awt.*;//CON ASTERISCO TODAS LAS LIBERIAS, CON
NOMBRE SOLO ESA LIBRERIA
import javax.swing.*; //Todas las liberias de javax swing

public class Label401 extends JFrame{//JFrame es para definir...


private final JLabel label1;
private final JLabel label2;
private final JLabel label3;

public Label401(){
super("Hola mundo");
setLayout(new FlowLayout());
label1= new JLabel("esto es tesjo etiqueta solo texto");
label1.setToolTipText("grupo 401");
add(label1);

Icon bug=new
ImageIcon(getClass().getResource("iamhappy.jpg"));
label2=new JLabel("etiqueta 2 con icono e
imagen",bug,SwingConstants.CENTER); //SwingConstants: contantes
horizontal y vertical
label2.setToolTipText("ESTA ES TESJO, ETIQUETA 2");
add(label2);

label3=new JLabel();
label3.setText("etiqueta 3");
label3.setIcon(bug);
//label3.setHorizontalTextPosition(SwingConstants.BOTTOM);
label3.setHorizontalTextPosition(SwingConstants.RIGHT);
label3.setToolTipText("esta es etiqueta 3");
add(label3);
}
}

TRABAJO EN CLASE
CLASE MAIN
package texto;
import javax.swing.JFrame;
public class Texto {
public static void main(String[] args) {
TextoPrueba campoDeTexto = new TextoPrueba();
campoDeTexto.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
campoDeTexto.setSize(350, 150);
campoDeTexto.setVisible(true);
}

CLASE TEXTO
package texto;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class TextoPrueba extends JFrame{


private final JTextField texto1;
private final JTextField texto2;
private final JTextField texto3;
private final JPasswordField password;

public TextoPrueba(){
super ("ACCESO A INTERNET");
setLayout(new FlowLayout());
texto1 = new JTextField(12);
add(texto1);

texto2= new JTextField("Ingresa tu usuario: ");


add(texto2);

texto3 = new JTextField("ingresa tu contraseña");


//texto3.setEditable(false);
add(texto3);

//Construye campo de contraseña con texto predeterminado


password = new JPasswordField("Oculta el texto");
add(password);

//registra los manejadores de eventos


TextFieldHandler handler = new TextFieldHandler();
texto1.addActionListener(handler);
texto2.addActionListener(handler);
texto3.addActionListener(handler);
password.addActionListener(handler);
}

//Clase interna privada para el manejo de eventos


private class TextFieldHandler implements ActionListener{
//Procesa los eventos de texto
@Override
public void actionPerformed(ActionEvent event){
String string = "";
//usuario presionando enter en campo texto
if(event.getSource()==texto1)
string = String.format("Texto1: %s",event.getActionCommand());
//ususario presionando enter en campo texto2
else if(event.getSource()== texto2)
string= String.format("Texto2: %s",event.getActionCommand());
//ususario presionando enter en campo texto3
else if(event.getSource()== texto3)
string= String.format("Texto3: %s",event.getActionCommand());
//ususario presionando enter en campo password
else if(event.getSource()== password)
string= String.format("password: %s",event.getActionCommand());
//Muestra el contenido del objeto
JOptionPane.showMessageDialog(null, string);
}
}
}

TRABAJO EN CLASE
Clase de una suma en interfaz gráfica
import javax.swing.JOptionPane;
public class Entradassalidas {

public static void main(String[] args) {

String numeroUno=JOptionPane.showInputDialog("Ingresa el primer dato:");


String numeroDos=JOptionPane.showInputDialog("Ingresa el segundo
dato:");
int numero1=Integer.parseInt(numeroUno);
int numero2=Integer.parseInt(numeroDos);

int resultado=numero1+numero2;

JOptionPane.showMessageDialog(null,"El resultado es: "+resultado, " Hola,


esto es una suma",JOptionPane.ERROR_MESSAGE);
}

También podría gustarte