I don't know when. The carousel effect is popular on the webpage.
At present, swing has not seen any related performance components... today I wrote ..
Dynamically scroll the component to a certain direction ..
In fact, it is not difficult to analyze ..
Two jbuttons, one JList, and one JScrollPanel. You can do it in the next animation.
Because I used my own extension package... so in some places, you can change it... for example, change IButton to JButton ..
Currently, only scroll left .. I have left you with a small question .. I hope you can do everything you can to scroll right... if yes. you can contact me for your name on the blog. update code
Package swing;
Import com. xx. xswing. base. DataBox;
Import com. xx. xswing. base. Element;
Import com. xx. xswing. base. node. Node;
Import com. xx. xswing. ui. button. IButton;
Import com. xx. xswing. ui. list. IList;
Import com. xx. xswing. xutil. XUtil;
Import java. awt. BorderLayout;
Import java. awt. Dimension;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import javax. swing. JFrame;
Import javax. swing. JList;
Import javax. swing. JPanel;
Import javax. swing. JScrollPane;
Import javax. swing. ScrollPaneConstants;
Import javax. swing. SwingUtilities;
Import javax. swing. Timer;
/**
*
* @ Author chensiyu04
* @ Createdate 2011/8/20
*/
Public class AutoComponent extends JPanel implements ActionListener {
Private static final int LIST_CELL_WIDTH = 70;
Private DataBox dataBox;
Private JScrollPane sp;
Private int index = 0;
Private int currentResolution = 50;
Private long cycleStart;
Private Timer timer = null;
Private final int MOVE_TIME = 2000;
Private int movemloud;
Private int moveMaxX;
Private int moveX;
Private ActionListener moveActionListener = new ActionListener (){
@ Override
Public void actionreceivmed (ActionEvent e ){
Movemedia = index * LIST_CELL_WIDTH;
MoveMaxX = movemwidth + LIST_CELL_WIDTH;
StartTimer (currentResolution );
}
};
Public AutoComponent (){
Init ();
}
Private void init (){
SetLayout (new BorderLayout (0, 0 ));
IButton leftButton = new IButton ("<");
LeftButton. addActionListener (moveActionListener );
LeftButton. setPreferredSize (new Dimension (70, 70 ));
Add (leftButton, BorderLayout. WEST );
IButton rightButton = new IButton ("> ");
RightButton. setPreferredSize (new Dimension (70, 70 ));
Add (rightButton, BorderLayout. EAST );
InitDataBox ();
IList list = new IList (dataBox );
List. setFixedCellHeight (65 );
List. setFixedCellWidth (LIST_CELL_WIDTH );
List. setLayoutOrientation (JList. HORIZONTAL_WRAP );
List. setVisibleRowCount (1 );
Sp = new JScrollPane (list, ScrollPaneConstants. VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants. HORIZONTAL_SCROLLBAR_NEVER );
Add (sp, BorderLayout. CENTER );
}
Private void initDataBox (){
DataBox = new DataBox ();
For (int I = 0; I <10; I ++ ){
Element element = new Node ("Node _" + I );
DataBox. addElement (element );
}
}
Public void animate (float fraction ){
Float animationFactor = (float) Math. sin (fraction * (float) Math. PI/2 );
AnimationFactor = Math. min (animationFactor, 1.0f );
AnimationFactor = Math. max (animationFactor, 0.0f );
MoveX = movemion+ (int) (. 5f + animationFactor * (float) (moveMaxX-movemion ));
If (moveX> = moveMaxX ){
MoveX = moveMaxX;
Timer. stop ();
CycleStart = 0;
Index ++;
}
Sp. getHorizontalScrollBar (). setValue (moveX );
}
Private void startTimer (int resolution ){
If (timer = null ){
Timer = new Timer (resolution, this );
}
If (! Timer. isRunning ()){
Timer. start ();
}
}
@ Override
Public void actionreceivmed (ActionEvent AE ){
Long currentTime = System. nanoTime ()/1000000;
Long totalTime = currentTime-cycleStart;
If (totalTime> MOVE_TIME ){
CycleStart = currentTime;
}
Float fraction = (float) totalTime/MOVE_TIME;
Fraction = Math. min (1.0f, fraction );
Fraction = 1-Math. abs (1-(2 * fraction ));
Animate (fraction );
}
Public static void main (String [] args ){
Final JFrame frame = new JFrame ();
Frame. setLayout (new BorderLayout ());
Frame. setSize (400, 70 );
Frame. getContentPane (). add (new AutoComponent ());
Frame. setUndecorated (true );
XUtil. setComponentCenter (frame );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
SwingUtilities. invokeLater (new Runnable (){
@ Override
Public void run (){
Frame. setVisible (true );
}
});
}
}