JTextField won't disappear completely

Riotson

I created a dialog box and have the user enter 5 colors in it from memory. That all completely works, there's just a slight aesthetic problem. Upon entering all 5 colors correctly, or getting one incorrect, it's suppose to wipe the contents within the dialog box and print a message "Sorry! Incorrect color" or "Congratulations". It prints the message, but the JTextField can still be seen somewhat behind the message (A left over portion/clipping).

I've tried using the hide() and remove() methods but they didn't seem to work (Or I'm using them incorrectly), I tried re-making a dialog box upon either but I couldn't seem to solve the issue still. What am I doing wrong/how can I make the JTextField disappear upon completion? Thank you in advance for any help!

Here's the portion where if the user enters a color incorrectly or gets them all correct (txtName is the JTextField):

        if(count == 6)//User either finished or entered a color incorrectly
        {                
            //Entered color incorrectly
            if(incorrect == true)
            {                    
                txtName.setEnabled(false); //Doesn't work
                homeScreen.remove(txtName); //Doesn't work
                labelName.setText("Incorrect! Sorry - Wrong color.");
                //txtName.removeActionListener(new MyButtonListener());
            }
            else//Correctly finished the game.
            {
                labelName.setText("Congratulations - your memory skills are perfect!");
                //txtName.removeActionListener(new MyButtonListener());
                homeScreen.remove(txtName);//Doesn't work
            }
        }

Here's my entire program (I can't get it format properly in the post):

package memorygame;

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

public class MemoryGame 
{
    private JFrame homeScreen;
    private JLabel labelName;
    private JTextField txtName;
    private JLabel correct;
    Vector<String> name = new Vector();
    private int count = 1;

    private MyButtonListener listen1 = new MyButtonListener();

    //Constructor - Method to be called when MemoryGame object called
    public void MemoryGame ()
    {
        homeScreen = new JFrame();    
        homeScreen.setSize(400,200);
        homeScreen.setTitle("Memory Game");  
        homeScreen.setDefaultCloseOperation(homeScreen.EXIT_ON_CLOSE);
        homeScreen.setLayout(new FlowLayout());  
        labelName = new JLabel();
        txtName = new JTextField(10);
        createContents();
        homeScreen.setVisible(true);
    }//End Constructor

    //Create components and add them to the window/dialog box
    private void createContents()
    {       
        labelName.setText("Enter the color " + count + ":");
        System.out.println("The current count is: " + count);
        homeScreen.add(labelName); 
        homeScreen.add(txtName);
        txtName.addActionListener(new MyButtonListener());//Allows you to press enter to invoke action
    }
    //Upon user hitting enter
    private class MyButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)//When event occurs
        {
            Scanner in = new Scanner (System.in);//For program input
            String answer = "";
            //Make memColor an array for randomized colors
            /*

            Random r = new Random();
            String[] memColors = new String[5];             
            String[] colors = {"red", "green", "blue", "yellow", "brown", "purple"};

            for(int i =0; i < memColors.length; i++)
            {
                memColors[i] = colors[r.nextInt(6)];
            }
            */
            String memColor1 = "red";
            String memColor2 = "black";
            String memColor3 = "yellow";
            String memColor4 = "green";
            String memColor5 = "blue";
            boolean incorrect = false;

            //If answered incorrectly set count to 5(it'll be 6)
            //And have a boolean for if count== 6 for congrats and failure
            if(e.getSource() == txtName)
            {
                answer = txtName.getText();
                System.out.println(answer);
            }
            else
            {}     
            //Check if user entered Correct color, 1= Red, 2= Black, etc.
            if(count == 1)
            {
                if(answer.equalsIgnoreCase(memColor1))
                {
                    txtName.setText("");                
                }
                else
                {//Needs to be a custom message box
                    count = 5;
                    incorrect = true;
                }                
             }
            else if(count == 2)
            {
                if(answer.equalsIgnoreCase(memColor2))
                {
                    txtName.setText("");       
                }
                else
                {                  
                    count = 5;
                    incorrect = true;
                }                
            }            
            else if(count == 3)
            {
                if(answer.equalsIgnoreCase(memColor3))
                {
                    txtName.setText("");                
                }
                else
                { 
                    count = 5;
                    incorrect = true;
                } 
            }
            else if(count == 4)
            {
                if(answer.equalsIgnoreCase(memColor4))
                {
                    txtName.setText("");                
                }
                else
                {
                    count = 5;
                    incorrect = true;
                }
            }
            else if(count == 5)
            {
                if(answer.equalsIgnoreCase(memColor5))
                {
                    txtName.setText("");

                }
                else
                {
                    count = 5;
                    incorrect = true;
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Something went wrong!");
            }            

            count += 1;
            //User has completed the game or entered a color incorrectly
            if(count == 6)
            {

                if(incorrect == true) //Incorrect color
                {                    
                    txtName.setEnabled(false);
                    homeScreen.remove(txtName);
                    labelName.setText("Incorrect! Sorry - Wrong color.");
                    //txtName.removeActionListener(new MyButtonListener());
                }
                else //Completed the game correctly
                {
                    labelName.setText("Congratulations - your memory skills are perfect!");
                    //txtName.removeActionListener(new MyButtonListener());
                    homeScreen.remove(txtName);
                }
            }
            else
            {
                labelName.setText("Enter the color " + count + ":");
            }              
        }//End Listener        
    }//End Button class

        public static void main(String[] args) {       

        //Show message box
        //Randomize colors
        JOptionPane.showMessageDialog(null, "How good is your memory?\nTry to memorize this color sequence:\n\n red black yellow green blue");

        MemoryGame mem = new MemoryGame();        
        mem.MemoryGame();        
    }//End Main
}// End Class
MadProgrammer

Use txtName.setVisible(false); instead of homeScreen.remove(txtName);

Basically, if you want to call remove, you will need to revalidate and repaint container...

You'll also want to ensure that your UI is create within the context of the Event Dispatching Thread, see Initial Threads for more details

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사