Slide JPanel Content in a JForm on Java

前端 未结 3 1940
深忆病人
深忆病人 2020-11-30 12:22

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

3条回答
  •  情话喂你
    2020-11-30 12:48

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

提交回复
热议问题