NetBeans 中 JFrame 中的 JPanel
我在 NetBeans 中创建了一个 Java 应用程序(项目),其中设计了一个带有菜单栏的 JFrame
和不同的 JPanels
。 我希望这些 JPanels
在不同菜单项的操作上出现在 JFrame
内,这样每当单击菜单项时,不同的 JPanels
应该出现在里面JFrame
。 我设计了 JFrame
和 JPanel
分开,但我无法将它们链接在一起。
请朋友们帮帮我。
I have created a Java application (project) in NetBeans, in which I have designed a JFrame
with menu bar, and different JPanels
. I want these JPanels
to appear inside the JFrame
on action of different menu items, so that whenever the menu items are clicked different JPanels
should appear inside the JFrame
. I have designed both JFrame
& JPanel
separately, but I couldn't link them together.
Please help me out friends.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用卡片布局来实现此目的。 卡片布局可以包含许多组件(在您的情况下为 JPanel),并且您可以在它们之间切换。 在 Netbeans 调色板中添加卡片布局很容易。
文档:
http://java.sun。 com/j2se/1.4.2/docs/api/java/awt/CardLayout.html
You could use Card Layout for this. A Card Layout can contain many components (JPanel in your case), and you can switch between them. It's easy to add a card layout in the netbeans palette.
Doc:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/CardLayout.html
从您的评论中可以看出,您希望在单击按钮时动态创建 JPanel。 如果是这种情况,那么 CardLayout 并不理想。 自己实现同样的效果相对容易。 代码将如下所示:
假设更改面板是框架中的唯一组件。 如果不是,则将带有 BorderLayout 的 JPanel 添加到 Matisse 中的内容窗格,然后将新面板添加到其中而不是内容窗格。
It appears from one of your comments that you wish to dynamically create the JPanels when the buttons are clicked. If this is the case then CardLayout is not ideal. It is relatively easy to achieve the same effect yourself. The code will look something like this:
That assumes that the changing panel is the only component in the frame. If it is not then add a JPanel with a BorderLayout to the content pane in Matisse and then add the new panels to that rather than the content pane.
Matisse GUI 构建器对“JTabbedPane”控件有很好的支持(如果您正在寻找的话)。 您可以将 TabbedPane 容器拖到表单中,然后将其他容器拖到其上以创建新选项卡。
如果您正在寻找更高级的行为,例如当用户按下不同的按钮时隐藏/显示不同的容器,您将需要编写一些代码; GUI 构建器无法处理此问题。
The Matisse GUI builder has pretty good support for the "JTabbedPane" control, if that's what you're looking for. You can drag the TabbedPane container into your forms, then drag other containers onto it to create new tabs.
If you're looking for more advanced behavior, such as hiding/showing different containers when the user presses different buttons, you will need to write some code; the GUI builder isn't equipped to handle this.
首先将其保存为
.java
文件。 这是主要的 jframe 类。 你先运行一下。 然后您会看到外部面板通过构造函数添加到其中。面板类:
First you save it as a
.java
file. This is the main jframe class. You run it first. Then you see the external panel add to it by constructor.Panel class:
如果您希望菜单对点击做出反应,则需要将侦听器添加到菜单项,其响应将显示正确的面板。
If you want the menus to react to clicks, you need to add Listeners to the menu items, Which in response will show the correct panel.
更改 JFrame 中的面板后,执行frameObj.pack();
after changing the panel in JFrame do the frameObj.pack();
如果您确实想让您的 GUI 更具吸引力,请使用 JDesktopPane 并查看结果。
If You really want make your GUI more attractive then use JDesktopPane and see the result.