2013-05-20 35 views
8

有誰知道如何將JTextField添加到圖形名稱bufferstrategy.getDrawGraphics? Tryed痛成圖形,這樣的事情:將文本字段添加到java中的圖形

private JTextField Input = new JTextField(); 
BufferStrategy bs = getBufferStrategy(); 

if (bs == null) { 
    createBufferStrategy(3); 
    return; 
} 

final Graphics gCommands = bs.getDrawGraphics(); 
Graphics gCC = bs.getDrawGraphics(); 
Input.requestFocus(); 
Input.paint(gCC); 
Input.setBounds(800,250, 350,20); 
Input.setBorder(BorderFactory.createLineBorder(Color.BLACK, 0)); 
Input.setEditable(true); 
Input.setBackground(getBackground()); 
Input.setForeground(getForeground()); 
Input.addKeyListener(key); 

但是,儘管它已經顯示出來,我不能編輯它。即使是Input.setBounds(800,250, 350,20)也不起作用。上面寫的這個方法,在一個gameloop中被調用。誰能幫我?

+0

出了什麼問題?你有運行時錯誤嗎?顯示了嗎? – Frecklefoot

+0

雖然「JTextField」不可編輯,但它顯示 – null

+0

該類是Canvas嗎? JFrame的? – Tips48

回答

11

Graphics上繪製組件不會使其成爲「實時」組件。組件需要添加到連接到本地對等體的有效容器,然後才能生效。

此刻,您正在做的唯一一件事是在圖形上下文的表面上生成組件的「橡皮圖章」/圖像。

有一些技巧,因爲繪畫過程期望組件附加到有效的本地對等體。

首先,你必須準備場...

Input.setBounds(800,250, 350,20); 
Input.setBorder(BorderFactory.createLineBorder(Color.BLACK, 0)); 
Input.setEditable(true); 
Input.setBackground(getBackground()); 
Input.setForeground(getForeground()); 

然後,你需要繪製它。該字段不會自動重新繪製,再次,這與沒有被連接到本機對現場做...

Input.printAll(gCC); 

如果你想要的生活組成部分,你將不得不將組件添加到集裝箱。當使用緩衝策略時,這可能是有問題的......

Swing組件已經被雙緩衝。

相關問題