So I already made a panel that contains multiple labels, text fields, and buttons. And within the panel I placed another JLabel
and went to properties -> icon and uploaded an image, however the image(which I'm trying to use as the background) does not scale to the panel.
What can I do to make the image fit to scale the panel dimension?
For example, this is a New Project JFrame
named Sample
and I have added a JPanel
with variable name pnMain
and a JLabel
with variable name lblBackground
. With the label I went to Properties -> Icon then uploaded an image.
package tempo;
public class Sample extends javax.swing.JFrame {
/**
* Creates new form Sample
*/
public Sample() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
pnMain = new javax.swing.JPanel();
lblBackground = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pnMain.setLayout(null);
lblBackground.setIcon(new javax.swing.ImageIcon("C:\\Users\\Anderson\\Desktop\\Monkee-Boy Blurred Backgrounds\\bkg-3.jpg")); // NOI18N
pnMain.add(lblBackground);
lblBackground.setBounds(0, 0, 400, 280);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pnMain, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pnMain, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sample().setVisible(true);
}
});
}
// Variables declaration
private javax.swing.JLabel lblBackground;
private javax.swing.JPanel pnMain;
// End of variables declaration
}