在 Java 上的 JForm 中滑动 JPanel 内容
我有一个问题。我想制作一个摆动表单,当单击按钮时,他会将面板(及其内容)向左滑动,以便右侧的面板以平滑的效果替换它。
我尝试了一段时间如何检查面板的大小,然后将其最小化并显示下一个面板,如下所示:
while (jpanelprincipal1.getWidth() < 439 || jpanelprincipal1.getHeight() > 250)
{
int panel1width = jpanelprincipal1.getWidth();
int panel2height = jpanelprincipal1.getHeight();
jpanelprincipal1.setSize(panel1width -- , panel2height --);
jpanelprincipal2.setSize(440,250);
}
我在 C# 中使用了这个技巧,但使用了 Application.DoEvent(); (显然它在java上不可用)。
无论如何,我可以制作 2 个或更多面板的幻灯片效果吗?
顺便说一句:抱歉我的英语很糟糕!
提前致谢, 路易斯·达·科斯塔
I have a question. I want to make a swing form that, when clicking in a button he slides a panel (with his content) to the left so the panel on the right replaces it with a smooth effect.
I Have tried to do a while how checks the size of the panel and then minimize it and shows the next one like this :
while (jpanelprincipal1.getWidth() < 439 || jpanelprincipal1.getHeight() > 250)
{
int panel1width = jpanelprincipal1.getWidth();
int panel2height = jpanelprincipal1.getHeight();
jpanelprincipal1.setSize(panel1width -- , panel2height --);
jpanelprincipal2.setSize(440,250);
}
I used this trick in C# but with the Application.DoEvent(); (how obviously it's not available on java).
Is there anyway i can make a slide effect of 2 or more panels?
BTW : Sorry for my very bad english !
Thanks In Advance,
Luis Da Costa
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题提到您希望面板“滑动”,但代码看起来像您试图让面板“收缩”,所以它被另一个面板取代。
假设您有两个面板,每个面板的大小相同,那么您可以将其中一个面板“滑出”视图,同时另一个面板滑入视图。
为此,您可以使用带有 GridLayout 的面板。这样每个组件的大小都相同。然后将面板添加到没有任何滚动条的滚动窗格中。滚动窗格的大小需要设置为第一个组件的大小。然后您可以通过更改视口的位置来“滑动”两个面板。因此,在您的计时器中,您将具有类似以下的代码:
当位置大于组件的大小时,您将停止计时器。
You question mentions you want the panel to "slide", but the code looks like you are trying to get the panel to "shrink", so it is replaced by another panel.
Assuming you have two panels each with the same size, then you can "slide" one out of view while the other slides into view.
To do this you an use a panel with a GridLayout. This way each component will be the same size. Then you add the panel to a scrollpane without any scrollbars. The size of the scrollpane will need to be set to the size of the first compnoent. Then you can "slide" the two panels by changing the position of the viewport. So in your Timer you would have code something like:
You would then stop the Timer when the position is greater than the size of the component.
正如 @HFOE 所建议的,
javax.swing.Timer
是动画的不错选择。setDividerLocation()
的方法JSplitPane
可以从ActionListener
调用。请参阅如何使用拆分窗格以获得更多选项。As suggested by @HFOE,
javax.swing.Timer
is a good choice for animation. ThesetDividerLocation()
method ofJSplitPane
can be called from theActionListener
. See How to Use Split Panes for additional options.我可能会用 Swing Timer 来做到这一点。更改计时器的 ActionListener 中表示滑动 JPanel 的 x、y 位置的类字段,然后对容纳 JPanel 的容器调用 repaint。 JLayeredPane 可以很好地用作滑动 JPanel 的容器。
编辑1:关于您对代码的请求,我认为最好的事情是您尝试创建一个非常小的可编译的可运行程序来尝试执行此操作,然后发布您的代码并解释您的程序的行为作为对您的编辑原始帖子。另请向我们发送评论以通知我们您的更改。然后我们可以检查您的代码,测试它,修改它,并帮助您将其塑造成一个工作程序。这称为创建“简短、独立、正确(可编译)示例”或 SSCCE(请检查链接)。
I would probably do this with a Swing Timer. Change a class field representing the x, y position of the sliding JPanel in the timer's ActionListener and then call repaint on the container holding the JPanels. A JLayeredPane could work well as the container for the sliding JPanels.
Edit 1: regarding your request for code, I think the best thing is for you to try to create a very small compilable runnable program that attempts to do this, and then post your code with an explanation of your program's behavior as an edit to your original post. Also send us a comment to notify us of your changes. Then we can inspect your code, test it, modify it, and help you mold it into a working program. This is called creating a "Short, Self Contained, Correct (Compilable), Example" or SSCCE (please check the link).