簡體   English   中英

JFrame標題欄中的字幕

[英]marquee in title bar in JFrame

如果使用marquee標簽,如何使JFrame的標題欄像HTML中的選取框一樣成為選取框?

請不要

如果您決定這樣做,那么顯而易見的技術是設置帶有所需文本子序列的setTitle。 我猜想Unicode中的各種局部大小的空格可能會讓您稍微平滑一些(或者它們可能顯示為正方形)。

或者,您可以裝飾PL&F窗口(不能與Sun / Oracle實現中的本機PL&F一起使用)並自己繪制文本。 為了看起來不錯,您需要將其調整為特定的PL&F和配置。

上帝原諒我以下代碼

如果您希望選取框在加載后直接啟動,請將以下代碼放入框架構造函數中:

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

該代碼僅用於學習目的,請勿在實際產品中使用。

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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM