So I have an image:
ImageIcon i = new ImageIcon("foo.png");
JLabel j = new JLabel(i);
And I have a JFrame that is 600 X 600.
frame.setSize(600, 600);
frame.setLayout(null);
I want to have the bottom of the image touching the bottom of the frame, so I thought this would work:
j.setBounds(250, 600 - i.getIconHeight(), i.getIconWidth(), i.getIconHeight());
But the image sticks out of the bottom of the frame and not all of it is shown. The bottom of the image is below the frame.
EDIT: I need to use absolute positioning for this application (a game).
EDIT2: Here is the code for reference:
import javax.swing.*;
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
private Test() {
setSize(600, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
ImageIcon i = new ImageIcon("foo.png");
JLabel j = new JLabel(i);
add(j);
j.setBounds(250, 600 - i.getIconHeight(), i.getIconWidth(), i.getIconHeight());
setVisible(true);
}
}
although I would prefer using layout managers for this,just to answer your problem, the parameters of setBounds() are
You got the idea right that you have to adjust to the frame’s size but I think what you need to adjust there is the second parameter which is the y not the height of the Image Icon