I am trying to set a background image for this window and I have tried multiple ways but I am compiling/saving it, it's giving an error as shown below:
Here is my code:
public class Admin_hs extends JFrame {
JButton bking_btn= new JButton("Bookings");
JButton fd_btn= new JButton("Financial Data");
JButton ctm_btn= new JButton("Customers");
JButton room_btn= new JButton("Rooms");
JButton adc_btn= new JButton("Additional Costs");
JButton endb_btn= new JButton("Ending Bookings");
//Images
JLabel bking_img= new JLabel();
JLabel fd_img= new JLabel();
JLabel ctm_img= new JLabel();
JLabel room_img= new JLabel();
JLabel adc_img= new JLabel();
JLabel endb_img= new JLabel();
JLabel logout= new JLabel();
JLabel copyrightL = new JLabel("Hotel Management System \u00a9 2016");
private BufferedImage image;
///Panels
JPanel pnl1= new JPanel();//panel for buttons and images
JPanel pnl2= new JPanel(new GridBagLayout());//panel for copyright label
JPanel pnl3= new JPanel(); //panel for logout button
///Constructors
public Admin_hs(){
this.setTitle("Welcome Admin!");
this.setLayout(new GridBagLayout());
///Setting a layout
pnl1.setLayout(new GridBagLayout());
logout.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/logout.jpg"));
pnl3.setLayout(new GridBagLayout());
//logout = new JLabel( "Logout" );
pnl3.add(logout, new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(6, 6, 6, 6), 0, 0));
//background image
try {
image = ImageIO.read(new File("C:/Users/Diksha/Desktop/simple-blue.jpg"));
this.setContentPane(new JLabel(new ImageIcon(image)));
} catch (IOException e) {
e.printStackTrace();
}
///First Column of Grid
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth= GridBagConstraints.REMAINDER;
gbc.fill= gbc.HORIZONTAL;
GridBagConstraints gc= new GridBagConstraints();
gc.insets = new Insets(6, 6, 6, 6);
gc.anchor = GridBagConstraints.EAST;
gc.weightx = 0.5;
gc.weighty = 0.5;
gc.gridx = 0;
gc.gridy = 0;
bking_btn.setPreferredSize(new Dimension(130,30));
bking_btn.setMinimumSize(new Dimension(130,30));
pnl1.add(bking_btn, gc);
gc.gridx = 0;
gc.gridy = 1;
fd_btn.setPreferredSize(new Dimension(130,30));
fd_btn.setMinimumSize(new Dimension(130,30));
pnl1.add(fd_btn, gc);
gc.gridx = 0;
gc.gridy = 2;
ctm_btn.setPreferredSize(new Dimension(130,30));
ctm_btn.setMinimumSize(new Dimension(130,30));
pnl1.add(ctm_btn, gc);
gc.gridx = 0;
gc.gridy = 3;
room_btn.setPreferredSize(new Dimension(130,30));
room_btn.setMinimumSize(new Dimension(130,30));
pnl1.add(room_btn, gc);
gc.gridx = 0;
gc.gridy = 4;
adc_btn.setPreferredSize(new Dimension(130,30));
adc_btn.setMinimumSize(new Dimension(130,30));
pnl1.add(adc_btn, gc);
gc.gridx = 0;
gc.gridy = 5;
endb_btn.setPreferredSize(new Dimension(130,30));
endb_btn.setMinimumSize(new Dimension(130,30));
pnl1.add(endb_btn, gc);
gc.gridx = 0;
gc.gridy = 8;
pnl1.add(copyrightL, gc);
/////second column of grid
gc.anchor = GridBagConstraints.WEST;
gc.gridx = 1;
gc.gridy = 0;
bking_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/ReserveNowIcon.png"));
pnl1.add(bking_img, gc);
gc.gridx = 1;
gc.gridy = 1;
fd_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/booking.jpg"));
pnl1.add(fd_img, gc);
gc.gridx = 1;
gc.gridy = 2;
ctm_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/guest.jpg"));
pnl1.add(ctm_img, gc);
gc.gridx = 1;
gc.gridy = 3;
room_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/room.jpg"));
pnl1.add(room_img, gc);
gc.gridx = 1;
gc.gridy = 4;
adc_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/fd.jpg"));
pnl1.add(adc_img, gc);
gc.gridx = 1;
gc.gridy = 5;
endb_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/endb.png"));
pnl1.add(endb_img, gc);
pnl1.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
pnl2.add(copyrightL, gbc);
gbc.gridwidth = 1;
gbc.weightx = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
add(pnl3, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(pnl1, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
add(pnl2, gbc);
this.setSize(image.getWidth(),image.getHeight());
this.setLayout(new FlowLayout());
this.setResizable(true);
this.setLocationRelativeTo(null);
}
}
Main
public class Admin_main {
public static void main(String[] args) {
Admin_hs adm= new Admin_hs();
adm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
adm.pack();
adm.setVisible(true);
adm.setSize(800,701);
}
}
I have added images on the window using setIcon
but I didn't get this problem upon compilation. Even when I am commenting out all the lines added for the background image, I am getting the same error. Only when I remove it that I am able run it.
Here is a pic to get an idea of how it looks:
EDIT When I clicked on "Save as UTF", it's throwing an exception:
I would like to know any alternative ways of how I can set a background image as I have tried overriding the paint method also but it didn't work. Thanks.