JLabel positioning (setAlignment not working)

I want to post the image that I posted inside JLabel. Here is my code:

public Main() { setLayout (new FlowLayout()); image = new ImageIcon(getClass().getResource("title.gif")); image1 = new JLabel(image); image1.setAlignmentX(400); image1.setAlignmentY(400); add(image1); } 

Displays the image but the lines

 image1.setAlignmentX(400); image1.setAlignmentY(400); 

Do not do anything. I am new to Java, any help is appreciated.

(I would appreciate an example.)

+4
source share
1 answer

Questions:

  • You are using FlowLayout, which will not respect absolute positioning, even if you do it right.
  • setAlignmentX(...) intended to offer the container alignment of the component along the x axis. As noted above, it takes a floating-point number from 0.0f to 1.0f, with 0f meaning left justification, 0.5f in the center, and 1.0f right. You definitely do not want to use this method, or it is here.
  • Absolute positioning requires a container that uses a null layout, not FlowLayout.
  • Absolute positioning is done using the setBounds(...) or setLocation(...) method.
  • Having said that, I suggest you not try to position things absolutely, unless absolutely necessary, and there is a good chance that you will not do this, that there is a better way.

Please tell us more about your problem, not how you are trying to solve it, and we will probably be able to help you.

+7
source

Source: https://habr.com/ru/post/1416695/


All Articles