private Thread thread=null; /** * This method initializes jDesktopPane * * @return javax.swing.JDesktopPane */ private JDesktopPane getMainDesktopPane() { if (mainDesktopPane == null) { scrollLabel = new JLabel(); scrollLabel.setBounds(new java.awt.Rectangle(500,10,40,20)); scrollLabel.setText("我爱婷"); mainDesktopPane = new JDesktopPane(); mainDesktopPane.add(scrollLabel, null); } return mainDesktopPane; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ScrollText application = new ScrollText(); application.thread.start();
application.setVisible(true);
} /** * This is the default constructor */ public ScrollText() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() {
你可以把JLabel的位置放到最右边。调用JLable的setLocation(int x,int y)然后把x值不断减少。可以把它写到一个方法中,然后用一个独立的线程来调用这个方法。就可以实现滚动延迟了.我把这个类实现了Runnable接口。
例如
import java.awt.BorderLayout;
import java.awt.Point;import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class ScrollText extends JFrame implements Runnable{ private JPanel jContentPane = null; private JDesktopPane mainDesktopPane = null; private JLabel scrollLabel = null;
private Thread thread=null;
/**
* This method initializes jDesktopPane
*
* @return javax.swing.JDesktopPane
*/
private JDesktopPane getMainDesktopPane() {
if (mainDesktopPane == null) {
scrollLabel = new JLabel();
scrollLabel.setBounds(new java.awt.Rectangle(500,10,40,20));
scrollLabel.setText("我爱婷");
mainDesktopPane = new JDesktopPane();
mainDesktopPane.add(scrollLabel, null);
}
return mainDesktopPane;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub ScrollText application = new ScrollText();
application.thread.start();
application.setVisible(true);
} /**
* This is the default constructor
*/
public ScrollText() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
scrollLabel=new JLabel("我爱你");
this.setContentPane(getJContentPane());
thread=new Thread(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 298);
this.setTitle("Application");
} /**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getMainDesktopPane(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
} private void startScroll()
{
while (thread!=null) {
Point p=scrollLabel.getLocation() ;
p.x=(p.x-1)%this.getWidth();
if(p.x==0) p.x=this.getWidth();
scrollLabel.setLocation(p);
try {
thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} public void run() {
// TODO Auto-generated method stub
startScroll();
}
} // @jve:decl-index=0:visual-constraint="163,-19"