所以,我爲我的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更改方向。
爲什麼不彈出它在一個JOptionPane或模態JDialog? –
我從來沒有辛苦那些。那些單獨的彈出窗口? – OSXpro