#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Need help, GUI issue


    Hi, I am trying to get the layout of my GUI for my Yahtzee game right and am having issues. The panel(scoringLeft) that i set to BorderLayout.WEST is working great and is how i want it to look. I tried to make the panel(scoringRight) I set to BorderLayout.EAST look the exact same, but it will not work. I'm not getting any errors, but i cannot figure out why the east panel wont show up the same as the west panel. I do not know what needs to be adjusted so I'm posting everything i have so far. Please help, thanks.


    13 public class Driver extends JFrame implements ActionListener {
    14
    15 private static final int FRAME_WIDTH = 700;
    16 private static final int FRAME_HEIGHT = 500;
    17 private static final int X_AXIS = 0;
    18 private static final int Y_AXIS = 0;
    19 private static final int FIELD_WIDTH1 = 10;
    20
    21 JMenu fileMenu, aboutMenu;
    22 JLabel displayTurn, displayRoll, ones,
    23 twos, threes, fours, fives, sixes, threeKind,
    24 fourKind, fullHouse, smStraight, lgStraight, yahtzee, chance,
    25 dice1, dice2, dice3, dice4, dice5;
    26
    27
    28 /**
    29 * @param args the command line arguments
    30 */
    31 public static void main(String[] args) {
    32 Driver frame = new Driver();
    33 frame.setVisible(true);
    34
    35
    36 }
    37
    38 public Driver(){
    39 setTitle("Yahtzee!");
    40 setSize(FRAME_WIDTH, FRAME_HEIGHT);
    41 setLocation(X_AXIS, Y_AXIS);
    42 setResizable(false);
    43 addComponents(getContentPane());
    44 createFileMenu();
    45 JMenuBar menuBar = new JMenuBar();
    46 setJMenuBar (menuBar);
    47 menuBar.add(fileMenu);
    48 setDefaultCloseOperation(EXIT_ON_CLOSE);
    49 }
    50
    51 public void actionPerformed(ActionEvent event){
    52
    53 }
    54
    55 private void createFileMenu()
    56 {
    57 JMenuItem item;
    58
    59 fileMenu = new JMenu("File");
    60
    61 item = new JMenuItem("New Game");
    62 item.addActionListener(this);
    63 fileMenu.add(item);
    64
    65 fileMenu.addSeparator();
    66
    67 item = new JMenuItem("Quit");
    68 item.addActionListener(this);
    69 fileMenu.add(item);
    70 }
    71
    72 public void addComponents(Container contentPane){
    73
    74 contentPane.setLayout(new BorderLayout());
    75
    76 JLabel turn, roll;
    77 JRadioButton hold1, hold2, hold3, hold4, hold5;
    78 JButton rollDice, onesBtn, twosBtn, threesBtn, foursBtn, fivesBtn, sixesBtn, threeKindBtn, fourKindBtn, fullHouseBtn, smStraightBtn, lgStraightBtn, chanceBtn, yahtzeeBtn;
    79 JPanel scoringLeft, scoringRight, diceDisplay;
    80
    81 scoringLeft = new JPanel();
    82 scoringLeft.setBackground(Color.white);
    83 scoringLeft.setLayout(new GridLayout(14,1));
    84
    85 turn = new JLabel("Turn #:");
    86 scoringLeft.add(turn);
    87
    88 displayTurn = new JLabel("");
    89
    90 scoringLeft.add(displayTurn);
    91
    92 onesBtn = new JButton("Ones:");
    93 scoringLeft.add(onesBtn);
    94
    95 ones = new JLabel("");
    96 scoringLeft.add(ones);
    97
    98 twosBtn = new JButton("Twos:");
    99 scoringLeft.add(twosBtn);
    100
    101 twos = new JLabel("");
    102 scoringLeft.add(twos);
    103
    104 threesBtn = new JButton("Threes:");
    105 scoringLeft.add(threesBtn);
    106
    107 threes = new JLabel("");
    108 scoringLeft.add(threes);
    109
    110 foursBtn = new JButton("Fours:");
    111 scoringLeft.add(foursBtn);
    112
    113 fours = new JLabel("");
    114 scoringLeft.add(fours);
    115
    116 fivesBtn = new JButton("Fives:");
    117 scoringLeft.add(fivesBtn);
    118
    119 fives = new JLabel("");
    120 scoringLeft.add(fives);
    121
    122 sixesBtn = new JButton("Sixes:");
    123 scoringLeft.add(sixesBtn);
    124
    125 sixes = new JLabel("");
    126 scoringLeft.add(sixes);
    127
    128 contentPane.add(scoringLeft, BorderLayout.WEST);
    129
    130 scoringRight = new JPanel();
    131 scoringRight.setLayout(new GridLayout(14,1));
    132 scoringRight.setBackground(Color.white);
    133
    134 roll = new JLabel("Roll #:");
    135 scoringRight.add(roll);
    136
    137 displayRoll = new JLabel("");
    138 scoringRight.add(displayRoll);
    139
    140 threeKindBtn = new JButton("3 of a Kind:");
    141 scoringRight.add(threeKindBtn);
    142
    143 threeKind = new JLabel("");
    144 scoringRight.add(threeKind);
    145
    146 fourKindBtn = new JButton("4 of a Kind:");
    147 scoringRight.add(fourKindBtn);
    148
    149 fourKind = new JLabel("");
    150 scoringRight.add(fourKind);
    151
    152 fullHouseBtn = new JButton("Full House:");
    153 scoringRight.add(fullHouseBtn);
    154
    155 fullHouse = new JLabel("");
    156 scoringRight.add(fullHouse);
    157
    158 smStraightBtn = new JButton("Sm. Straight:");
    159 scoringRight.add(smStraightBtn);
    160
    161 smStraight = new JLabel("");
    162 scoringRight.add(smStraight);
    163
    164 lgStraightBtn = new JButton("Lg. Straight:");
    165 scoringRight.add(lgStraightBtn);
    166
    167 lgStraight = new JLabel("");
    168 scoringRight.add(lgStraight);
    169
    170 yahtzeeBtn = new JButton("Yahtzee:");
    171 scoringRight.add(yahtzeeBtn);
    172
    173 yahtzee = new JLabel("");
    174 scoringRight.add(yahtzee);
    175
    176 chanceBtn = new JButton("Chance:");
    177 scoringRight.add(chanceBtn);
    178
    179 chance = new JLabel("");
    180 scoringRight.add(chance);
    181
    182 contentPane.add(scoringRight, BorderLayout.EAST);
    183
    184 setDefaultCloseOperation(EXIT_ON_CLOSE);
    185
    186
    187
    188 }
    189 }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you post the code without the numbers in the first column? The posted code would not compile with those numbers.
    Also there needs to be import statements.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    16
    Your right panel has 16 items, so line 131 needs to configure the GridLayout to (16, 1), not (14, 1).

    Hope this helps.

    slink

    BTW, you can equally configure it as (0, 1). Then it will display a column of however many items there are.
    Last edited by slink; June 7th, 2012 at 11:41 PM.

IMN logo majestic logo threadwatch logo seochat tools logo