Java Swing 设计 [英] Java Swing design

查看:27
本文介绍了Java Swing 设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Swing UI 编码的问题.如果我想制作带有某些选项的软件,例如在第一帧我有三个按钮(新建、选项、退出).

I have a question regarding coding a Swing UI. If I want to make a software with some option e.g. on the first Frame I have three buttons (New, Option, Exit).

现在,如果用户单击新按钮,我想将 Frame 中的整个内容更改为其他内容.我知道我必须对那个按钮使用 addActionListener.但我的问题是如何更改框架中的内容.创建新框架然后使用 setVisible() 对我来说不是一个选择.

Now if a user clicks the new button, I want to change the entire content in the Frame to something else. I know I have to use addActionListener to that button. But my question is how to change the contents in the frame. Creating new Frames and then use setVisible() isn't an option for me.

并且使用 frame.remove() 如果有几个需要删除的东西,所有的对象看起来都很尴尬.还是方法正确?

And to use frame.remove() all the objects seems awkward if it is several things that needs to be removed. Or is it the right way?

推荐答案

CardLayout 在这种情况下确实是更好的选择;但是,当场合需要时,组件 可以从 Container 使用remove()removeAll() 方法.之后,必不可少的步骤是调用 [validate()](http://java.sun.com/javase/6/docs/api/java/awt/Container.html#validate()) 方法再次布置容器的子组件.Oscar Reyes 的示例使用 Frame's [pack()](http://java.sun.com/javase/6/docs/api/java/awt/Window.html#pack()) 方法,继承自 Window,实现这个效果.在这个示例中,resetGame() 方法重构了以类似的方式显示面板.

CardLayout is indeed the better choice in this case; but, when the occasion demands, a Component may be removed from a Container using either the remove() or removeAll() method. Afterward, the essential step is to invoke the [validate()](http://java.sun.com/javase/6/docs/api/java/awt/Container.html#validate()) method to lay out the container's subcomponents again. Oscar Reyes' example uses the Frame's [pack()](http://java.sun.com/javase/6/docs/api/java/awt/Window.html#pack()) method, inherited from Window, to achieve this effect. In this example, the resetGame() method reconstructs the display panel in a similar way.

这篇关于Java Swing 设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆