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

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Java- Beginners problem- but Challenge yourselves :)


    Hey everyone, I'm new here and I have a challenge for a beginners program for expert programmers, I wonder if you can help me out. I am currently making a memory card game, and I'd like to know why when I reset the game, the items in the pnl class don't reappear. I think it is because I set the pnl layout to null- I needed to do that so I could position the scorepnl in the correct spot. This code is not complete, but I need the restart option to reset everything correctly- is there a way to set the pnl layout back to the way it was at some point in the code? Any help in fixing up would be much appreciated. Thanks
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package main;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    /**
    *
    * @author-----
    */
    public class MainView extends JFrame implements ActionListener {
    Container contentPane = getContentPane();
    JPanel pnl= new JPanel();
    JTextArea infotext = new JTextArea( 2 , 80 ) ;
    ModelCard theCardValues;
    ModelScore theScore;
    ClassLoader ldr= this.getClass().getClassLoader();
    JButton info= new JButton ("Help");
    JButton restart= new JButton ("Restart");
    JButton exit= new JButton ("Exit");
    JLabel credits= new JLabel (" Created by ___ 2012");
    java.net.URL titleURL= ldr.getResource("title.png");
    ImageIcon title= new ImageIcon(titleURL);
    JLabel titlebtn= new JLabel(title);
    int visibility;
    JPanel scorepnl= new JPanel();
    String matches= "Matches: ";
    String mistakes= "Turns Taken: ";
    JLabel matcheslbl= new JLabel(matches);
    JLabel mistakeslbl= new JLabel(mistakes);
    JPanel cardpnl= new JPanel();

    public void scorePanel()
    {
    pnl.add(scorepnl);
    pnl.setLayout(null);
    scorepnl.add(matcheslbl);
    scorepnl.add(mistakeslbl);
    matcheslbl.setForeground(Color.red);
    matcheslbl.setFont(new Font("Arial", Font.BOLD, 22));
    mistakeslbl.setForeground(Color.red);
    mistakeslbl.setFont(new Font("Arial", Font.BOLD, 22));
    scorepnl.setBounds(750, 300, 200, 200) ;
    scorepnl.setOpaque(false);
    }
    public void cardPanel()
    {
    contentPane.add(cardpnl);
    cardpnl.setLayout(new GridLayout(4,4));

    }
    public void menuButtons()
    {
    contentPane.add(pnl);
    info.addActionListener(this);
    restart.addActionListener(this);
    exit.addActionListener(this);
    pnl.add(titlebtn);
    pnl.add(info);
    pnl.add(restart);
    pnl.add(exit);
    pnl.add(credits);
    pnl.add(infotext);
    title.getIconHeight();
    title.getIconWidth();
    infotext.setOpaque(false);
    infotext.setVisible(false);
    infotext.setText("Welcome to Concentration Trivia! Concentration Trivia is a challenging game"
    + " of memory and knowledge with a twist!"
    + " The aim is to match each question to their answers in"
    + " the least number of turns. Each turn involves flipping"
    + " over two cards. "
    + " Good luck, and have fun!"
    + " CLICK HELP TO HIDE TEXT");
    infotext.setLineWrap(true);
    infotext.setWrapStyleWord(true);
    infotext.setBounds(50, 90, 900, 300);
    credits.setOpaque(false);
    credits.setForeground(Color.blue);
    credits.setVerticalTextPosition(credits.CENTER);
    credits.setHorizontalTextPosition(credits.CENTER);
    }
    public void actionPerformed( ActionEvent event )
    {

    if( event.getSource() == exit)
    {
    System.exit(0);
    }
    if( event.getSource()== info)
    {
    if (visibility == 0){
    infotext.setVisible(true);
    visibility++;
    }
    else {
    infotext.setVisible(false);
    visibility--;
    }
    }

    if( event.getSource()== restart)
    {
    this.dispose();
    contentPane.setLayout(new BorderLayout());
    Concentration.main(new String[0]);
    }
    }
    public MainView ()
    {

    //The title caption is being set
    super("Concentration");
    //An instance of the card values and the score are implemented
    theCardValues = new ModelCard();
    theScore = new ModelScore();
    //The size of the Concentration window is being set
    setSize(1000,750);
    //The default operation when the window is closed is set to exit
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    //The window is visible
    setVisible(true);
    //Stop user from resizing window
    setResizable(false);
    setLocation(125,5);
    pnl.setBackground(Color.yellow);

    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    42
    Rep Power
    5
    Put code tags around your code please.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Nevermind, I figured it out

IMN logo majestic logo threadwatch logo seochat tools logo