I'm trying to build a simple GUI interface. I have added a background image to the JPanel using the paintComponent method.
The problem is when the output is built it shows only a small window as follows:
I have to resize the output window to show the full image. How can I make the image fit the window?
Here is my new source code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test extends JFrame {
public test(){
super("Staff Management");
this.setContentPane(new staff());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.pack();
}
public class staff extends JPanel{
private ImageIcon i;
public staff() {
i = new ImageIcon("D:\\staff-directory.jpg");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(i.getIconWidth(),i.getIconHeight());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
i.paintIcon(this,g,0,0);
}
}
}