Search code examples
javaswingjframejtextfield

JTextField not appearing in JFrame


I have created a game that spawns a image (JLabel) at a random location on the screen. When clicked, the image spawns in a different random location. I now plan to also have text on the screen. I am doing this using a JTextField. The problem is that the JTextField msg is not appearing despite it being added using the same method as the JLabel box. Can someone explain why it is not spawning in the JFrame?

BoxGame:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class BoxGame02 extends JFrame {

    static JLabel box       = new JLabel();
    static JTextField msg   = new JTextField();

    static int min          = 2;
    static int max          = 350;

    static Random random    = new Random();
    static int rand1        = min + random.nextInt(max - min + 1);
    static int rand2        = min + random.nextInt(max - min + 1);
    static int randMessage  = 1   + random.nextInt(10  -  1  + 1);

    public BoxGame02() {
        super("Click the Box!");
        setLayout(null);

        ImageIcon icon = new ImageIcon("C:/Users/btayl/JavaProjects/Java Game Development/Box Game/BoxGame02/Images/face.png");
        box.setIcon(icon);
        box.setSize(50,50);

        box.setLocation(rand1, rand2);
        add(box);

        msg.setText("Text on Screen");
        msg.setLocation(10, 200);
        add(msg);

        box.setName("box");
        BoxListener clickBox = new BoxListener();

        box.addMouseListener(clickBox);

    }

    class BoxListener extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            JLabel l = (JLabel) e.getSource();

            if(l.getName().equals("box"))
                moveBox();
        }
    }
    public void moveBox() {
        System.out.println("Testing!");

        rand1            = min + random.nextInt(max - min + 1);
        rand2            = min + random.nextInt(max - min + 1);
        randMessage      = 1   + random.nextInt(10  -  1  + 1); 

        box.setLocation(rand1, rand2);
        add(box);

        revalidate();
        repaint();
    }
}

Window:

public class Window {
    public static void main(String[] args) {

        BoxGame02 frame = new BoxGame02();
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }
}

Solution

  • the JTextField msg is not appearing despite it being added using the same method as the JLabel box.

    No you did not use the same method as you did with the label. Look at your code again closely.

    What methods did you use on the JLabel?

    Now compare the methods used for the JTextField to see what the difference is.

    In your "moveBox()" method you don't need to keep adding the box to the panel. You only add the box once. Then you can simple change its location.

    You should not be using static variables. The variables should be instance variables for the class.