[英]How to add JTextField in time running in Java?
In order to develop a edit text in java for study. 为了在Java中开发一个编辑文本进行研究。 I'm with a problem: My program once opened by the user, if the user to click in button "Search", then the
ActionListener
will add a field in Jpanel. 我有一个问题:我的程序由用户打开,如果用户单击按钮“搜索”,则
ActionListener
将在Jpanel中添加一个字段。
For example: I have a class JToolBar that sets up jtoolbar menu that extends JPanel. 例如:我有一个类JToolBar,它设置了扩展JPanel的jtoolbar菜单。 Then, I add it in
JFrame
. 然后,将其添加到
JFrame
。 Within that JToolbar there's a button "Search", if the user to click at this button, a JTextField
will appears side this menu instantly. 在该JToolbar中,有一个“搜索”按钮,如果用户单击此按钮,则
JTextField
将立即显示在此菜单旁边。
I try to create a class private within that JToolBar
class. 我尝试在该
JToolBar
类中创建一个私有类。 So, I just add the JTextField to JPanel
contains the JToolbar
. 因此,我只是将JTextField添加到包含
JToolbar
JPanel
中。 However, is not working. 但是,不起作用。 There's no error.
没有错 Simply not appears the
JTextField
. 根本不会出现
JTextField
。 What I do to solve that problem ? 我要如何解决这个问题?
When you add a component to a visible GUI the general code is: 将组件添加到可见的GUI时,通用代码为:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
You need the revalidate() to tell the layout manager that a component has been added. 您需要revalidate()来告知布局管理器已添加组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.