2016-05-13 69 views
0

所以,我爲我的AP計算機科學課程創建了一個RPG遊戲。我爲之制定的一個關卡是玩家被無限循環卡住的水平,他會不斷移回屏幕中心。爲了解決這個問題,玩家按下Enter打開一個文本框,以便輸入「break」。或「計數++」。但是,當我按下回車鍵時,窗口的大小會完全改變,jpanel上繪製的內容將消失。如何將文本字段添加到繪製到jframe上的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更改方向。

+0

爲什麼不彈出它在一個JOptionPane或模態JDialog? –

+0

我從來沒有辛苦那些。那些單獨的彈出窗口? – OSXpro

回答

0

你可以做一件事情,將JTextField添加到JPanel中,並初始設置爲可見,並且當你想從用戶輸入時,使其對用戶可見。

+0

謝謝你,這是正確的想法,但是當我嘗試在手邊添加它時,我無法移動字符,因爲不可見的文本字段正在輸入,所以我甚至無法使用輸入鍵使其可見) 。 – OSXpro

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