本文介绍了我的jlabels没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不创建一个记忆集中游戏,除了我的标签没有出现之外,我有一些问题。我会发布我所拥有的所有内容,因为我不确定问题出在哪里。



我尝试了什么:



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:

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();
    }
    
}

推荐答案


这篇关于我的jlabels没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:37