0

So, I have been working on a Java Memory/Concentration Game assignment. I've not gotten as far as I wanted, it's only half finished, but I did have the GUI mostly working... Until I tried to add radio buttons to my Frame. I think the problem might be because I changed a JFrame(CardButtonPanelFrame) into a JPanel. I'm trying to add 3 JPanels to a larger JPanel which I add to a JFrame. I'm just getting a small blank window popping up when I used to have all 52 cards pop up.

Basically when I work on projects things can get out of control in their complexity so I thought I'd come here to make sure I'm heading in the right direction.

Here's my main:

import javax.swing.*;

public class Project3{
public static void main(String[] args){

    JFrame frame = new JFrame();

    Grid game = new Grid();

    frame.pack();
    frame.add(game);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}
}

Then this is outermost JPanel I'd like to hold the radio buttons at the top, the cards in the middle, and eventually the score at the bottom.

import java.awt.*;
import javax.swing.*;

public class Grid extends JPanel{

public Grid(){

    JPanel panel = new JPanel(); //construct a frame
    CardButtonPanelFrame buttons = new CardButtonPanelFrame();
    wtf choices = new wtf();

    panel.setLayout(new GridLayout(3,1)); //that panel uses GridLayout

    panel.add(choices);//add the panels to the Frame
    panel.add(buttons);
    //frame.add(scores);

    add(panel);
    setVisible(true);

}
}

This is the radiobuttons panel... named wtf because I had some compiling issues and experimented with changing the name. I've not even gotten to the stage of figuring out how to implement the different player amounts yet.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class wtf extends JPanel implements ActionListener {
   static String zerostring = "Zero Player Game";
   static String onestring = "One Player Game";
   static String twostring = "Two Player Game";

   public wtf() {
     super(new BorderLayout());

     //Create the radio buttons.
     JRadioButton zeroButton = new JRadioButton(zerostring);
     zeroButton.setMnemonic(KeyEvent.VK_C);
     zeroButton.setActionCommand(zerostring);

     JRadioButton oneButton = new JRadioButton(onestring);
     oneButton.setMnemonic(KeyEvent.VK_B);
     oneButton.setActionCommand(onestring);
     oneButton.setSelected(true);

     JRadioButton twoButton = new JRadioButton(twostring);
     twoButton.setMnemonic(KeyEvent.VK_D);
     twoButton.setActionCommand(twostring);

     //Group the radio buttons.
     ButtonGroup group = new ButtonGroup();
     group.add(zeroButton);
     group.add(oneButton);
     group.add(twoButton);

     //Register a listener for the radio buttons.
     zeroButton.addActionListener(this);
     oneButton.addActionListener(this);
     twoButton.addActionListener(this);


     //Put the radio buttons in a column in a panel.
     JPanel radioPanel = new JPanel(new GridLayout(0, 1));
     radioPanel.add(zeroButton);
     radioPanel.add(oneButton);
     radioPanel.add(twoButton);


     add(radioPanel, BorderLayout.LINE_START);
     setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}

/** Listens to the radio buttons. */
public void actionPerformed(ActionEvent e) {
   //do something with e.getActionCommand()
}
}

So I have two more classes but I think the current problem is here and I'm afraid of making this a giant wall of code. I have more questions but I think I'll take it one at a time so I don't end up posting pages and pages of code that no one wants to read.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Confused
  • 176
  • 3
  • 15
  • 3
    Add what is the problem exactly? –  Nov 25 '12 at 01:34
  • It's basically another, "here's my code, please fix it for me". Sorry, but that's not how it works here. Please take some time to give more details on your problem so that we can understand it enough to be able to help. – Hovercraft Full Of Eels Nov 25 '12 at 01:36
  • When I tried putting the wtf panel and the CardButtonPanelFrame into a bigger panel nothing but a empty window pops up. I used to just have CardButtonPanelFrame by itself and that was working. – Confused Nov 25 '12 at 01:37
  • Sorry, I was trying to be concise because I can be overly wordy. My biggest hope is that people will be nice honestly. Not 'fix' things for me. – Confused Nov 25 '12 at 01:38
  • Nice is good, but helpful is better. Please give us enough information to allow us to possibly be helpful. – Hovercraft Full Of Eels Nov 25 '12 at 01:41
  • I'm not entirely sure how to give more details. The CardButtonPanelFrame is a class which displays 52 cards that are face down and they are all JButtons. That worked fine when I had that set to come up by itself. But I wanted to add the radio buttons above the cards and the scores below the cards so I thought I'd make a 3x1 grid layout where each element in the grid was a panel. I left the 3rd blank for now but made a radio button one for the top, and the cards for middle. Now only a blank window comes up. – Confused Nov 25 '12 at 01:43
  • I'm not sure how to ask for help without asking for help as that seems to be offensive. I think it has something to do with how Im adding the panels maybe. I dont know.... – Confused Nov 25 '12 at 01:44
  • This Q&A discusses how to [set and disable icons of JToggleButton](http://stackoverflow.com/questions/7877576/set-and-disable-icons-of-jtogglebutton) in the context of a memory game. – trashgod Nov 25 '12 at 01:52

1 Answers1

2

One problem you're doing is calling pack() on your JFrame before adding components. Don't do that, but instead add all components that you can first, then call pack(), then setVisible(true)

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373