在同一框架中单击按钮后显示一个面板,在单击第二个按钮时显示另一个面板 [英] show one panel after button is clicked and other panel when second button is clicked in the same frame
问题描述
我正在创建一个基于Swing的应用程序,它实际上由两个按钮组成,如下所示-
I am creating a Swing based application , which actually consist of two buttons as shown below -
现在我想要的是,当单击第一个按钮时,它必须执行一个操作,例如在同一帧中显示包含标签,文本字段和一些按钮的面板,如下所示-
now what i want is when first button is clicked it must perform a action like showing a panel containing label, textfields , and some buttons in the same frame as shown below -
当单击第二个按钮时,它将在同一框架中显示另一个面板,如下所示.
and when second button is clicked it will show another panel in the same frame as shown below ..
问题是我不了解如何通过提供事件处理程序和操作侦听器来使此接口实际起作用..所以请让我知道如何归档此接口.任何帮助,将不胜感激..谢谢
the thing is i am not understanding how to make this interface in action by providing event handler and action listener .. So please letme me know how can i archive this . Any help would be appreciated .. Thanks
推荐答案
有2种方法.
-
CardLayout
为基础.
创建所有面板(空面板,带有字段的面板,带有列表的面板),然后将其添加到具有CardLayout
的容器中(默认为空).在单击按钮上,交换显示必要卡片的可见卡片(面板).
CardLayout
based.
Create all the panels (empty, panel with fields, panel with list) and add them in a container withCardLayout
(the empty one is default). On click buttons swap visible cards (panels) showing necessary one.
基于娱乐.
在单击按钮上,创建一个包含新内容的新面板.从容器中删除旧的窗格,然后添加新创建的窗格.之后,请致电:
Recreation based.
On click button create a new panel with new content. Remove the old one from container and add the newly created pane. After then call:
container.revalidate();
container.repaint();
这篇关于在同一框架中单击按钮后显示一个面板,在单击第二个按钮时显示另一个面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!