Curso de Java Netbeans Completo☕ [79.- Agrear imágenes a nuestras etiquetas (jlabel + images)]
En este tutorial vamos a agrear imágenes a nuestras etiquetas y vamos a interactuar entre el texto de nuestras etiquetas y sus respectivas imágenes.
Java swing: https://es.wikipedia.org/wiki/Swing_(biblioteca_gr%C3%A1fica)
Codigo: https://github.com/programadornovato/java/commit/409723bd07a95d22f9b2269b91a1e60346ac61e1
package com.programadornovato.proy1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Image;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
/**
*
* @author eugenio
*/
public class Ventana extends JFrame{
JPanel panel1;
JPanel panel2;
JPanel panel3;
JLabel e1;
JLabel e2;
JLabel e3;
/*
public Ventana() throws HeadlessException {
Dimension d=new Dimension(500, 500);
this.setSize(d);
}
*/
public Ventana(String title) throws HeadlessException, InterruptedException {
super(title);
//Dimension d=new Dimension(500, 500);
//this.setSize(d);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setLocation(300, 300);
this.setBounds(300, 300, 500, 500);
this.setLocationRelativeTo(null);
iniciarPaneles();
inicaEtiquetas();
}
protected void iniciarPaneles() {
JPanel contenedor=new JPanel();
this.getContentPane().add(contenedor);
contenedor.setBackground(Color.red);
this.panel1=new JPanel();
this.panel2=new JPanel();
this.panel3=new JPanel();
contenedor.add(this.panel1);
contenedor.add(this.panel2);
contenedor.add(this.panel3);
this.panel1.setBackground(new Color(0, 51, 102));
this.panel2.setBackground(new Color(0, 102, 255));
this.panel3.setBackground(new Color(255, 0, 255));
contenedor.setLayout(new BoxLayout(contenedor, BoxLayout.X_AXIS));
//this.panel3.setVisible(false);
}
protected void inicaEtiquetas() {
//ImageIcon imagen1=new ImageIcon("images/netbeans 11.png");
//ImageIcon imagenEscala=new ImageIcon(imagen1.getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT));
e1=new JLabel("<html>Hola1</html>",new ImageIcon(new ImageIcon("images/netbeans 11.png").getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT)),SwingConstants.LEFT);
e2=new JLabel("<html>Hola2</html>");
e3=new JLabel("h");
this.panel1.add(e1);
this.panel2.add(e2);
this.panel3.add(e3);
e1.setForeground(Color.white);
e2.setForeground(Color.white);
e3.setForeground(Color.white);
this.panel1.setLayout(null);
this.panel2.setLayout(null);
this.panel3.setLayout(null);
e1.setBounds(10, 100, 120, 60);
e2.setBounds(10, 100, 120, 60);
e3.setBounds(10, 100, 120, 60);
//e1.setText("<html>Hola humano</html>");
//this.panel2.setVisible(false);
e1.setOpaque(false);
e2.setOpaque(true);
e3.setOpaque(true);
e1.setBackground(Color.black);
e2.setBackground(Color.black);
e3.setBackground(Color.black);
/*
HorizontalAlignment
CENTER = 0;
LEFT = 2;
RIGHT = 4;
VerticalAlignment
TOP = 1;
BOTTOM = 3;
*/
e1.setHorizontalAlignment(SwingConstants.LEFT);
e2.setHorizontalAlignment(SwingConstants.CENTER);
e3.setHorizontalAlignment(SwingConstants.RIGHT);
e1.setVerticalAlignment(SwingConstants.TOP);
e2.setVerticalAlignment(SwingConstants.BOTTOM);
/*
name
DIALOG = "Dialog";
DIALOG_INPUT = "DialogInput";
SANS_SERIF = "SansSerif";
SERIF = "Serif";
style
PLAIN = 0;
BOLD = 1;
ITALIC = 2;
*/
e1.setFont(new Font("Megatron",Font.ITALIC,15));
e2.setFont(new Font("Face Your Fears",Font.BOLD,20));
e3.setFont(new Font("ObelixPro",Font.PLAIN,25));
e2.setIcon(new ImageIcon(new ImageIcon("images/netbeans 11.png").getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT)));
e3.setIcon(new ImageIcon(new ImageIcon("images/netbeans 11.png").getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT)));
}
public void setTextos(String textos[]){
this.e1.setText(textos[0]);
this.e2.setText(textos[1]);
this.e3.setText(textos[2]);
}
}
🔗 Instalar Netbeans 11 en Ubuntu con Snap [Mas fácil que en windows?]: https://www.youtube.com/watch?v=LllPPV9SMzQ
🔗 Instalar Netbeans 11 en Windows 10: https://www.youtube.com/watch?v=EouitrKS6Cw
🔗 Descargar e Instalar Netbeans 11 en ubuntu 18 04: https://www.youtube.com/watch?v=tWiX3Z5t5kQ
🔗 Netbeans Sublime Theme ?: https://www.youtube.com/watch?v=oAF2Q7mTZZM
🔗 Editar CSS directamenete en Chrome con Netbeans: https://www.youtube.com/watch?v=HlQs0a7R2cY
🔗 Esta lista de reproducción: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTT-qEHGqZH3fkBgXH4GOTF
Codigos en gdrive: https://drive.google.com/file/d/1M6c0VYqrzpq6KwdWkrkw7Aalm8FkdITH/view?usp=sharing
Gracias por apoyar este canal: https://www.patreon.com/programadornovato?fan_landing=true
🔗 Facebook: https://facebook.com/ProgramadorNovatoOficial
🔗 Twitter: https://twitter.com/programadornova
🔗 Linkedin: https://www.linkedin.com/in/programadornovato/
🔗 Instagram: https://www.instagram.com/programadornovato/
Anterior tutorial Siguiente tutorial