Results 1 to 16 of 16
Thread: How to use BorderLayout
- 10-27-2011, 02:36 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
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 06:16 AM.
-
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?
- 10-27-2011, 02:56 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: How to use BorderLayout
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.Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class BorderLayout extends JPanel { CrapsGUIPanel panel = new CrapsGUIPanel(); }
-
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
- 10-27-2011, 04:20 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
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 04:26 AM.
-
Re: How to use BorderLayout
Good. It will pretty well show exactly how to use the BorderLayout class.
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.I wasnt sure how to place the dice in this class though.
This I don't understand.Does it have to do with the actionlistener for the dice?
- 10-27-2011, 04:30 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: How to use BorderLayout
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?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(); } }Last edited by Daman12; 10-27-2011 at 04:36 AM.
-
Re: How to use BorderLayout
- 10-27-2011, 04:37 AM #9
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
-
Re: How to use BorderLayout
-
Re: How to use BorderLayout
- 10-27-2011, 04:44 AM #12
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: How to use BorderLayout
I put my CrapsGUIPanel up top. Here is what i have so far.
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.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(); } }
-
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.
- 10-27-2011, 04:52 AM #14
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
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 06:16 AM.
-
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.
- 10-27-2011, 05:30 AM #16
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: How to use BorderLayout
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.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"));
Similar Threads
-
BorderLayout for 2 Panels
By collwill in forum New To JavaReplies: 2Last Post: 03-31-2011, 03:49 AM -
BorderLayout problem
By TGH in forum New To JavaReplies: 3Last Post: 05-27-2010, 09:51 PM -
BorderLayout
By oneself in forum New To JavaReplies: 3Last Post: 08-06-2009, 10:59 PM -
BorderLayout Demo
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:51 PM -
Help with BorderLayout
By lenny in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks