Page 1 of 2 12 LastLast
Results 1 to 20 of 39
Like Tree1Likes

Thread: Setting x and y for String

  1. #1
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Question Setting x and y for String

    Title says it all. I have been learning java and studying Swing but now i have a problem, i want to set x and y coordinates for this
    Java Code:
    		JLabel jlbWorld = new JLabel("You have spawned");
    But i have no idea how (I have searched internet for answers but all of them have JPanel)
    Last edited by NitroDev; 02-15-2014 at 04:03 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Setting x and y for String

    Are you talking about the location of the component (the JLabel) in the container in the GUI?
    See the API doc for the Component class. It has methods for setting its location.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Quote Originally Posted by Norm View Post
    Are you talking about the location of the component (the JLabel) in the container in the GUI?
    See the API doc for the Component class. It has methods for setting its location.
    I'm talking about the "You have spawned!" string/sentence

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Setting x and y for String

    Strings by themselves do not have locations. I assumed you were talking about some component that is displaying a String.
    Components can be positioned.

    Are you asking about how to tell a component where to position a String when it displays it within its bounds? Look at the JLabel class's set methods to see which ones would be useful.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Are you asking about how to tell a component where to position a String when it displays it within its bounds?
    Excatly that now i'm just looking around to see where the set methods are for JLabel.
    I have found them but there's a bug i don't understand, here's the code so far:
    Java Code:
    import javax.swing.*;
    
    public class gameWorld extends JFrame {
    
    	public static void main(String[] args) {
    		new gameWorld();
    		
    	}
    	gameWorld() {
    		JLabel jlbWorld = new JLabel("You have spawned!");
    		jlbWorld.setHorizontalAlignment(JLabel.NORTH);
    		jlbWorld.setVerticalAlignment(JLabel.CENTER);
    		add(jlbWorld);
    		this.setSize(700, 700);
    		// pack();
    		setVisible(true);
    		
    	}
    
    }
    And here is the result for that:

    Exception in thread "main" java.lang.IllegalArgumentException: horizontalAlignment
    at javax.swing.JLabel.checkHorizontalKey(Unknown Source)
    at javax.swing.JLabel.setHorizontalAlignment(Unknown Source)
    at gameWorld.<init>(gameWorld.java:11)
    at gameWorld.main(gameWorld.java:6)

    So what did i do wrong
    Last edited by NitroDev; 02-15-2014 at 04:36 PM.

  6. #6
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Setting x and y for String

    I would just use a layout to do this. You could use something like FlowLayout or GridBagLayout, but that is kind of confusing until you figure it out. Here's a link about layout managers: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  7. #7
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Thanks pj i shall look into that

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Setting x and y for String

    Because NORTH is not a valid option. Check out the JLabel API for details on what is.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Quote Originally Posted by jim829 View Post
    Because NORTH is not a valid option. Check out the JLabel API for details on what is.

    Regards,
    Jim
    But when i scroll down on the popup eclipse shows me it says NORTH

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Setting x and y for String

    I'd use what it says in the API doc.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Setting x and y for String

    Eclipse is simply showing you all the possible int constants that are supported by JLabel. Eclipse has no idea which one is appropriate for the method. You must refer to the JLabel API for that information.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Image not showing

    Yea first of all thank you for answering my last problem (it was pretty simple) but now i got a new one; i can't seem to make the image show up in this code:

    Java Code:
    import javax.swing.*;
    
    public class gameWorld extends JFrame {
    
    	public static void main(String[] args) {
    		new gameWorld();
    		
    	}
    	gameWorld() {
    		Icon map = new ImageIcon("MyCoreWorld.jpg");
    		JLabel jlbConsole = new JLabel("You have spawned!");
    		jlbConsole.setHorizontalAlignment(JLabel.CENTER);
    		jlbConsole.setVerticalAlignment(JLabel.TOP);
    		JLabel jlbWorld = new JLabel(map);
    		jlbWorld.setHorizontalAlignment(JLabel.CENTER);
    		jlbWorld.setVerticalAlignment(JLabel.CENTER);
    		add(jlbConsole);
    		add(jlbWorld);
    		this.setSize(700, 700);
    		// pack();
    		setVisible(true);
    		
    	}
    
    }

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Setting x and y for String

    What layout manager is controlling where the components are to be displayed after they are added to the JFrame?
    Where will that layout manager put the components that are added?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    i'm not using any layout managers at the moment why?

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Setting x and y for String

    Many containers have a default layout manager. If you don't set the layout manager, the default will be used.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Quote Originally Posted by Norm View Post
    Many containers have a default layout manager. If you don't set the layout manager, the default will be used.
    Can you give me a simple code that would work with my code?
    Last edited by NitroDev; 02-15-2014 at 08:42 PM.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Setting x and y for String

    What have you found out about using a layout manager? Take a look at the tutorial:
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.*;
    
    public class gameWorld extends JFrame {
    
    	public static void main(String[] args) {
    		new gameWorld();
    		
    	}
    	gameWorld() {
    		setLayout(new BorderLayout());
    		Icon map = new ImageIcon("MyCoreWorld.jpg");
    		JLabel jlbConsole = new JLabel("You have spawned!");
    		jlbConsole.setHorizontalAlignment(JLabel.CENTER);
    		jlbConsole.setVerticalAlignment(JLabel.TOP);
    		
    		JLabel jlbWorld = new JLabel(map);
    		jlbWorld.setHorizontalAlignment(JLabel.CENTER);
    		jlbWorld.setVerticalAlignment(JLabel.CENTER);
    		add(jlbConsole);
    		add(jlbWorld);
    		this.setSize(700, 700);
    		// pack();
    		setVisible(true);
    		
    	}
    
    }
    There but the code doesn't make a difference, it still makes a blank java window

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Setting x and y for String

    Where does the layout manager you have chosen put the components that are added to the JFrame?
    There are no instructions included in the add() method calls to tell the layout manager where to put the components.

    Try using the FlowLayout. It'll work without any instructions on where to put the components that are added.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Okay now if only i could know how to set up the FlowLayout...

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 06-29-2011, 05:46 PM
  2. Help, Setting Key Code
    By quafflepunch in forum New To Java
    Replies: 2
    Last Post: 01-05-2011, 01:37 AM
  3. Setting up a String Array
    By AJArmstron@aol.com in forum New To Java
    Replies: 12
    Last Post: 04-16-2010, 12:42 PM
  4. Setting the DSN
    By tim in forum JDBC
    Replies: 1
    Last Post: 02-14-2008, 09:55 PM
  5. Help with setting up please
    By BlitzA in forum New To Java
    Replies: 6
    Last Post: 12-29-2007, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •