Adding textfield to Graphics in java

强颜欢笑 提交于 2019-12-05 03:50:54

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!