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

Thread: Need help please, cannot get GUI to work right.

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help please, cannot get GUI to work right.

    Hi, I am trying to get the layout of my GUI for my Yahtzee game right and am having issues. The panel that i set to BorderLayout.WEST is working great and is how i want it to look. I tried to make the panel I set to BorderLayout.EAST to look the exact same, but it will not work. Im not getting any errors, but i cannot figure out why the other side wont show up the same as the WEST panel. Please help, thanks.


     
    D:\JavaII\YahtzeeGame\src\yahtzeegame\Driver.java
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package yahtzeegame;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /**
     *
     * @author Owner
     */
    public class Driver extends JFrame implements ActionListener {
     
        private static final int FRAME_WIDTH = 700;
        private static final int FRAME_HEIGHT = 500;
        private static final int X_AXIS = 0;
        private static final int Y_AXIS = 0;
        private static final int FIELD_WIDTH1 = 10;
     
        JMenu fileMenu, aboutMenu;
        JLabel  displayTurn, displayRoll, ones, 
                twos, threes, fours, fives, sixes, threeKind,
                fourKind, fullHouse, smStraight, lgStraight, yahtzee, chance,
                dice1, dice2, dice3, dice4, dice5;
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Driver frame = new Driver();
            frame.setVisible(true);
     
     
        }
     
        public Driver(){
            setTitle("Yahtzee!");
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
                setLocation(X_AXIS, Y_AXIS);
                setResizable(false);
                addComponents(getContentPane());
                createFileMenu();
                JMenuBar menuBar = new JMenuBar();
                setJMenuBar (menuBar);
                menuBar.add(fileMenu);
                setDefaultCloseOperation(EXIT_ON_CLOSE); 
        }
     
        public void actionPerformed(ActionEvent event){
     
        }
     
        private void createFileMenu()
            {
                    JMenuItem item;
     
                    fileMenu = new JMenu("File");
     
                    item = new JMenuItem("New Game");
                    item.addActionListener(this);
                    fileMenu.add(item);
     
                    fileMenu.addSeparator();
     
                    item = new JMenuItem("Quit");
                    item.addActionListener(this);
                    fileMenu.add(item);
            }
     
       public void addComponents(Container contentPane){
     
           contentPane.setLayout(new BorderLayout());
     
           JLabel turn, roll;
           JRadioButton hold1, hold2, hold3, hold4, hold5;
           JButton rollDice, onesBtn, twosBtn, threesBtn, foursBtn, fivesBtn, sixesBtn, threeKindBtn, fourKindBtn, fullHouseBtn, smStraightBtn, lgStraightBtn, chanceBtn, yahtzeeBtn;
           JPanel scoringLeft, scoringRight, diceDisplay;
     
           scoringLeft = new JPanel();
           scoringLeft.setBackground(Color.white);
           scoringLeft.setLayout(new GridLayout(14,1));
     
           turn = new JLabel("Turn #:");
           scoringLeft.add(turn);
     
           displayTurn = new JLabel("");
     
           scoringLeft.add(displayTurn);
     
           onesBtn = new JButton("Ones:");
           scoringLeft.add(onesBtn);
     
           ones = new JLabel("");
           scoringLeft.add(ones);
     
           twosBtn = new JButton("Twos:");
           scoringLeft.add(twosBtn);
     
           twos = new JLabel("");
           scoringLeft.add(twos);
     
           threesBtn = new JButton("Threes:");
           scoringLeft.add(threesBtn);
     
           threes = new JLabel("");
           scoringLeft.add(threes);
     
           foursBtn = new JButton("Fours:");
           scoringLeft.add(foursBtn);
     
           fours = new JLabel("");
           scoringLeft.add(fours);
     
           fivesBtn = new JButton("Fives:");
           scoringLeft.add(fivesBtn);
     
           fives = new JLabel("");
           scoringLeft.add(fives);
     
           sixesBtn = new JButton("Sixes:");
           scoringLeft.add(sixesBtn);
     
           sixes = new JLabel("");
           scoringLeft.add(sixes);
     
           contentPane.add(scoringLeft, BorderLayout.WEST);
     
           scoringRight = new JPanel();
           scoringRight.setLayout(new GridLayout(14,1));
           scoringRight.setBackground(Color.white);
     
           roll = new JLabel("Roll #:");
           scoringRight.add(roll);
     
           displayRoll = new JLabel("");
           scoringRight.add(displayRoll);
     
           threeKindBtn = new JButton("3 of a Kind:");
           scoringRight.add(threeKindBtn);
     
           threeKind = new JLabel("");
           scoringRight.add(threeKind);
     
           fourKindBtn = new JButton("4 of a Kind:");
           scoringRight.add(fourKindBtn);
     
           fourKind = new JLabel("");
           scoringRight.add(fourKind);
     
           fullHouseBtn = new JButton("Full House:");
           scoringRight.add(fullHouseBtn);
     
           fullHouse = new JLabel("");
           scoringRight.add(fullHouse);
     
           smStraightBtn = new JButton("Sm. Straight:");
           scoringRight.add(smStraightBtn);
     
           smStraight = new JLabel("");
           scoringRight.add(smStraight);
     
           lgStraightBtn = new JButton("Lg. Straight:");
           scoringRight.add(lgStraightBtn);
     
           lgStraight = new JLabel("");
           scoringRight.add(lgStraight);
     
           yahtzeeBtn = new JButton("Yahtzee:");
           scoringRight.add(yahtzeeBtn);
     
           yahtzee = new JLabel("");
           scoringRight.add(yahtzee);
     
           chanceBtn = new JButton("Chance:");
           scoringRight.add(chanceBtn);
     
           chance = new JLabel("");
           scoringRight.add(chance);
     
           contentPane.add(scoringRight, BorderLayout.EAST);
     
           setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
     
    }
    }
    Last edited by swerdnick; June 7th, 2012 at 08:12 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Need help please, cannot get GUI to work right.

    Also posted at: Need help, GUI issue - Dev Shed

    See the response at that site.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please, cannot get GUI to work right.

    Ok, i removed the line numbers.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Need help please, cannot get GUI to work right.

    Sorry, I forgot to ask you to wrap the code in code tags to preserve its formatting:
    Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please, cannot get GUI to work right.

    done, thanks for explaining that to me.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Need help please, cannot get GUI to work right.

    Not great on GUI and layouts. I changed this line and looked at the layout:
           displayRoll = new JLabel("xxx");
    That should give you a clue as to what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please, cannot get GUI to work right.

    Yes i know it doesn't look amazing, I am just using the same layouts that were taught in the class I am taking. Ok, so i can see that its placing the labels next to all of the buttons instead of between them like i want. Shouldn't the GridLayout(14,1) make it so there is only one column instead of two?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Need help please, cannot get GUI to work right.

    How many components are you adding to the container?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please, cannot get GUI to work right.

    Oh I see, I didn't realize i had more components in the panel I was placing in the right side. I just raised the number of rows with my GridLayout and now they fit nicely. Thank you much.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Need help please, cannot get GUI to work right.

    Details. details, details.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how does this work?
    By somebodyonearth in forum Java Theory & Questions
    Replies: 3
    Last Post: March 10th, 2012, 12:23 PM
  2. Does this work?
    By marmanq in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 20th, 2012, 09:51 AM
  3. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 03:25 AM
  4. Do you work out?
    By thesolo in forum Totally Off Topic
    Replies: 4
    Last Post: October 24th, 2011, 08:03 AM
  5. Why does this not work?
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 09:47 AM