lunes, 27 de febrero de 2023

JFRAME

 javasx.swing.JFrame deriva de varias clases, es una clase perteneciente a la biblioteca gráfica de java para poner ventanas y objetos

En el caso de windows y usando netbeans se puede usar un jframe y es extremadamente sencillo, solo arrastrar los elementos como si estuvieras usando word

Para hacerlo con eclipse en linux en help windowsmarket se agrega el plugin de window, despues se agrega el jframe y en contentPane se pone el Layout en Absolute Layout para poder acomodar los iconos al gusto.






------------------------VENTANA

package ventana;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.ButtonGroup;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

public class Ventana extends JFrame {

private JPanel panel; // para usar este atributo en iniciarcomponentes

private JLabel saludo;

private JTextField cajaTexto;

private JButton boton;

private JButton botonloco;

private JTextArea areaTexto;

private JTextField cajaTexto2;

// Constructor

public Ventana() {

setTitle("Taquitos"); // establece título

setSize(600, 600); // setZise establece el tamaño ancho y largo

// setLocation(2600, 300); // posicion inicial de la ventana

// setBounds(2600, 300, 500, 500); // x,y,ancho,largo, unión de size y

// location

setLocationRelativeTo(null); // Pone la ventana en el centro

// setResizable(false); // establece si puede cambiar de tamaño o no

setMinimumSize(new Dimension(200, 200)); // tamaño minimo

// this.getContentPane().setBackground(Color.BLACK); // color de la

// ventana

iniciarComponentes();

setDefaultCloseOperation(EXIT_ON_CLOSE); // al cerrar la ventana el

// programa se cierra

}

private void iniciarComponentes() {

colocarPaneles();

colocarEtiquetas();

colocarBotones();

colocarRadioBotones();

colocarCajaDeTexto();

colocarCajasDeTexto();

colocarAreasDeTexto();

colocarListasDesplegables();

}

private void colocarPaneles() {

panel = new JPanel(); // crea un panel, aqui se instancia el panel que

// viene

// de ventana

panel.setLayout(null); // desactiva el diseño del panel

panel.setBackground(Color.WHITE);// Color del panel

this.getContentPane().add(panel); // this nos referimos a la ventana al

// JFrame, agrega panel a la ventana

}

private void colocarEtiquetas() {

// Etiqueta texto

JLabel etiqueta = new JLabel("Hola, ¿qué quieres comer?", SwingConstants.CENTER); // crea

// las

// etiquetas, swing funciona con center, left,right

etiqueta.setBounds(350, 20, 600, 80);

etiqueta.setForeground(Color.BLACK); // color de letra

etiqueta.setBackground(Color.YELLOW);

etiqueta.setOpaque(true);// permite cambiar el fondo de la etiqueta,

// igual se puede usar en los botones

etiqueta.setFont(new Font("cooper black", Font.PLAIN, 30));// establece

// la fuente del texto por nombre, estilo y tamaño

panel.add(etiqueta); // agrega etiqueta al panel

// Etiqueta imagen

ImageIcon imagen = new ImageIcon("1LENTESGUY.png");

JLabel etiqueta2 = new JLabel(imagen);

// etiqueta2.setIcon(new ImageIcon("1LENTESGUY.png"); //es otro modo

// JLabel etiqueta2 = new JLabel(new ImageIcon("1LENTESGUY.png")); //

// esta es una forma de poner la imagen

etiqueta2.setBounds(50, 30, 300, 300);

// etiqueta2.setIcon(new

// ImageIcon(imagen.getImage().getScaledInstance(100, 100,

// Image.SCALE_SMOOTH)));

// smooth es para calidad suave el get hace que la imagen se une a la

// etiqueta

etiqueta2.setIcon(new ImageIcon(

imagen.getImage().getScaledInstance(etiqueta2.getWidth(), etiqueta2.getHeight(), Image.SCALE_SMOOTH)));

// la imagen debe tener el mismo ancho width que la etiqueta

panel.add(etiqueta2);

}

private void colocarBotones() {

JButton boton1 = new JButton("click");

boton1.setBounds(550, 350, 150, 50); // largo, altura

boton1.setEnabled(true);// permite que el botón se pueda pulsar o no

boton1.setMnemonic('a'); // se pone alt y la tecla y funciona

boton1.setForeground(Color.BLUE);

boton1.setFont(new Font("cooper black", 3, 20));

panel.add(boton1);

boton = new JButton();

boton.setBounds(550, 500, 200, 200);

// boton2.setBackground(Color.BLUE);

ImageIcon clicAqui = new ImageIcon("nubetexto.png");

boton.setIcon(new ImageIcon(

clicAqui.getImage().getScaledInstance(boton.getWidth(), boton.getHeight(), Image.SCALE_SMOOTH)));

panel.add(boton);

saludo = new JLabel();

saludo.setBounds(50, 200, 300, 40);

saludo.setFont(new Font("arial", 1, 20));

panel.add(saludo);

eventoOyenteDeAccion();

botonloco = new JButton("click");

botonloco.setBounds(750, 250, 150, 50);

botonloco.setEnabled(true);

botonloco.setMnemonic('a');

botonloco.setForeground(Color.BLUE);

botonloco.setFont(new Font("cooper black", 3, 20));

panel.add(botonloco);

eventoOyenteDeRaton();

}

private void eventoOyenteDeAccion() {

ActionListener oyenteDeAccion = new ActionListener() { // sirve para

// instanciar un

// objeto

public void actionPerformed(ActionEvent ae) { // lo convierte en

// clase anonima

saludo.setText("Hola " + cajaTexto.getText());// aqui pones la

// acción

saludo.setBounds(20, 500, 500, 30);

}

};

boton.addActionListener(oyenteDeAccion);

}

private void eventoOyenteDeRaton() {

MouseListener oyenteDeRaton = new MouseListener() {

public void mouseClicked(MouseEvent me) {

areaTexto.append("Presionar dentro\n");

}

public void mousePressed(MouseEvent me) {

areaTexto.append("Presionar fueran");

}

public void mouseReleased(MouseEvent me) {

areaTexto.append("Presionar y soltar fuera\n");

}

public void mouseEntered(MouseEvent me) {

areaTexto.append("Pasar con el cursor\n");

}

public void mouseExited(MouseEvent me) {

areaTexto.append("Salir del icono\n");

}

};

botonloco.addMouseListener(oyenteDeRaton);

}

private void colocarCajaDeTexto() {

cajaTexto = new JTextField("mmm creo que mejor quiero ");

cajaTexto.setBounds(500, 450, 300, 40);

panel.add(cajaTexto);

}

private void colocarRadioBotones() {

JRadioButton radioBoton1 = new JRadioButton("Taquitos", false);

radioBoton1.setBounds(450, 100, 200, 50);

radioBoton1.setFont(new Font("cooper", 0, 20));

radioBoton1.setBackground(Color.WHITE);

panel.add(radioBoton1);

JRadioButton radioBoton2 = new JRadioButton("Hamburguesa", false);

radioBoton2.setBounds(450, 150, 200, 50);

radioBoton2.setFont(new Font("cooer", 1, 20));

radioBoton2.setBackground(Color.WHITE);

panel.add(radioBoton2);

JRadioButton radioBoton3 = new JRadioButton("Sandía", false);

radioBoton3.setBounds(450, 200, 200, 50);

radioBoton3.setFont(new Font("coor", 2, 20));

radioBoton3.setBackground(Color.WHITE);

panel.add(radioBoton3);

ButtonGroup grupoRadioBotones = new ButtonGroup();

grupoRadioBotones.add(radioBoton1);

grupoRadioBotones.add(radioBoton2);

grupoRadioBotones.add(radioBoton3);

}

private void colocarCajasDeTexto() {

cajaTexto2 = new JTextField("mmm quiero ");

// JTextField cajaTexto2 = new JTextField("mmm quiero "); asi se jala

// como local

cajaTexto2.setBounds(450, 250, 200, 30);

// cajaTexto.setText("Hola..."); //poner texto en la caja

// System.out.println("Texto en la caja: " + cajaTexto.getText());

// //info en sistema

panel.add(cajaTexto2);

eventosDelTeclado();

}

private void colocarAreasDeTexto() {

areaTexto = new JTextArea("Escriba el texto aqui...");

areaTexto.append("\nPor aqui..."); // añade mas texto al area

areaTexto.setEditable(false); // se pueda o no modificar el area de

// texto

areaTexto.setBounds(760, 350, 350, 350);

areaTexto.setBackground(Color.YELLOW);

panel.add(areaTexto);

}

private void colocarListasDesplegables() {

String[] paises = {"México", "Canada", "Argentina", "EEUU"};

JComboBox listaDesplegable = new JComboBox(paises);

listaDesplegable.setBounds(220, 400, 150, 50);

listaDesplegable.addItem("Perú"); // agregar objetos al string

listaDesplegable.setSelectedItem("Argentina"); // seleccionar primer

// objeto

// visto

panel.add(listaDesplegable);

}

private void eventosDelTeclado() {

KeyListener eventoTeclado = new KeyListener() {

@Override

public void keyTyped(KeyEvent e) {

areaTexto.append("solo admite alfanumerico\n");

}

@Override

public void keyPressed(KeyEvent e) {

areaTexto.append("keypressed\n");

}

@Override

public void keyReleased(KeyEvent e) {

if (e.getKeyChar() == 'p') {

areaTexto.append("presionar y soltar\n");

}

}

};

cajaTexto2.addKeyListener(eventoTeclado);

}

}

____________________---______PRINCIPAL

package ventana;

public class Principal {

public static void main(String[] args) {

Ventana v1 = new Ventana();

v1.setVisible(true); // hace visible la ventana

}

}


No hay comentarios:

Publicar un comentario