marquee in title bar in JFrame
How can I make the title bar of a JFrame to be Marquee like the mar开发者_StackOverflow中文版quee in HTML if you use the marquee tag?
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.
精彩评论