Hi All,
To add image as background image in JPanel ,JLabel is used for example:-
setLayout(new BorderLayout());
try {
image = ImageIO.read(new File("D:\\New folder (2)\\fish.jpg"));
} catch (IOException ex) {
// handle exception...
System.out.println("error");
}
JLabel picLabel = new JLabel(new ImageIcon(image));
picLabel.setLayout(null);
picLabel.setLocation(0, 0);
picLabel.setSize(750, 100);
picLabel.setVisible(true);
add(picLabel,BorderLayout.NORTH);
In this first i assigned border layout to parent container then add JLabel in BorderLayout.NORTH which means at top of parent controller.
Using same you can assign image to JButton as follow:-
JButton button = new JButton(new ImageIcon(buttonIcon));
button.setBorder(BorderFactory.createEmptyBorder());
button.setContentAreaFilled(false);
button.setBounds(5,72,24,24);
picLabel.add(button);
Now i added JButton with image. SetBound method is used to give location and size to JButton
0 Comment(s)