我怎样才能让一个JFrame的标题栏是像跑马灯在HTML选取框如果您使用字幕标记?

有帮助吗?

解决方案

上帝原谅我下面的代码

如果您希望字幕开始将这个代码在你的框架构造直接装车后:

    int delay = 3000;
    int period = 50;
    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
        int spaces=0;
        public void run() {
            String title="";
            for (int j = 0; j < spaces; j++) {
                title+= " " ;
            }
            title+= "Annoying";
            Main.this.setTitle(title);
            spaces=(spaces+1)%50;
        }
    }, delay, period);

<强>更新结果 按照评价,这里是使用swing.Timer另一个版本

    Timer timer = new Timer(delay,new ActionListener(){

        int spaces=0;

        public void actionPerformed(ActionEvent e) {
            String title="";
            for (int j = 0; j < spaces; j++) {
                title+= " " ;
            }
            title+= "Annoying";
            Main.this.setTitle(title);
            spaces=(spaces+1)%50;

        }}
    );
    timer.start();

此代码是学习的目的而已,请不要在真正的产品中使用它。

其他提示

请不要。

如果你决定,那么明显的技术是使用的setTitle所需的文本序列。我想以Unicode各个部分尺寸的空间可以让你做出了略微更流畅(否则可能会显示为方块)。

此外,也可以使窗口PL&F饰(与本地PL&F在Sun / Oracle实施不会工作),并绘制自己的文字。为了好看,你需要将其调整到特定的PL&F和配置。

int delay = 0;
int period = 500;
    t.scheduleAtFixedRate(new TimerTask(){
        String test = "  Test marquee";
        int i = 0;
        public void run(){
            titleChanger(test.substring(i, test.length()));
            i++;
            if(test.length()==i){
                i = 0;
            }
        }
    }, delay, period);


Timer t = new Timer();
public void titleChanger(String t){
this.setTitle(t);
}

尝试此,傻瓜证明。和更容易理解。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top