1

I have just started with making frames in java and wrote the following code :

 import javax.swing.*;

public class HelloWorldFrame extends JFrame

{       
  public static void main(String[] args)
{   
  new HelloWorldFrame(); 
}
HelloWorldFrame()
 {
    JLabel jlb=new JLabel("HelloWorld");  
    add(jlb);
    this.setSize(100,100);
    setVisible(true);
 }
}   

Can anyone help explaining how can I change the position of label in the above code

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
WantTobeAbstract
  • 133
  • 1
  • 1
  • 7
  • 1
    See [Lesson: Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/). It's all explained there. Hint: the default layout manager of JFrame's content pane is BorderLayout. – dic19 Jan 22 '15 at 14:49
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with **layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556).** – Andrew Thompson Jan 22 '15 at 15:01

2 Answers2

2

It is fairly easy to change the position of your JLabel instance by using two methods which are really handing. Look below for the code to use :

HelloWorldFrame()
 {
    JLabel jlb=new JLabel("HelloWorld");  
    jlb.setHorizontalAlignment(50); // set the horizontal alignement on the x axis !
    jlb.setVerticalAlignment(50); // set the verticalalignement on the y axis !
    add(jlb);
    this.setSize(100,100);
    setVisible(true);
 }

You can learn more about JLabel and how to manipulate them here : http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html

//EDITS

Base on the comment that I've given earlier, the alignement that was given could not work. So I made the necessary changes to make them work !

 HelloWorldFrame()
     {
        JLabel jlb=new JLabel("HelloWorld");  
        jlb.setHorizontalAlignment(SwingConstants.CENTER); // set the horizontal alignement on the x axis !
        jlb.setVerticalAlignment(SwingConstants.CENTER); // set the verticalalignement on the y axis !
        add(jlb);
        this.setSize(100,100);
        setVisible(true);
     }
Kevin Avignon
  • 2,853
  • 3
  • 19
  • 40
  • @WantTobeAbstract I'm glad it did ! Continue to rock on on that java code ! – Kevin Avignon Jan 22 '15 at 15:14
  • but why am I getting the following warning during compilation now: Exception in thread "main" java.lang.IllegalArgumentException: horizontalAlignme nt at javax.swing.JLabel.checkHorizontalKey(JLabel.java:607) at javax.swing.JLabel.setHorizontalAlignment(JLabel.java:765) at HelloWorldFrame.(HelloWorldFrame.java:13) at HelloWorldFrame.main(HelloWorldFrame.java:7) – WantTobeAbstract Jan 22 '15 at 15:28
  • 1
    This was an error of mine to provide you sample code like that. That's my fault. Just look the code on Oracle and as you can see "alignment - One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM." You have a good example on how to use it here : http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node37.html – Kevin Avignon Jan 22 '15 at 15:34
  • thanks finally got it displayed ,but I can't understand what is a swingconstant. – WantTobeAbstract Jan 22 '15 at 16:03
  • You can learn more about the SwingConstant interface here : http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingConstants.html You access it while importing the java swing implementation – Kevin Avignon Jan 22 '15 at 16:06
2

Usually (read recommended) what you are after is achieved through the use of Layout Managers. If that does not work for you, you would need to use setLocation(int x, int y) and combine it with this.setLayoutManager(null) (or something like that). This will remove the default layout manager and give you complete control on where/how are your object placed.

EDIT: As outlined by @AndrewThompson, please go for the first approach rather than the second unless you really, really have to.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • I did not think about that. That's better than using the alignment on the axis which means that the position would have been set already ! Nice answer – Kevin Avignon Jan 22 '15 at 14:54
  • 1
    This horrendous advice again? Please stop giving advice on making GUIs until your advice solves more problems *than it causes..* – Andrew Thompson Jan 22 '15 at 15:00
  • 1
    @AndrewThompson: I'm not a big fan of the second approach myself. I did inform the OP which way is the recommended way. The OP did not specify any context I think both of them are valid answers, but as you say, the second approach will cause more trouble than it solves. – npinti Jan 22 '15 at 15:04