Swing滑动抽屉动画

Swing滑动抽屉动画

本文介绍了使用Java Swing滑动抽屉动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Java swing设计和设计接口。我希望抽屉在单击按钮时以滑动动画拉出。首先,是否可以这样做,如果可以,我该怎么做。谢谢。对于某些特定的方法信息,我将不胜感激。

I am new to swing and designing an interface using java swing. I want a drawer to pull out with sliding animation on a button click. Firstly, is it possible to do so and if yes, how do I do it. Thank you. I will appreciate a response in terms of some specific method information.

推荐答案

根据您可能有多种可能的实现方式您想要实现的目标。

There are a number of possible ways you might achieve depending on what you want to achieve.

基本方法是简单地绘制和Swing

The basic method would be simply draw the graphics and a Swing Timer

这将使您可以简单地更新一个变量,该变量将作为抽奖的大小,例如...

This would allow you to simply update a variable which would act as the bases for the size of the draw, for example...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawerPaneTest {

    public static void main(String[] args) {
        new DrawerPaneTest();
    }

    public DrawerPaneTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new DrawerPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class DrawerPane extends JPanel {

        private boolean drawIn = false;
        private Timer timer;
        private int drawWidth = 0;
        private int drawDelta = 4;

        public DrawerPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    timer.stop();
                    drawIn = !drawIn;
                    drawDelta = drawIn ? 4 : -4;
                    timer.start();
                }
            });

            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    drawWidth += drawDelta;
                    if (drawWidth >= getWidth() / 2) {
                        drawWidth = getWidth() / 2;
                        timer.stop();
                    } else if (drawWidth < 0) {
                        drawWidth = 0;
                        timer.stop();
                    }
                    repaint();
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.drawRect(0, 0, drawWidth, getHeight());
            g2d.dispose();
        }
    }

}

这是确实很基本,并且没有考虑到概念变慢/变慢等问题。对于这些,您最好看一下专用的动画框架,例如

This is really basic and doesn't take into account things like slow out/slow in concepts. For those, you'd better look at a dedicated animation framework, like





  • Timing Framework
  • Trident
  • Universal Tween Engine

您可能使用哪种取决于您要实现的目标,我就个人而言,就像Timing Framework,主要是因为它给了我很多自由,例如Trident之类的东西(主要是)旨在提供更改对象属性(例如大小或位置)的能力。虽然Timing Framework可以做到这一点,但要达到这一点还需要做更多的工作。

Which you might use depends on what it is you want to achieve, I personally like the Timing Framework, mostly because it gives me a lot of freedom, where as something like Trident has being (mostly) designed to provide the ability to change properties on a object, such as the size or position, for example. While the Timing Framework can do this do, it would require more work to get it that point.

我没有使用Universal Tween Engine,但已经看到了一些真正的方法。很好的示例,例如,它实际上可以满足您的需求...

I've not used the Universal Tween Engine, but have seen some real nice examples, such as the Sliding-Layout, which might actually meeet your needs...

这篇关于使用Java Swing滑动抽屉动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:02