Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JLabel and set Location doesn't work correctly

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JLabel and set Location doesn't work correctly

    Hello everyone
    i'm making a programe that has buttons, labels, and text fields and i'm having a problem with JLabel where when i try to use SetLocation it works on the first three Label but its doesn't work on the fourth one !!
    so for example i have 3 labels and when i set their location it works correctly but when i set the location for the fourth one it doesn't work.
    the one that doesn't work is named AgeLabel in my code.

    also sometimes when i run the program one of the buttons get resize by it self and full the whole screen. so for example if i have the button size set to (50,50) sometimes when i run the program it full the whole window and the seize change to for example (800,600). Why ?!!

    here is the full class code.

    package finalproject;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Interface extends JFrame implements ActionListener{
     
        private final int FRAME_WIDTH = 1024;
        private final int FRAME_HIGHT = 768;
        private final int FRAME_X_ORIGIN = 150;
        private final int FRAME_Y_ORIGIN = 200;
        private String numberStr;
     
        private JTextField FirstNameFiled   = new JTextField();
        private JTextField LastNameFiled    = new JTextField();
        private JTextField AddressFiled     = new JTextField();
        private JTextField AgeFiled         = new JTextField();
     
        private Container ContentPane = getContentPane();
     
        public Interface() {
     
            SetWindow();
            Buttons();
            TextField();
            Lable();
        }
     
        private void SetWindow() {
     
            this.setTitle("Final Project");
            this.setSize(FRAME_WIDTH, FRAME_HIGHT);
            this.setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setVisible(true);
            this.setResizable(false);
        }
     
        private void Buttons() {
            JButton ExitButton, SaveButton, LoadButton;
     
            ExitButton = new JButton("Exit");
            ExitButton.setActionCommand("Exit");
            ExitButton.setLocation(10, 140);
            ExitButton.setSize(95, 25);
            ExitButton.addActionListener(this);
            ContentPane.add(ExitButton);
     
            SaveButton = new JButton("Save File");
            SaveButton.setActionCommand("Save_File");
            SaveButton.setLocation(110, 140);
            SaveButton.setSize(95, 25);
            SaveButton.addActionListener(this);
            ContentPane.add(SaveButton);
     
            LoadButton = new JButton("Load File");
            LoadButton.setActionCommand("Load_File");
            LoadButton.setLocation(210, 140);
            LoadButton.setSize(95, 25);
            LoadButton.addActionListener(this);
            ContentPane.add(LoadButton);  
        }
     
        private void Lable() {
     
            JLabel FirstNameLabel = new JLabel(); 
            FirstNameLabel.setText("First Name: ");
            FirstNameLabel.setSize(100,20);
            FirstNameLabel.setLocation(10, 10);
            ContentPane.add(FirstNameLabel); 
     
            JLabel LastNameLabel = new JLabel(); 
            LastNameLabel.setText("Last Name: ");
            LastNameLabel.setSize(100,20);
            LastNameLabel.setLocation(240, 10);
            ContentPane.add(LastNameLabel); 
     
            JLabel AddressLabel = new JLabel(); 
            AddressLabel.setText("Address: ");
            AddressLabel.setSize(100,20);
            AddressLabel.setLocation(10, 50);
            ContentPane.add(AddressLabel); 
     
            JLabel AgeLabel = new JLabel(); 
            AgeLabel.setText("Age: ");
            AgeLabel.setSize(100, 20);
            AgeLabel.setLocation(240, 50);
            ContentPane.add(AgeLabel);
        }
     
        private void TextField() {
     
            FirstNameFiled.setActionCommand("FirstNameFiled");
            FirstNameFiled.setText("First Name");
            FirstNameFiled.setSize(100, 25);
            FirstNameFiled.setLocation(85, 9);
            FirstNameFiled.addActionListener(this);
            ContentPane.add(FirstNameFiled);  
     
            LastNameFiled.setActionCommand("LastNameFiled");
            LastNameFiled.setText("Last Name");
            LastNameFiled.setSize(100, 25);
            LastNameFiled.setLocation(315, 9);
            LastNameFiled.addActionListener(this);
            ContentPane.add(LastNameFiled); 
     
            AddressFiled.setActionCommand("AddressFiled");
            AddressFiled.setText("Address");
            AddressFiled.setSize(100, 25);
            AddressFiled.setLocation(85, 48);
            AddressFiled.addActionListener(this);
            ContentPane.add(AddressFiled); 
     
            AgeFiled.setActionCommand("AgeFiled");
            AgeFiled.setText("Age");
            AgeFiled.setSize(100, 25);
            AgeFiled.setLocation(315, 48);
            AgeFiled.addActionListener(this);
            ContentPane.add(AgeFiled); 
        }
     
        public void actionPerformed(ActionEvent Action) {
            if(Action.getActionCommand().contains("Exit"))
                System.exit(0);
     
            if(Action.getActionCommand().equals("Save_File")) {
                System.out.print("Saving File");
            }
     
            if(Action.getActionCommand().equals("Load_File")) {
                System.out.print("Loading File");
            }
     
            if (Action.getActionCommand().contains("FirstNameFiled")) {
                numberStr = FirstNameFiled.getText();
            }
     
            if (Action.getActionCommand().contains("LastNameFiled")) {
                numberStr = LastNameFiled.getText();
            }
     
            if (Action.getActionCommand().contains("AddressFiled")) {
                numberStr = AddressFiled.getText();
            }
     
            if (Action.getActionCommand().contains("AgeFiled")) {
                numberStr = AgeFiled.getText();
            }
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JLabel and set Location doesn't work correctly

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Fantasy (November 15th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JLabel and set Location doesn't work correctly

    LOL sorry and thank you
    i'm a newbie

  5. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JLabel and set Location doesn't work correctly

    ok i just added this line of code and everything is now working correctly
    ContentPane.setLayout(null);
    if you guys don't mind can you just take a quick look at my code and tell me if i'm doing some bad programing practice and can anyone give me some tips on my code.

    thanks alot.

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JLabel and set Location doesn't work correctly

    Using a null layout is a bad programming practice.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Fantasy (November 15th, 2011)

  8. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JLabel and set Location doesn't work correctly

    may i ask why ?

  9. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JLabel and set Location doesn't work correctly

    What happens when you resize your container? What happens if you want to add or remove a component? What happens if you want to switch the orientation of your container? The layout managers are there for a reason. Use 'em.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 06:05 AM
  2. Returning location instead of value?
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2011, 04:39 PM
  3. Open location from JList
    By hexwind in forum AWT / Java Swing
    Replies: 13
    Last Post: August 21st, 2011, 08:46 PM
  4. [SOLVED] How to specify FileOutputStream location?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 29th, 2011, 06:36 PM
  5. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 04:20 PM