有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JFrame中标题栏中的java字幕

如果使用Marquee标记,我如何使JFrame的标题栏像HTML中的Marquee一样成为Marquee


共 (3) 个答案

  1. # 1 楼答案

    上帝原谅我的以下代码

    如果希望字幕在加载后直接启动,请将此代码放入框架构造函数中:

        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 = 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();
    

    此代码仅供学习之用,请勿在实际产品中使用

  2. # 2 楼答案

    请不要

    如果您确实决定这样做,那么显而易见的技巧是使用所需文本的子序列设置标题。我猜Unicode中的各种部分大小的空格可以让你稍微平滑一些(或者看起来像正方形)

    或者,您可以使窗口PL&;F装饰(不会在Sun/Oracle实现中使用本机PL&;F)并自己绘制文本。要看起来不错,您需要将其调整到特定的PL&;F和配置

  3. # 3 楼答案

    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);
    }
    

    试试这个,傻瓜式的。而且更容易理解