Ayuda con fondo en un JFrame
Hola que tal programadores tengo una duda de un error que no e podido solucionar tengo la siguiente clase JPanel para agregar una imagen que se ajuste al tamaño del fondo.
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.Graphics;
public class panelImagen extends JPanel{
public void paintComponent(Graphics graphics){
Dimension tam = getSize();
ImageIcon imagenFondo = new ImageIcon(new ImageIcon(getClass().getResource("/Imagenes/login/fondoLogin.png")).getImage());
graphics.drawImage(imagenFondo.getImage(), 0, 0, tam.width, tam.height, null);
}
}
creo el objeto en el JFrame para el login de la app pero no agrega la imagen asta que quito el Layout del JFrame asi lo tengo
import clases.panelImagen;
import javax.swing.*;
public class entrar extends JFrame {
JButton botonEntrar = new JButton();
JButton botonCancelar = new JButton();
JLabel labelNombre = new JLabel("Nombre: ");
JLabel labelContra = new JLabel("Contraseña: ");
JLabel labelFondo = new JLabel();
JTextField textoNombre = new JTextField();
JPasswordField textContra = new JPasswordField();
public entrar(){
super("Inicio de sesión");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(entrar.EXIT_ON_CLOSE);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setSize(300, 220);
this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/icon.png")).getImage());
add(new panelImagen());
paintComponents(getGraphics());
}
}
se bien que es por que el layout esta en null pero lo necesito así para asignar las coordenadas por mi parte alguna forma de agregar el fondo y dejar el layout null ?
De ante mano gracias por el tiempo que se tomaron al entrar.
- Inicie sesión o regístrese para enviar comentarios
Re: layout null
Utilizar un
java.awt.LayoutManager
no debería tener ningún efecto «negativo». Por ejemplo:Image img = ImageIO.read(new URL("http://bit.ly/1BB6qLJ"));
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, getParent().getWidth(), getParent().getHeight(), this);
}
};
panel.setOpaque(false);
panel.setLayout(new FlowLayout());
panel.add(new JLabel("Username"));
panel.add(new JTextField(10));
panel.add(new JLabel("Password"));
panel.add(new JPasswordField(10));
panel.add(new JButton("Sing In"));
JFrame frame = new JFrame("Test");
frame.add(panel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout());
frame.setSize(250, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Si requieres posicionamiento absoluto, he aquí un pequeño ejemplo:
Image img = ImageIO.read(new URL("http://bit.ly/1BB6qLJ"));
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, getParent().getWidth(), getParent().getHeight(), this);
}
};
panel.setOpaque(false);
panel.setLayout(null);
JLabel usrLabel = new JLabel("Username");
usrLabel.setBounds(10, 5, 100, 20);
panel.add(usrLabel);
JTextField usrTextField = new JTextField();
usrTextField.setBounds(10, 30, 100, 20);
panel.add(usrTextField);
JLabel pwdLabel = new JLabel("Password");
pwdLabel.setBounds(10, 55, 100, 20);
panel.add(pwdLabel);
JPasswordField pwdPasswordField = new JPasswordField(10);
pwdPasswordField.setBounds(10, 80, 100, 20);
panel.add(pwdPasswordField);
JButton okButton = new JButton("Sing In");
okButton.setBounds(120, 5, 100, 100);
panel.add(okButton);
JFrame frame = new JFrame("Test");
frame.add(panel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout());
frame.setSize(250, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
¡Por si sirve de algo!
Fe de Erratas
Debe decir «Sign In» en lugar de «Sing In». ¡Pequeña alusión de alegría ahí!
~~~
2-factor
Es que es autenticación de dos factores... tienes que cantar una canción y depende qué tan desafinado seas, se puede comprobar si realmente eres tú.
Re: cantar una canción
Richie Rich - Opening the Vault - YouTube
Agradecer
Hola que tal jpaul gracias me sirvió mucho tu explicación te dejo un gyazo con la captura de como quedo gracias por la ayuda.
http://gyazo.com/6876e850e3076b44ef474032ee2e9469