Results 1 to 16 of 16
  1. #1
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default How to use BorderLayout

    Im currently trying to use border layout for my craps game and need a little help as i haven't ever used it before. I need to place my welcome message in the North Region, both of my dice in the Center Region and the my remaining components into the South Region. I know that i have to create a new JPanel but could someone assist me in the code for the new class?

    Java Code:
    import javax.swing.JFrame;
    
    public class CrapsGUI
    {
        /********************************************************************
         * Creates and displays the Craps game GUI.
         *******************************************************************/
        public static void main (String[] args)
    Last edited by Daman12; 10-27-2011 at 07:16 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    "could someone assist me in the code for the new JPanel?" isn't a real question. Do you have a specific question, a specific step where you're stuck? Can you tell us how what you've tried doesn't work?

  3. #3
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: How to use BorderLayout

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class BorderLayout extends JPanel {
      
          CrapsGUIPanel panel = new CrapsGUIPanel();
      
       }
    Thats the code i have so far, i need to add dice into the CENTER region of my Panel, My WELCOME MESSAGE to the NORTH region of the panel and anything else i have into the SOUTH region of the panel. I havent ever used BorderLayout so i dont really have a lot that i can show you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    Suggestions
    • Don't name your class BorderLayout unless you really want to confuse the compiler and yourself. This is the same name as a core java class, one that you will want to use, and the compiler will likely use the wrong one. So name it BorderLayoutTest or some such.
    • If you don't know anything about using BorderLayout, learn something about using it! The tutorials explain all, and I urge you to go there first rather than ask others to re-write a tutorial for you. You can find them here: How to use BorderLayout

  5. #5
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: How to use BorderLayout

    Yes i used that link before i posted here. I wasnt sure how to place the dice in this class though. Does it have to do with the actionlistener for the dice?

    I also used this website for help but i dont understand how i get the labels i have already made inside my CRAPSGUIPANEL to get inside the class for the borders, and also dont know how to get the dice in there. This example just shoes buttons with text.
    Last edited by Daman12; 10-27-2011 at 05:26 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    Quote Originally Posted by Daman12 View Post
    Yes i used that link before i posted here.
    Good. It will pretty well show exactly how to use the BorderLayout class.

    I wasnt sure how to place the dice in this class though.
    If Dice derives from any JComponent class (i.e., if it extends JPanel, JLabel, JComponent, ... etc), then you add it to the container just as you would add any other component.

    Does it have to do with the actionlistener for the dice?
    This I don't understand.

  7. #7
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: How to use BorderLayout

    Java Code:
    BorderTest() {
            //... Create components (but without listeners)
            JButton north  = new JButton("North");
            JButton east   = new JButton("East");
            JButton south  = new JButton("South");
            JButton west   = new JButton("West");
            JButton center = new JButton("Center");
    
            //... Create content pane, set layout, add components
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
    
            content.add(north , BorderLayout.NORTH);
            content.add(east  , BorderLayout.EAST);
            content.add(south , BorderLayout.SOUTH);
            content.add(west  , BorderLayout.WEST);
            content.add(center, BorderLayout.CENTER);
    
            //... Set window characteristics.
            setContentPane(content);
            setTitle("BorderTest");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
        }
    }
    Here is a good example but i still dont understand how i get the information from the JLabels and also the dice into this class? Do you understand what im saying?
    Last edited by Daman12; 10-27-2011 at 05:36 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    Quote Originally Posted by Daman12 View Post
    Does it extend JPanel?
    ????

    You've got the code for the Die class, we don't!

  9. #9
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: How to use BorderLayout

    Quote Originally Posted by Fubarable View Post
    ????

    You've got the code for the Die class, we don't!
    I love how you act like im a pro coder like you. This is my first year taking a coding class so bare with me here.

    Here is what GVDie extends:

    public class GVdie extends JPanel implements MouseListener, Comparable{

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    Quote Originally Posted by Daman12 View Post
    Do you understand what im saying?
    Not at all, sorry. Please elaborate. And why did you delete the statement "Does it extend JPanel?"

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    Quote Originally Posted by Daman12 View Post
    I love how you act like im a pro coder like you. This is my first year taking a coding class so bare with me here.
    You are asking about a class that you have and have not shown us. How can we have any chance in hades of knowing? this has nothing to do with being a pro coder and all to do with common sense.

  12. #12
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: How to use BorderLayout

    I put my CrapsGUIPanel up top. Here is what i have so far.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class BorderLayoutExample extends JPanel {
    
        public static void main(String[] args) {
            Frame frame= new Frame("BorderLayout Frame");
            Panel pa= new Panel();
        }
        
    }
    I need to know how to get the information from the CrapsGUIPanel (Such as current labels) into this class so i can locate them within my CrapsGUI. My GVDie class is where the dice are created and i also have to get locate the dice in a certain place on the GUI.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    Again, what is your die class which I'm guessing is your GVDie class? I'm still having trouble groking this whole thing as there are too many gaps for my poor brain to fill.

  14. #14
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: How to use BorderLayout

    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    /*****************************************************************
    A graphical representation of a six-sided die with various controls 
    over the appearance.  Current value is constrained between 1 and 6. 
    You can control the size and color of the dice. 
    <h4>An Example</h4>
    <blockquote><pre><code>
    GVdice die1 = new GVdice();
    die1.roll( );
    int result = die1.getValue(); 
    </code></pre></blockquote>
    <p> 
    @author Scott Grissom
    @version 1.4 October 10, 2006
    *****************************************************************/
    public class GVdie extends JPanel implements MouseListener, Comparable{
    /** current value of the die */
    private int myValue, displayValue; 
    
    /** is the dice currently on hold? */
    private boolean held;
    
    /** current size in pixels */
    private int mySize;
    
    /** dot size in pixels defined by overall die size */ 
    private int dotSize;
    
    /** offset in pixels for the left row of dots */ 
    private int left;
    
    /** offset in pixels for the right row of dots */ 
    private int right;
    
    /** offset in pixels for the middle dot */ 
    private int middle;
    
    /** delay for animation */
    private int DELAY;
    
    /** color of the dice when held */
    private Color HELD_COLOR = Color.pink;
    
    /** default color of dice */
    private Color BACKGROUND = Color.white;
    
    /** repeats for animation */
    private int NUM_ROLLS;
    
    /** Timer for animation */
    private javax.swing.Timer myTimer;
    
    
    /*****************************************************************
    constructor creates a die of specified size X size pixels
    
    @param size the length of each side in pixels
    *****************************************************************/
    public GVdie(int size) {
    // initialize the die and determine display characteristics 
    mySize = size;
    held = false; 
    dotSize = mySize / 5;
    int spacer = (mySize - (3*dotSize))/4; 
    left = spacer; 
    right = mySize - spacer - dotSize;  paint dots    
    switch (displayValue){   
        case 1:
            g.fillOval (middle,middle,dotSize,dotSize); 
            break;
        case 2:
            g.
            g.fillOval (right,left,dotSize,dotSize); 
            g.fillOval (right,middle,dotSize,dotSize); 
            g.fillOval (right,right,dotSize,dotSize); 
            break;
        }   
    
    }
    
    /*****************************************************************
    respond to the dice being clicked
    
    @param e the mouse event
    *****************************************************************/
    public void mouseClicked(MouseEvent e){
        if(held){
            held = false;
            setBackground(BACKGROUND);
        }else{
            held = true;
            setBackground(HELD_COLOR);
        }
        repaint();
        
    }
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    
    /*****************************************************************
    allows dice to be compared if necessary
    
    @param o compare the dice with this object
    @return -1 if dice is less than passed object
    *****************************************************************/
    public int compareTo(Object o){
        GVdie d = (GVdie) o;
        return getValue() - d.getValue();
    } 
    
    /******************************************************
    INNER class to roll the dice as an animation
    ******************************************************/
    private class Animator implements ActionListener{
    int count = 0;
    public void actionPerformed(ActionEvent e){
            displayValue = (int) (Math.random()*6)+1; 
            repaint();
        count++;
        // Should we stop rolling?
        if (count == NUM_ROLLS){
            count=0;
            myTimer.stop();
            displayValue = myValue;
            repaint();
        }
    }
    }    
    
    
    }
    
    Here is my GVDie class.
    Last edited by Daman12; 10-27-2011 at 07:16 AM.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to use BorderLayout

    OK, so we now see that the GVdie class (which is your teacher's code, correct?) does in fact extend JPanel.

    I think that you'll want to create a JPanel to hold both of the die, give this JPanel a GridLayout(1, 2) so that the dice are held in a single row with two columns, and then after adding each die to this JPanel, add this JPanel to your main GUI BorderLayout.CENTER.

    As far as extracting the information, this is done the same as in your current code above.

  16. #16
    Daman12 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: How to use BorderLayout

    Java Code:
    GridLayout experimentLayout = new GridLayout(0,2);
    
    ...
    
            compsToExperiment.setLayout(experimentLayout);
    
            compsToExperiment.add(new JButton("Button 1"));
            compsToExperiment.add(new JButton("Button 2"));
            compsToExperiment.add(new JButton("Button 3"));
            compsToExperiment.add(new JButton("Long-Named Button 4"));
            compsToExperiment.add(new JButton("5"));
    So what would i put where they have ("Button 1")) and so forth? Yes that is my teachers GVDie class. I understand these examples im seeing but i dont understand where to change the information at so it reflects my project.

Similar Threads

  1. BorderLayout for 2 Panels
    By collwill in forum New To Java
    Replies: 2
    Last Post: 03-31-2011, 04:49 AM
  2. BorderLayout problem
    By TGH in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 10:51 PM
  3. BorderLayout
    By oneself in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 11:59 PM
  4. BorderLayout Demo
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:51 PM
  5. Help with BorderLayout
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:26 AM

Posting Permissions

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