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