Memory/Concentration Game Problems - java

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.

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)

Related

How to make a Jframe containing a Jpanel scrollable?

So I have this JFrame that contains a JPanel and in there I add JLabels with information I want but since I'll be adding labels all the time at some point the text is too long to appear so I want to add a scrollbar. Basically I want to make my JFrame with a JPanel in it scrollable. I have this code but my problem is that even though the scrollbar appears but it doesnt move and doesn't really work when the text is a lot, meaning the text still gets cut out and the scrollbar is there not moving. Does anyone know how to fix this?
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Bar {
JFrame info = new JFrame("Information");
JLabel ballinf = new JLabel();
JPanel contentPane = new JPanel();
JScrollPane scrolling = new JScrollPane();
public Bar(){
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
scrolling = new JScrollPane(contentPane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
info.add(scrolling);
info.setSize(750, 600);
info.setLocationByPlatform(true);
info.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
info.setVisible(true);
}
public void adding(int pos){
ballinf = new JLabel("Something ",JLabel.CENTER);//assume the text will be bigger here and have more info
ballinf.setSize(700, 30);
ballinf.setForeground(Color.green);
ballinf.setLocation(5, 5+pos);
contentPane.add(ballinf);
info.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
info.setVisible(true);
}
public static void main(String[] args){
Bar stats = new Bar();
stats.adding(0);
stats.adding(20);//this will be done in a for loop for more than 2 times so the text ends up to be a lot
}
}
contentPane.setLayout(null);
Don't use a null layout!!!
You need to use an appropriate layout manager. Read the section from the Swing tutorial on Layout Managers for more information and working examples. The layout manager will then determine the preferred size of the panel as you add components to the panel.
The scrollpane will then display the scrollbars when necessary.
If you dynamically add components to the panel (after the GUI is visible) then the code should be something like:
panel.add(...);
panel.revalidate();
panel.repaint();

How to reset or refresh Jframe with new values

I have a Jframe that the user enters new information through a Joptionpane, it is added to an array which is then appended and displayed to a contentpane.. the cycle then repeats till the user enters "STOP". Currently the program is outputting the new array under the old one. How would I clear away the old array in the content pane and only display the new values?
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
public class Project1GUI {
static JFrame Frame;
static TextArea unsorted_words, sorted_words, linked_words;
public Project1GUI(String title){
//All this does is make an empty GUI FRAME.
Frame=new JFrame();//i made a new variable from the JFrame class
Frame.setSize(400,400);//Used the Variable from JFrame and used some of it functions. This function sets the hieght and width of the Frame
Frame.setLocation(200,200);//This sets where the Empty Frame should be
Frame.setTitle(title);//This puts a title up top of the Frame
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//places an x box that closes when clicked on
Frame.setVisible(true);//This activates the JFram when is set true.
Frame.setLayout(new GridLayout(1,2));//This sets the layout of the Frame and since i want a Grid i used a GirdLayout
//Functions and placed it inside the setlayout functions. to get 2 grids i places 1 meaning 1 row and 2 for 2 cols
unsorted_words=new TextArea(); //From the TextArea class i made three variables
sorted_words= new TextArea();
linked_words= new TextArea();
Container panel=new Container();
panel=Frame.getContentPane();
panel.add(unsorted_words);
panel.add(sorted_words);
panel.add(linked_words);
}
public void add_unsorted(String words){
unsorted_words.append(words+"\n");//add words to GUI
}
public void add_sorted(String words){
sorted_words.append(words+"\n");
}
public void add_linked(List<String> linked_words2){
linked_words.append(linked_words2+"\n");
}
}
For a more definitive answer, post an MCVE
Seeing as you haven't posted any code, I'm guessing you are using a JLabel or a JList or something of that sort to display the array. No matter which one you are doing, you need to tell the component to update it's content, it doesn't just do it itself. To do that, you need to call the components .setText() or similar method.
If you have a JLabel or JTextArea it could look like this.
labelOrTextArea.setText("New Text");
If you are using a JList you should update the lists Default List Model like this
dlm.addElement("New Text");
UPDATE
I see a couple things wrong with your code. First off JFrame Frame = new JFrame conventionally, variables should start with a lower case letter and they should not contain underscores '_'. You are also using AWT Components instead of Swing components. You should be using the likes of JTextArea, JPanel (Theres no JContainer), JLabel etc.
You are also never adding the panel to the frame.
frame.add(panel);
You should also not be adding stuff to the frame or panels after you set its visibility to true. So you should setup your frame like this
import javax.swing.*;
import java.awt.*;
import java.util.List;
public class Project1GUI
{
JTextArea unsorted_words, sorted_words, linked_words;
public Project1GUI()
{
JFrame frame = new JFrame("Title");
JPanel panel = new JPanel(new GridLayout(2, 1));
unsorted_words = new JTextArea();
sorted_words = new JTextArea();
linked_words = new JTextArea();
panel.add(unsorted_words);
panel.add(sorted_words);
panel.add(linked_words);
frame.add(panel);
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You can then implement the methods you currently have and call them in an ActionListener or such.
Result:
On top of all of that, you should not rely on the use of static as it takes away from the main points of OOP.

New to creating GUIs. How would I combine text area and radio buttons

I understand how to create radio buttons and I understand how to create a text area. What I have trouble with is combing the two onto one frame.
Can anyone help me or show me how to do so? Do I have to layout a grid with boarders?
Here I was trying to follow my text book on ways to combine the two but I sort of failed. :(
I've tried other ways but none have worked for me. When I attempt to do the text area and radio buttons separately, they work.
This isn't my final code. My finish product should allow me to keep track of how many times a certain student is picked, like a vote, and printing out the totals in the text area each time.
Getting this part to work will help me further advance in my code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class AsnTwo extends JFrame
{
private JRadioButton jrbS1 = new JRadioButton("Student 1");
private JRadioButton jrbS2 = new JRadioButton("Student 2");
private JRadioButton jrbS3 = new JRadioButton("Student 3");
private JTextArea jtaT = new JTextArea("Hello");
public static void main(String[] args)
{
AsnTwo frame = new AsnTwo();
JPanel panel = new JPanel();
frame.pack();
frame.setTitle("Assignment Two");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public AsnTwo()
{
JPanel jpTextArea = new JPanel();
jpTextArea.setLayout(new GridLayout(2,1));
jtaT.setEditable(false);
jtaT.setLineWrap(true);
jtaT.setText("Something");
add(jpTextArea, BorderLayout.EAST);
JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(3,1));
jpRadioButtons.add(jrbS1);
jpRadioButtons.add(jrbS2);
jpRadioButtons.add(jrbS3);
add(jpRadioButtons, BorderLayout.WEST);//Adds buttons to GUI
ButtonGroup group = new ButtonGroup();
group.add(jrbS1);
group.add(jrbS2);
group.add(jrbS3);
}
public void printTextField(String text)
{
jtaT.setText("Hello");
}
}
Never mind. I got it. I just had to change
add(jpTextArea, BorderLayout.EAST);
to
add(jtaT, BorderLayout.EAST);

Moving a button into another class JPanel - Becker Robots

I am using the Becker Robots making a board game with two player. One Random and one Human (me).
The player should click UP, DOWN, LEFT, RIGHT buttons.
So far: I have created 2 classes. One that displays the board (9,9) and one that contains the player buttons.
I want to move this button into my main board display board. However, I want to keep these classes seperate because in order to make them do something I will need a listener class for each button which also redirects to the main.
import becker.robots.*;
import javax.swing.*;
public class PlayerButtons {
public static void main(String[] args) {
JFrame frame = new JFrame ("Test");
frame.setVisible(true);
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contents = new JPanel();
JButton upButton = new JButton("UP");
JTextArea textDisplay = new JTextArea (5,10);
//set it up
contents.add(upButton);
contents.add(textDisplay);
//display in panel
frame.setContentPane(contents);
}
}
I think you should take a look to MODEL VIEW CONTROLER:
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
It also could help if you post more code, but guessing, you could create a listener and check the boton id. On that way you'll be able to have just one listener.

JPanel only showing one component when added to container

Hello all I am having a bit of an issue with this. I created a JPanel and added components to it and then added the JPanel to container. Now when I call this class from main a window pops up but it only displays the first component of the JPanel. Why is it only showing the first item and not all of them? Thanks.
Note: this code is not complete, I am simply trying to figure out why my components are not showing up before moving on to other things, please just address the components issue.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class Player extends JFrame implements ActionListener
{
private CardLayout playerCard;
private JPanel cardPanel;
public String player1;
public String player2;
// Constructor:
public Player()
{
setTitle("Game");
setSize(300,200);
setLocation(10,200);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
//set up the panel
cardPanel = new JPanel();
playerCard = new CardLayout();
cardPanel.setLayout(playerCard);
//get player one name
JLabel p1Name = new JLabel("Player 1 Name:");
JTextField oneName = new JTextField();
//get the name for player 2
JLabel p2Name = new JLabel("Player 2 Name:");
JTextField twoName = new JTextField();
//the button to start the game
JButton start = new JButton("Start");
//add the components << Why is only the first component shown??
cardPanel.add(start);
cardPanel.add(p1Name);
cardPanel.add(oneName);
cardPanel.add(p2Name);
cardPanel.add(twoName);
contentPane.add("startCard",cardPanel);
}
#Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
}
}
You're not using your layouts correctly. You use the String constant when adding components to the CardLayout using component, not the FlowLayout using component. And the String constant goes after the component in the add method. Please read the layout manager tutorial since this is all explained quite well there. It looks like you're using CardLayout where it shouldn't be used, and this is why you're only seeing one component. In other words, your program is using layouts completely bass ackwards.
In other words, the container that uses CardLayout can only show one component at a time, meaning, since cardPanel uses CardLayout, it can only display one component, here twoName will likely be the only thing showing on it.

Categories

Resources