Click here to Skip to main content
15,881,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having to create a memory concentration game and I have pretty much everything except I'm having a bit of an issue with my labels not showing up. I'll post everything I have since I'm not sure where the issue could be.

What I have tried:

Java
package more.objects;
/**
 *
 * @author stephenwessels
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MoreObjects extends JFrame
     implements ActionListener, MouseListener
{
    Container content = this.getContentPane();
    
    JLabel[] lblBoard = new JLabel[16];
    int[] nums;
    int firstChoice = -1;
    int tries = 0;
    
    JPanel pnlCntrls = new JPanel();
    JPanel pnlBoard = new JPanel();
    JLabel lblFirst = new JLabel();
    JButton btnGame = new JButton("New Game");
    JLabel lblTries = new JLabel("0");
    JLabel numTries = new JLabel("Amount of tries:");
    Font fnt = new Font("Helvetica",Font.BOLD,24);
    
    public MoreObjects()
    {   
        this.setVisible(true);
        this.setSize(350,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Memory Game");
        
        
        
        content.add(pnlCntrls, BorderLayout.SOUTH);
        pnlCntrls.setLayout(new FlowLayout());
        pnlCntrls.add(btnGame);
        pnlCntrls.add(numTries);
        pnlCntrls.add(lblTries);
        
        btnGame.addActionListener(this);
    }
    
    
    public JLabel[] createLabels()
    {   
        content.add(pnlBoard);
        pnlBoard.setLayout(new GridLayout(4,4,5,5));
        for(int i = 0; i < 16; i++)
        {
           lblBoard[i].setOpaque(true);
           lblBoard[i].setBackground(Color.white);
           lblBoard[i].setForeground(Color.blue);
           lblBoard[i].setFont(fnt);
           lblBoard[i].addMouseListener(this);
           lblBoard[i].setName("" + i );
           lblBoard[i] = new JLabel("Test", JLabel.CENTER);
           pnlBoard.add(lblBoard[i]);
        }
        return null;
    }
    
    
    public void shuffle()
    {
        int num1, num2, tmp;
        Random r = new Random();
        
        for(int x = 0; x < nums.length; x++)
        {
            num1 = r.nextInt(15);
            num2 = r.nextInt(15);
            
            
            tmp = nums[num1];
            nums[num1] = nums[num2];
            nums[num2] = tmp;
        }
    }
    
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        shuffle();
        firstChoice = -1;
 
        for (int i = 0; i < lblBoard.length; i++) 
        {
            lblBoard[i].setText("");
        }
        tries = 0;
        lblTries.setText("" + tries);
    }
    
    @Override
    public void mouseClicked(MouseEvent e) 
    {
        JLabel l = (JLabel) e.getSource();
        if(firstChoice == -1)
        {
            l.setText("" + nums[1]);
            lblFirst = l;
            firstChoice = nums[1];
        }else if(nums[1] != nums[firstChoice])
        {
            l.setText("" + nums[1]);
            pnlBoard.paintImmediately(0,0, pnlBoard.getWidth(), pnlBoard.getHeight());
            try 
            {
                Thread.sleep(250);
            }catch(InterruptedException ie) 
            {
                lblFirst.setText("");
                l.setText("");
                lblFirst = null;
                firstChoice = -1;
                tries++;
            }
        }else
        {
            l.setText("" + nums[1]);
            firstChoice = -1;
            tries++;
            lblTries.setText("" + tries);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) 
    {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) 
    {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) 
    {
        
    }

    @Override
    public void mouseExited(MouseEvent e) 
    {
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        MoreObjects gui = new MoreObjects();
    }
    
}
Posted
Updated 11-Mar-18 21:42pm
v2
Comments
Maciej Los 12-Mar-18 4:07am    
Again???
wseng 12-Mar-18 4:22am    
What?
Richard MacCutchan 12-Mar-18 4:58am    
It's a repost of a repost.
wseng 12-Mar-18 5:07am    
I see
Richard MacCutchan 12-Mar-18 4:57am    
This is the third time you have posted this question. Please follow the advice you have been given already.

1 solution

You probably miss to call createLabels() in somewhere.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900