简体   繁体   中英

marquee in title bar in JFrame

如果使用marquee标签,如何使JFrame的标题栏像HTML中的选取框一样成为选取框?

Please don't.

If you do decide to, then the obvious technique is to setTitle with a subsequence of the required text. I guess various partial sized spaces in Unicode may allow you to make the slightly smoother (or they might appear as squares).

Alternatively, you could make the window PL&F decorated (wont work with the native PL&F in the Sun/Oracle implementation) and draw the text yourself. To look good, you'd need to tune it to the particular PL&F and configuration.

God forgive me for the following code

Put this code in your frame constructor if you want the marquee to start directly after loading:

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

UPDATE
As per the comments, here is another version using 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();

This code is for learning purpose only, please don't use it in a real product.

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

Try this, fool proof. And easier to understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM