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 3 of 3

Thread: Aranging objects on a Gui

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Aranging objects on a Gui

    Basically im trying to get stuff aligned on a JPanel. I want everything to be on its own devidual line
    problem is idk. I was messing around with setBounds but that did seem to work so how would i go about doing this?

    heres my class:
    package main;
     
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Gui implements ActionListener {
     
    	private JFrame f = new JFrame("Simple Games");
     
    	private JPanel jp = new JPanel();
     
    	private JLabel title = new JLabel("Guess a Number between 1 and 10!");
    	private JTextField type = new JTextField("Type your number here!");
    	private JButton submit = new JButton("Submit");
     
    	private JMenuBar mb = new JMenuBar();
    	private JMenu muFile = new JMenu("File");
     
    	public Gui() {
     
    		f.setJMenuBar(mb);
    		f.add(jp);
     
    		jp.add(title);
    		jp.add(type);
    		jp.add(submit);
     
    		mb.add(muFile);
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void launchFrame() {
    		Container pane = f.getContentPane();
    		pane.setLayout(new GridLayout (0,1));
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		f.setResizable(false);
    		f.setSize(420, 200);
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Gui g = new Gui();
    		g.launchFrame();
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Aranging objects on a Gui

    Use a layout manager. See
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    And also note that layouts can be nested, for example one panel can have a certain layout while the panels that it contains another.

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

    frozen java (October 20th, 2011)

  4. #3
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Aranging objects on a Gui

    thanks it got it!

Similar Threads

  1. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 02:41 AM
  2. Vectors and objects
    By Matty Alan in forum Java Theory & Questions
    Replies: 3
    Last Post: May 10th, 2011, 11:44 AM
  3. New to Objects...
    By Java Neil in forum What's Wrong With My Code?
    Replies: 17
    Last Post: March 24th, 2011, 07:00 AM
  4. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 02:19 PM
  5. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 11:50 AM