在 Java 上的 JForm 中滑动 JPanel 内容

发布于 2024-10-18 08:09:37 字数 616 浏览 4 评论 0原文

我有一个问题。我想制作一个摆动表单,当单击按钮时,他会将面板(及其内容)向左滑动,以便右侧的面板以平滑的效果替换它。

我尝试了一段时间如何检查面板的大小,然后将其最小化并显示下一个面板,如下所示:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱*していゐ 2024-10-25 08:09:37

他将一个面板(包含他的内容)向左滑动,以便右侧的面板以平滑效果取代它

您的问题提到您希望面板“滑动”,但代码看起来像您试图让面板“收缩”,所以它被另一个面板取代。

假设您有两个面板,每个面板的大小相同,那么您可以将其中一个面板“滑出”视图,同时另一个面板滑入视图。

为此,您可以使用带有 GridLayout 的面板。这样每个组件的大小都相同。然后将面板添加到没有任何滚动条的滚动窗格中。滚动窗格的大小需要设置为第一个组件的大小。然后您可以通过更改视口的位置来“滑动”两个面板。因此,在您的计时器中,您将具有类似以下的代码:

JViewport viewport = scrollPane.getViewport();
Point position = viewport.getViewPosition();
position.x += 5;
viewport.setViewPosition( position );

当位置大于组件的大小时,您将停止计时器。

he slides a panel (with his content) to the left so the panel on the right replaces it with a smooth effect

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:

JViewport viewport = scrollPane.getViewport();
Point position = viewport.getViewPosition();
position.x += 5;
viewport.setViewPosition( position );

You would then stop the Timer when the position is greater than the size of the component.

南风起 2024-10-25 08:09:37

正如 @HFOE 所建议的,javax.swing.Timer 是动画的不错选择。 setDividerLocation() 的方法JSplitPane 可以从 ActionListener 调用。请参阅如何使用拆分窗格以获得更多选项。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5069152 */
public class SplitPaneTest {

    double ratio = 0.5;
    double delta = ratio / 10;
    private void create() {
        JFrame f = new JFrame("JSplitPane");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MyPanel p1 = new MyPanel(Color.red);
        MyPanel p2 = new MyPanel(Color.blue);
        final JSplitPane jsp = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, p1, p2);
        Timer timer = new Timer(200, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ratio += delta;
                if (ratio >= 1.0) {
                    ratio = 1.0;
                    delta = -delta;
                } else if (ratio <= 0) {
                    delta = -delta;
                    ratio = 0;
                }
                jsp.setDividerLocation(ratio);
            }
        });

        f.add(jsp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        timer.start();
    }


    private static class MyPanel extends JPanel {

        Color color;

        public MyPanel(Color color) {
            this.color = color;
            this.setPreferredSize(new Dimension(300, 300));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.drawLine(0, 0, getWidth(), getHeight());
            g.drawLine(getWidth(), 0, 0, getHeight());
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SplitPaneTest().create();
            }
        });
    }
}

As suggested by @HFOE, javax.swing.Timer is a good choice for animation. The setDividerLocation() method of JSplitPane can be called from the ActionListener. See How to Use Split Panes for additional options.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5069152 */
public class SplitPaneTest {

    double ratio = 0.5;
    double delta = ratio / 10;
    private void create() {
        JFrame f = new JFrame("JSplitPane");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MyPanel p1 = new MyPanel(Color.red);
        MyPanel p2 = new MyPanel(Color.blue);
        final JSplitPane jsp = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, p1, p2);
        Timer timer = new Timer(200, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ratio += delta;
                if (ratio >= 1.0) {
                    ratio = 1.0;
                    delta = -delta;
                } else if (ratio <= 0) {
                    delta = -delta;
                    ratio = 0;
                }
                jsp.setDividerLocation(ratio);
            }
        });

        f.add(jsp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        timer.start();
    }


    private static class MyPanel extends JPanel {

        Color color;

        public MyPanel(Color color) {
            this.color = color;
            this.setPreferredSize(new Dimension(300, 300));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.drawLine(0, 0, getWidth(), getHeight());
            g.drawLine(getWidth(), 0, 0, getHeight());
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SplitPaneTest().create();
            }
        });
    }
}
醉生梦死 2024-10-25 08:09:37

我可能会用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文