【发布时间】:2015-08-19 19:44:41
【问题描述】:
我在使用 JFrame 文本字段时遇到问题。
我正在尝试制作文字游戏,但问题是当我尝试创建文本字段以设置输入然后检查它是否是正确的单词时,我已经涵盖了单词 thing。
问题是,当我尝试添加 Textfield 时,它会在我将内容渲染到 JFrame 时消失。
public teksti() {
setTitle("Hirsipuu");
setSize(leveys,korkeus);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
setBackground(Color.white);
jp.add(tf); // adding JtextField (JTextField tf = new JTextField(30);)
add(jp);
}
而我的render() 是这样的(仅用于测试目的)。
private void render() {
BufferStrategy bs = this.getBufferStrategy(); // tehdään uusi bufferi
if (bs == null) {
createBufferStrategy(3);
return;
}
int R = (int) (Math.random( )*256);
int G = (int)(Math.random( )*256);
int B= (int)(Math.random( )*256);
Color randomColor = new Color(R, G, B);
Graphics g = bs.getDrawGraphics();
g.drawString("Arvaa sana", 100 , 100);
g.setColor(Color.white);
g.fillRect(0, 50, leveys, korkeus);
g.setColor(randomColor);
g.setFont(h);
// g.drawLine(0,0,liikey*2-1,liikex);
for(int i = 0; i < salat.size(); i ++) {
g.drawString(salat.get(i),liikex+rand.nextInt(50),liikey+rand.nextInt(50));
}
System.out.println(liikex + " " + liikey);
g.dispose();
bs.show();
g.dispose();
}
我可以让文本字段在开始时显示在顶部,但随后它消失了。
有人知道是否有更好的方法吗?
【问题讨论】:
-
主动渲染 (
BufferStrategy) 和 Swing 的被动渲染算法不兼容。您将需要使用 Swing API 或BufferStrategy,但不能同时使用