在编辑一个点名系统同,想在窗口里把所有人的名字循环地上下滚动,在一个时刻显示3个名字上下滚动,并且从上滚到中间的时候逐渐变大,从中间滚到下面的时候逐渐变小,哪位高手可以帮一下我?

解决方案 »

  1.   

    P.S.不要jsp和JavaScript的,要纯java application
      

  2.   

    以前做过个类似的,就是Label做的,
    设置多个Label,用timer计时,然后移动位置
    你的,估计再加上改变字体大小就行了不过,这种方法很耗资源
    希望你能找出更好的办法
      

  3.   

    另外可以试试看JEditorPane可不可以使用html的marquee
      

  4.   

    用timer定时。绘画自己写代码搞定。
      

  5.   

    楼上的几个都说了个 大概,用一个Timer,还有Font,然后定时改变Font中的大小,用g.drawString(name,x,y)绘制名字。
    这样应该就可以搞定了
      

  6.   

    绘制名字的时候 可以选择Label作为名字的载体也可直接 在paint方法里draw出来 如果是直接画出来 那么你可以根据timer运动label或panel 获得位置后repaint   全局参数scale 调用g.scale(double scale)放大或缩小当前图形 scale==1 正常大小 
      

  7.   

    主要都是Graphics2D和Timer的运用,楼主自己查查这些类的方法,不要只想着给代码,自己解决学到的是自己的,印象也深刻些
      

  8.   

    我之前写过一个相似的Demo,你可以参考。
    http://blog.csdn.net/chenweionline/archive/2008/08/18/2790322.aspx
      

  9.   

    还是看我的吧:////////////////////////ScrolledString.java
    import java.awt.*;public class ScrolledString {

    /** 字符串默认字体 */
    public static final Font DEFAULT_FONT = new Font("simsun", Font.BOLD, 12);

    /** 字符串默认颜色 */
    public static final Color DEFAUTE_COLOR = Color.black;

    /** 字符串字体 */
    private Font font;

    /** 字符串颜色 */
    private Color color;

    /** 字符串 */
    private String info;

    /** 显示的水平位置 */
    private int x; /** 显示的垂直位置 */
    private int y; public ScrolledString(String s, int x, int y) {
    this(s, x, y, null, null);
    } public ScrolledString(String s, int x, int y, Font f, Color c) {
    this.info = s;
    this.x = x;
    this.y = y;
    setFont(f);
    setColor(c);
    } public Point getLocation() {
    return new Point(x, y);
    }

    public void setLocation(Point p) {
    this.x = p.x;
    this.y = p.y;
    }

    public String getString() {
    return info;
    } public void setColor(Color c) {
    this.color = (c == null) ? DEFAUTE_COLOR : c;
    } public Font getFont() {
    return this.font;
    } public void setFont(Font f) {
    this.font = (f == null) ? DEFAULT_FONT : f;
    } /**
     * 绘制此{@code ScrolledString}到指定图形上下文。
     *  
     *  @param g 图形上下文
     */
    public void draw(Graphics g) {
    Color oldColor = g.getColor();
    Font oldFont = g.getFont();
    g.setColor(color);
    g.setFont(font);
    g.drawString(info, x, y);
    g.setColor(oldColor);
    g.setFont(oldFont);
    }}////////////////////////ScrolledStringList.java
    import java.awt.*;
    import java.util.ArrayList;public class ScrolledStringList {
    //字符串列表
    private ArrayList<ScrolledString> stringList = new ArrayList<ScrolledString>();
    //显示范围
    private Rectangle rectangle;
    //卷动速率,单位:像素/帧
    private int speed;
    //字符串之间的间隔
    private int gap;
    //字体大小增量
    private int fontSizeInc; public ScrolledStringList(Rectangle r) {
    this(r, 10, 2, 100);
    }

    public ScrolledStringList(Rectangle r, int speed, int fontSizeInc, int gap) {
    this.rectangle = r;
    this.speed = speed;
    this.fontSizeInc = fontSizeInc;
    this.gap = gap;
    }

    public void add(String[] ss) {
    for (String s : ss) {
    addString(s);
    }
    }

    public void addString(String s) {
    synchronized (stringList) {
    ScrolledString ss = null;
    if (!stringList.isEmpty()) {
    ScrolledString topString = getTopString();
    int topSy = topString.getLocation().y;
    ss = new ScrolledString(s, 0, topSy - gap);

    } else {
    ss = new ScrolledString(s, 0, 0);
    }
    stringList.add(ss);
    }
    } private ScrolledString getTopString() {
    ScrolledString top = stringList.get(0);
    for (ScrolledString ss : stringList) {
    if (ss.getLocation().y < top.getLocation().y) {
    top = ss;
    }
    }
    return top;
    }

    public void draw(Graphics g) {
    synchronized (stringList) {
    for (ScrolledString ss : stringList) {
    Point p = ss.getLocation();
    Font f = ss.getFont();
    p.y += speed;
    if (p.y > rectangle.height) {
    ss.setFont(ScrolledString.DEFAULT_FONT);
    ScrolledString topString = getTopString();
    int topSy = topString.getLocation().y;
    p.y = (topSy <= 0) ? (topSy - gap) : rectangle.y;
    }
    if (rectangle.contains(p)) {
    float ds = p.y < rectangle.height/2 ? fontSizeInc : -fontSizeInc;
    ss.setFont(f.deriveFont(f.getSize() + ds));
    }
    ss.setLocation(p);
    }

    for (ScrolledString ss : stringList) {
    if (rectangle.contains(ss.getLocation())) {
    ss.draw(g);
    }
    }
    }
    }
    }////////////////////////Painter.java
    import java.awt.Graphics;
    import java.awt.Rectangle;import javax.swing.JFrame;
    import javax.swing.JPanel;public class Painter extends JPanel implements Runnable { private static final long serialVersionUID = -2055656611173452692L;
    // 刷新速率,单位:帧/秒
    private static final int FPS = 5;

    private ScrolledStringList scrolledStringList;

    public Painter(ScrolledStringList ssl) {
    this.scrolledStringList = ssl;
    } public void paintComponent(Graphics g) {
    super.paintComponent(g);
    scrolledStringList.draw(g);
    } public void run() {
    int delay = 1000 / FPS;
    while (true) {
    repaint();
    try {
    //注意此处并没精确控制刷新率
    Thread.sleep(delay);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args) {
    JFrame jf = new JFrame();
    jf.setSize(300, 600);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setResizable(false);

    ScrolledStringList ssl = new ScrolledStringList(new Rectangle(0, 0, 300, 600));
    //姓名可以添加任意多个
    String[] names = {"姓名1", "姓名2", "姓名3", "姓名4", "姓名5", "姓名6"};
    ssl.add(names);

    Painter painter = new Painter(ssl);
    jf.getContentPane().add(painter);
    jf.setVisible(true);

    new Thread(painter).start();
    }
    }