简体   繁体   中英

How do you add a text field to a jpanel that is drawn onto a jframe?

So, I am creating an rpg game for my AP computer science class. One of the levels I am making for it is a level where the player is stuck in an infinite loop and he is continuously moved back to the center of the screen. To solve this the player presses enter to open up a text box to either type, "break;" or "count++". However, when i press enter, the window's size is changed completely and the things drawn on the jpanel are gone.

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class InfiniteLoop extends JPanel implements KeyListener{
    public Sprite character;
    Set<Character> keys = new HashSet<Character>();
    ArrayList<Image> sprite = new ArrayList<Image>();
    static JFrame frame;
    int frames;

    public InfiniteLoop(){
        super();
        frames = 0;
        addKeyListener(this);
        try {
            sprite.add(ImageIO.read(new File("src/Sprite/back.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/back walk 1.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/back walk 2.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/left idle.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/left walk.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/left walk 2.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/Idle.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/front walk 1.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/front walk 2.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/right idle.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/right walk.png")));
            sprite.add(ImageIO.read(new File("src/Sprite/right walk 2.png")));
            character = new Sprite(400,250, 0.25, sprite);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public Dimension getPreferredSize() {
        return new Dimension(800, 500);
    }

    public static void main(String[] args){
        InfiniteLoop panel = new InfiniteLoop();
        frame = new JFrame("APCS the game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.addKeyListener(panel);
        frame.setSize(800, 500);
        frame.pack();
        frame.setBackground(Color.white);
        frame.setVisible(true);

    }

    public void paintComponent(Graphics g) {    
        frames ++;
        if(frames > 500){
            character.move(400, 250);
            frames = 0;
        }
        int width = getWidth();
        int height = getHeight();
        g.clearRect(0, 0, width, height);
        g.setColor(Color.black);
        g.setFont(new Font("Comic Sans", Font.PLAIN, 32)); 
        g.drawString("while(count == 1){", 150, 100);
        g.drawString("}", 600, 400);
        character.draw(g);
        character.move(keys, width, height);
        repaint();
      }

    public synchronized void keyPressed(KeyEvent event) {
        //Adds pressed key to set
        keys.add(event.getKeyChar());
        if(event.getKeyCode() == event.VK_ENTER){
            frame.getContentPane().add(new JTextField("Stop this infinite loop"));
            frame.pack();
        }

    }

    public synchronized void keyReleased(KeyEvent event){
        //Method to remove key from Set if released
        System.out.println(event.getKeyChar());
        if(keys.contains(event.getKeyChar()))
            keys.remove(event.getKeyChar());
    }

    //Empty Implemented method that has to be here for use of listener
    public void keyTyped(KeyEvent event) {
    }
}

Can someone please tell me how to properly add the text field on the jpanel because I could only find people who added it directly on the Jframe?

*Note: Sprite class is a simple class I made to handle character movement, walking, and changing direction through an ArrayList.

您可以做一件事,将JTextField添加到JPanel并将其初始设置为Visible false,然后当您想从用户输入时将其显示给用户。

-You need to define the JPanel layout in order to correctly place a component in it. Simply add in your main method:

panel.setLayout(new BorderLayout());

-Then change the addition of the JTextField as follows:

frame.getContentPane().add(new JTextField("Stop this infinite loop"), BorderLayout.North);

Also, I strongly recommend you read about layouts: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM