Slide JPanel Content in a JForm on Java

拥有回忆 提交于 2019-11-27 05:13:40
camickr

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.

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();
            }
        });
    }
}

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!