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

Thread: GUI Layout - Does anyone know how to set a layout?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI Layout - Does anyone know how to set a layout?

    I have this blackjack layout and every panel and textfield already on the box except I don't know how to properly alligned them. I've been reading about layout manager but I get confused. I hope anybody can help me.

    package BlackJack;
     
    /*Program Name: BlackJackGUI.java
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class BlackJackGUI extends JPanel
    {
    	//declare all variables
    	private ImageIcon image;
    	private JLabel Photo;
    	private JLabel betsLabel;
    	private JTextField betsField;
    	private JLabel trackerLabel;
    	private JLabel winLabel;
    	private JTextField winField;
    	private JLabel loseLabel;
    	private JTextField loseField;
    	private JLabel winloseLabel;
    	private JTextField winloseField;
    	private JLabel playerLabel;
    	private JTextField playerField;
    	private JTextArea playerArea;
    	private JLabel dealerLabel;
    	private JTextField dealerField;
    	private JTextArea dealerArea;
    	private JButton dealButton;
    	private JButton hitButton;
    	private JButton stayButton;
    	private JButton resetButton;
    	private JButton highButton;
    	private JButton lowButton;
    	//private JFrame frame;
     
    	//contructs the screen
    	public BlackJackGUI()
    	{
    		//create layout manager
    		setBackground(new Color(0,122,0));
    		setPreferredSize(new Dimension(700, 400));
     
     
    		//create panel components
    		betsLabel = new JLabel ("Bets");
    		betsField = new JTextField(5);
    		trackerLabel = new JLabel ("TRACKER");
    		winLabel = new JLabel ("Win");
    		winField = new JTextField(5);
    		loseLabel = new JLabel ("lose"); 
     
    		loseField = new JTextField(5);
    		playerField = new JTextField(10);
    		playerArea = new JTextArea(5,10);
    		playerLabel = new JLabel("Player");
    		dealerField = new JTextField(10);
    		dealerArea = new JTextArea(5,10);
    		dealerLabel = new JLabel("Dealer");
    		winloseField = new JTextField(10);
    		winloseLabel = new JLabel("Win or Lose");
     
     
     
    		//components/objects
    		dealButton = new JButton("DEAL");
    		hitButton = new JButton("Hit");
    		stayButton = new JButton("Stay");
    		resetButton = new JButton("Reset");
    		highButton = new JButton("Ace High");
    		lowButton = new JButton("Ace Low");
     
    		//add components to the panel (left side)
    		add(betsLabel);
    		add(betsField);
    		add(trackerLabel);
    		add(winLabel);
    		add(winField);
    		add(loseLabel);
    		add(loseField);
    		add(resetButton);
     
    		//add compnents to the panel (player and dealer)		
    		add(playerLabel);
    		add(playerArea);
    		add(dealButton);
    		add(hitButton);
    		add(stayButton);
    		add(highButton);
    		add(lowButton);
    		add(dealerLabel);
    		add(dealerArea);
     
    		//add components to the panel (bottom side)
    		add(winloseLabel);
    		add(winloseField);	
     
    		//connect event handler to event source
    		dealButton.addActionListener(new ButtonListener());
    		hitButton.addActionListener(new ButtonListener());
    		stayButton.addActionListener(new ButtonListener());
     
     
    		//add(Photo,BorderLayout.WEST);
    		image = new ImageIcon("src/BlackJack/blackjack60.jpg");
    		Photo = new JLabel (image);
    		add(Photo);
     
    	}
     
    	//display the screen
    	public void display()
    	{
    		JFrame myFrame = new JFrame("BlackJack");
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		myFrame.setContentPane(this);
     
    		myFrame.pack();
    		myFrame.setVisible(true);
     
    	}
     
    	private class ButtonListener implements ActionListener
    	{
    	   public void actionPerformed (ActionEvent event)
    	   {
    		   double amt = Double.parseDouble(winloseField.getText());
    	   }
    	}
     
     
     
    }

    and here is my Panel so I can run it.

    package BlackJack;
     
    import javax.swing.JFrame;
     
    public class Cards
    {
    public static void main(String[] args)
    {
    	//Create frame and panel
      JFrame frame = new JFrame("BlackJack");
      BlackJackGUI panel = new BlackJackGUI();
     
    	//set frame behavior
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
     
    	//place panel in frame
      frame.getContentPane().add(panel);
     
     
    	//format frame and make available/visible
      frame.pack();
      frame.setVisible(true);
    }
    }

    blackjackGUI.jpg


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GUI Layout - Does anyone know how to set a layout?

    Explore Layout Managers. JPanel uses FlowLayout by default, so you'll see that as you resize the GUI you've built while it's being displayed, the components will move around to fit within the shape of the container but maintain the same order. You may want to use different layout managers in multiple containers to get the look you're going for.

Similar Threads

  1. Help with how to set up my layout
    By RepersGhost in forum AWT / Java Swing
    Replies: 1
    Last Post: November 14th, 2013, 01:45 AM
  2. Noob at GUI's, need layout help.
    By tyb97 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 2nd, 2011, 03:50 PM
  3. Is there a layout for this?
    By gkffjcs in forum AWT / Java Swing
    Replies: 2
    Last Post: September 27th, 2011, 09:15 AM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM