Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

swing - Slide JPanel Content in a JForm on Java

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...