【问题标题】:Sliding Drawer animation on a JToggle selected state in Java?Java中JToggle选定状态的滑动抽屉动画?
【发布时间】:2019-08-07 13:13:30
【问题描述】:

我正在尝试制作一个程序,其中在选择 JToggleButton 时显示一个滑动抽屉动画面板,在取消选择时隐藏它。

我想要在单击切换按钮时出现这样的内容,

|切换按钮|---->|面板|
像抽屉一样滑动面板

以及取消选择切换按钮

|切换按钮|

我能够创建一个关于选择 JToggle 按钮的新面板,但我对创建动画感到困惑。

myToggleButton.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            if(ev.getStateChange()==ItemEvent.SELECTED){

                myPanel.add(slidingPanel,BorderLayout.CENTER); 
                myPanel.revalidate();
                myPanel.repaint();

            } else if(ev.getStateChange()==ItemEvent.DESELECTED){

                myPanel.remove(slidingPanel);
                myPanel.revalidate();
                myPanel.repaint();
                }
            }
        }); 

如何在单击切换按钮时为面板实现滑动抽屉动画,然后隐藏面板。

【问题讨论】:

  • Swing 不支持布局动画。但是here 你可以找到一个例子(好吧,如果你没有 Swing 方面的专业知识,这对你来说可能会很复杂)。
  • 听起来类似于我曾经实现的github.com/javagl/CommonUI/blob/master/src/main/java/de/javagl/…。如果目的只是移动面板(而不是在动画期间真正调整大小),那么事情可能会更简单...

标签: java swing panel sliding jtogglebutton


【解决方案1】:

我能够弄清楚如何实现它。我正在编写代码以寻求帮助。

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

public class SlidePanelDemo extends JPanel {

    private JPanel pnlMain;
    private JFrame frame;
    private JPanel contentPane;
    private static int drawWidth = 200;
    private static int drawHeight = 100;
    private Timer timer;
    private int graphWidth = 5;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SlidePanelDemo().createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        JToggleButton button = new JToggleButton("Tools");
        button.setSize(30, 30);
        button.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ev) {
                if (ev.getStateChange() == ItemEvent.SELECTED) {
                    System.out.println("button is selected");
                    timer = new Timer(40, (ActionEvent e1) -> {
                        graphWidth = graphWidth + 5;
                        System.out.println("graphWidth is" + graphWidth);
                        pnlMain.setSize(graphWidth, drawHeight);
                        pnlMain.setVisible(true);
                        if (graphWidth >= 0 && graphWidth == drawWidth) {
                            timer.stop();
                        }
                    });
                    timer.setRepeats(true);
                    timer.start();
                } else if (ev.getStateChange() == ItemEvent.DESELECTED) {
                    System.out.println("button is not selected");
                    timer = new Timer(40, (ActionEvent e1) -> {
                        graphWidth = graphWidth - 5;
                        System.out.println("graphWidth is" + graphWidth);
                        if (graphWidth > 0 && graphWidth <= drawWidth) {
                            pnlMain.setSize(graphWidth, drawHeight);
                            pnlMain.setVisible(true);
                        }
                        if (graphWidth == 5) {
                            timer.stop();
                        }
                    });
                    timer.setRepeats(true);
                    timer.start();
                }
            }
        });
        pnlMain = createMainPanel();
        JButton label = new JButton("Click");
        JButton label2 = new JButton("Click me");
        pnlMain.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        pnlMain.add(label);
        pnlMain.add(label2);
        contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        contentPane.setOpaque(true);
        contentPane.add(button);
        contentPane.add(pnlMain);

        pnlMain.setVisible(true);

        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Slide Out Panel Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(contentPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setSize(500, 300);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(graphWidth, drawHeight);
            }
        };
        panel.setBorder(BorderFactory.createTitledBorder("Main"));
        return panel;
    }
}

【讨论】:

    猜你喜欢
    • 2012-12-04
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多