I use a GridBagLayout
and place a JLabel
on it as the picture shows. I managed to set the size of the JLabel
to the desired one via ipadx
and ipady
but I don't seem to work with the position of it. It seems it is always centered on the middle while I'd like to start from the red dot and don't reposition itself as I resize the window. What should I do?
Thanks. The code I use is also:
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 87;
gbc.ipady = 220;
add(jl, gbc);
I improved it... now you can work with insets to position that label around JFrame
, but after some research I would recommend to choose different layout manager.
So your code would look like:
Dimension d = new Dimension(350, 400);
GridBagLayout gbl = new GridBagLayout();
JFrame frame = new JFrame("Heloo");
frame.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.ipadx = 87;
gbc.ipady = 220;
gbc.insets = new Insets(0, 0, 360, 340);// here work with JFrame size!!
Hope this code will help you :)