I am having trouble displaying this JLabel which contains an image at the specific coordinates (40,80). Can anyone tell me what I am doing wrong here?
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;
public class Window extends JFrame{
//the pictures
JLabel guy = new JLabel(new ImageIcon("tester.gif"));
JPanel panel = new JPanel();
Window(){
super("WindowTitleThing");
setSize(700,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guy.setAlignmentX(40);
guy.setAlignmentY(80);
add(panel);
panel.add(guy);
setVisible(true);
}
}
I don’t see that you set the Layout manager of your JPanel somewhere in the code.
When you work with Swing, every container has a LayoutManager which deals with the placing of components on the corresponding container (see http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html for how to work with layout managers).
If you want to place your component at fixed coordinates like in your case, you should set the absolute layout on your JPanel:
and then you can place components you want to add with the setBounds Method:
Althogh I would use layout managers for bigger UI.