如何将文本字段添加到绘制到jframe上的jpanel?

时间:2016-05-13 02:45:33

标签: java swing jframe jpanel textfield

所以,我正在为我的AP计算机科学课创建一个RPG游戏。我正在制作的其中一个级别是玩家陷入无限循环并且他不断移回屏幕中心的等级。为了解决这个问题,玩家按下回车键打开一个文本框,键入," break;"或者"计算++"。但是,当我按下回车键时,窗口的大小会完全改变,而jpanel上绘制的内容也会消失。

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) {
    }
}

有人可以告诉我如何在jpanel上正确添加文本字段,因为我只能找到直接在Jframe上添加文本字段的人吗?

*注意:Sprite类是我用来处理角色移动,行走和通过ArrayList改变方向的简单类。

2 个答案:

答案 0 :(得分:0)

您可以执行一项操作,将JTextField添加到JPanel并初始设置为Visible false,当您想要从用户输入时,请将其显示给用户。

答案 1 :(得分:0)

- 您需要定义JPanel布局才能正确放置组件。只需添加您的主要方法:

panel.setLayout(new BorderLayout());

- 然后按如下所示更改JTextField的添加:

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

此外,我强烈建议您阅读有关布局的内容: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html