Swing组件动态滚动效果 (一)

2014-11-24 02:22:12 · 作者: · 浏览: 0

不知道什么时候..网页上流行了那种 旋转木马效果.
目前swing上还未看到任何相关的效果组件...今天就写了一个..

\


动态将组件滚动向某方向滚动..

其实不难..分析一下..
2个JButton 一个JList 一个JScrollPanel. 在来个动画效果 搞定.
由于用到了自己的扩展包..所以有些地方.大家改一下就可以了....比如 IButton 改成JButton..
目前只做了向左滚动..给大家留了个小题目..希望大家想尽一切办法 去实现向右滚动...如果实现.可以联系我 将在博客以你名义.更新代码

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 moveMinX;
private int moveMaxX;
private int moveX;
private ActionListener moveActionListener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
moveMinX = index * LIST_CELL_WIDTH;
moveMaxX = moveMinX + 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 = moveMinX + (int) (.5f + animationFactor * (float) (moveMaxX - moveMinX));
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 actionPerformed(ActionEvent ae) {
long c