Results 1 to 5 of 5

Thread: JFrame

  1. #1
    m_patten2 is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default JFrame

    Hello everyone,

    First of all I just want to let you all know that this is not homework. It was a piece of code that was given to us in class as an example of how a loop could create labels, text fields, and buttons in a JFrame.

    I've taken it upon myself to add a BorderLayout to position those items neatly. If you complile the code you can see I'm not getting the results that I am hoping for.

    Java Code:
    import java.util.Scanner;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    
    public class JFrameExercise extends JFrame
    {
    	private static double _relWidth, _relHeight;
    	
    	public JFrameExercise()
    	{
    		// Set-up the basic JFrame
    		this.setTitle("Day 2 Coding Exercise");
    		Dimension screenSize = this.getToolkit().getScreenSize();
    		this.setSize((int)(_relWidth * screenSize.width/100), 
    					 (int)(_relHeight * screenSize.height/100));
    		this.setLocationRelativeTo(null);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		// Create a JPanel 
    		JPanel pnl = new JPanel(new GridLayout(2,10,25,10));
    		pnl.setLayout(new BorderLayout());
    		
    		// Add 10 sets of labels and text fields to the JPanel
    		JLabel [] labels = new JLabel[10];
    		JTextField [] textflds = new JTextField[10];
    		for(int i = 0; i < labels.length; i++)
    		{
    			labels[i] = new JLabel("Label " + (i+1));
    			pnl.add(labels[i], BorderLayout.WEST);
    			textflds[i] = new JTextField("Text Field " + (i+1));
    			pnl.add(textflds[i], BorderLayout.EAST);
    		}
    		
    		// Add 5 buttons to the JPanel
    		JButton [] buttons = new JButton[5];
    		for(int i = 0; i < buttons.length; i++)
    		{
    			buttons[i] = new JButton("Button " + (i+1));
    			pnl.add(buttons[i], BorderLayout.SOUTH);
    			
    		}
    
    		// Add the panel to the frame
    		this.add(pnl);
    		
    		// Display the frame
    		this.setVisible(true);
    	}
    
    	public static void main(String[] args)
    	{
    		// Get the relative width and height of the JFrame
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter the relative width of the JFrame (<= 100%): ");
    		_relWidth = input.nextDouble();
    		System.out.print("Enter the relative height of the JFrame (<= 100%): ");
    		_relHeight = input.nextDouble();
    
    		// Create the JFrame
    		new JFrameExercise();
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    I'm not getting the results that I am hoping for.
    Before doing any work on this, could you explain what you were hoping for? And describe what the code actually does and why the two are different?

  3. #3
    m_patten2 is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Before doing any work on this, could you explain what you were hoping for? And describe what the code actually does and why the two are different?
    Ok heres whats happening:

    When the program was initially run it would automatically create 10 labels, 10 text fields, and 5 buttons. But it would put them all into a row. When I modified the code to add the BorderLayout.east/west/north/south it only displays the last label, text field, and button.

    What I'm trying to achieve is to have all 10 labels, 10 text fields, and 5 buttons displayed as followed:

    10 Labels EAST
    10 Text Fields WEST
    5 Buttons South

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    Thanks. That seems clear enough.
    You need to redesign your use of panels and layout managers.
    You should use one panel with its layout manager for each border position. The components in the west and east are to be vertical and in the south horizontal. Try a GridLayout for west and east and a flowlayout for the south.
    For the main panel use border layout. Add each of the above 3 panels to the main panel at its border position.
    Last edited by Norm; 09-14-2010 at 05:52 PM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. JFrame from a JFrame in project
    By anah in forum AWT / Swing
    Replies: 2
    Last Post: 04-19-2010, 06:20 PM
  3. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 PM

Tags for this Thread

Posting Permissions

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