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.

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

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值