问题
I have a JFrame
in which I want to occupy it entirely with a JPanel
and put a background image in the JPanel
.
Code:
public class InicioSesion extends javax.swing.JFrame{
private Image imagenFondo;
private URL fondo;
public InicioSesion(){
initComponents();
try{
fondo = this.getClass().getResource("fondo.jpg");
imagenFondo = ImageIO.read(fondo);
}catch(IOException ex){
ex.printStackTrace();
System.out.print("Image dont load"); //Dont load the message.
}
Container c = getContentPane();
c.add(PanelFondo);
}
public JPanel panelFondo = new JPanel(){
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imagenFondo, 0, 0, getWidth(), getHeight(), this);
}
};
Why doesn't the image load? Any solution to my code?
回答1:
Your problem is here:
initComponents();
You likely add all components to the GUI in this method, quite possibly using GroupLayout or other user-unfriendly layout manager, and then add the panelFondo
JPanel after all components have been added.
If you want a GUI to show a background image, the components need to be added to the image-drawing JPanel, and if any JPanels are added on top of the image drawer, they need to be transparent (setOpaque(false)`) so that the background image shows through.
I'm guessing that you're using a GUI builder to create your GUI layouts and to assist you in adding components to the GUI. Myself, I avoid using them and much prefer creating my GUI's by hand using layout managers (never null layouts). If you absolutely must use a GUI builder, then have the builder create a JPanel for you, not a JFrame, and then override this JPanel's paintComponent, drawing the image within it. Otherwise you may be better off learning the Swing layout managers and creating your GUI's by hand like I do.
Your window appears to be a sign-in window, and if so, if this were my program, I wouldn't even use a JFrame but rather a modal JDialog to display this since it would be much easier to control program flow in this way.
Proof of concept program using GridBagLayout and way too many "magic numbers":
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class LoginPanel extends JPanel {
public static final String TITLE = "INICIO DE SESIÓN";
public static final String IMG_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/thumb/6/69/MarsSunset.jpg/779px-MarsSunset.jpg";
private JTextField usuarioField = new JTextField(20);
private JPasswordField passwordField = new JPasswordField(20);
private BufferedImage backgroundImg = null;
public LoginPanel(BufferedImage img) {
this.backgroundImg = img;
JCheckBox showPasswordChkBx = new JCheckBox("Show Password");
showPasswordChkBx.setOpaque(false);
showPasswordChkBx.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
passwordField.setEchoChar((char) 0);
} else {
passwordField.setEchoChar('*');
}
}
});
JButton accederBtn = new JButton("Acceder");
accederBtn.addActionListener(e -> {
Window win = SwingUtilities.getWindowAncestor(LoginPanel.this);
win.dispose();
});
setForeground(Color.BLACK);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
int row = 0;
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
int ins = 12;
gbc.insets = new Insets(ins, ins, ins, ins);
gbc.anchor = GridBagConstraints.CENTER;
JLabel titleLabel = new JLabel(TITLE);
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 24f));
add(titleLabel, gbc);
row++;
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.LINE_START;
add(new JLabel("Usuario:"), gbc);
gbc.gridx = 1;
add(usuarioField, gbc);
row++;
gbc.gridx = 0;
gbc.gridy = row;
gbc.insets = new Insets(ins, ins, 0, ins);
add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
add(passwordField, gbc);
row++;
gbc.gridx = 0;
gbc.gridy = row;
gbc.insets = new Insets(0, ins, ins, ins);
add(new JLabel(""), gbc);
gbc.gridx = 1;
add(showPasswordChkBx, gbc);
row++;
gbc.gridx = 0;
gbc.gridy = row;
gbc.insets = new Insets(ins, ins, ins, ins);
add(new JLabel(""), gbc);
gbc.gridx = 1;
add(accederBtn, gbc);
}
public String getUsuario() {
return usuarioField.getText();
}
public char[] getPassword() {
return passwordField.getPassword();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), this);
}
}
@Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
int width = superSize.width;
int height = superSize.height;
if (backgroundImg != null) {
width = Math.max(width, backgroundImg.getWidth());
height = Math.max(height, backgroundImg.getHeight());
}
return new Dimension(width, height);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
BufferedImage img = null;
try {
URL imgUrl = new URL(IMG_PATH);
img = ImageIO.read(imgUrl);
} catch (IOException e) {
e.printStackTrace();
}
LoginPanel mainPanel = new LoginPanel(img);
JDialog dialog = new JDialog((JFrame) null, LoginPanel.TITLE, ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.add(mainPanel);
dialog.pack();
dialog.setLocationByPlatform(true);
dialog.setVisible(true);
System.out.println("User Name: " + mainPanel.getUsuario());
System.out.println("Password: " + new String(mainPanel.getPassword()));
}
}
来源:https://stackoverflow.com/questions/61851372/error-loading-background-image-into-jpanel-in-a-jframe