将文本字段添加到Java中的图形 [英] Adding textfield to Graphics in java
问题描述
有谁知道如何将 JTextField
添加到图片名称 bufferstrategy.getDrawGraphics
中?
尝试将它痛苦地刻入图形,如下所示:
private JTextField Input = new JTextField();
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
最终图形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中被调用。任何人都可以帮助我吗?
在 Graphics
不要让它成为现场组件。组件需要被添加到一个有效的容器,该容器在它们生效之前附着到本地对等体。
目前,您唯一要做的就是生成一个橡皮图章/图像上下文表面上的组件图像。
有一些技巧,因为绘画过程期望组件附着到
<$>
首先,您必须准备该字段...
c $ c> 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);
如果你想要一个life组件,你将不得不将组件添加到容器中。这在使用缓冲策略时可能会有问题...
Swing组件已经被双缓冲。
Does anyone know how to add JTextField
into Graphics name bufferstrategy.getDrawGraphics
?
Tryed to pain it into graphics, something like this:
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);
But, eventhough it displayed, I could not edit it. Even the Input.setBounds(800,250, 350,20)
did not work. This method that is written above, is being called inside a gameloop. Can anyone help me?
Painting a component on a Graphics
won't make it a "live" component. Components need to be added to a valid container that is attached to a native peer before they become live.
At the moment, the only thing you are doing is producing a "rubber stamp"/image of the component on the surface of the graphics context.
There are some tricks to this, as the painting process expects that the component is attached to a valid, native peer.
First, you must prepare the field...
Input.setBounds(800,250, 350,20);
Input.setBorder(BorderFactory.createLineBorder(Color.BLACK, 0));
Input.setEditable(true);
Input.setBackground(getBackground());
Input.setForeground(getForeground());
Then you need to paint it. The field will not automatically repaint, again, this has to do with the field not been attached to a native peer...
Input.printAll(gCC);
If you want a life component, you are going to have to add the component to the container. This may be problematic when using a buffer strategy...
Swing components are already double buffered.
这篇关于将文本字段添加到Java中的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!