Swing組件動態滾動效果 – JAVA編程語言程序開發技術文章

不知道什麼時候..網頁上流行瞭那種 旋轉木馬效果.
目前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 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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                frame.setVisible(true);
            }
        });
    }
}

發佈留言