Java基於線程實現帶有滾動效果的Label標籤執行個體_java

來源:互聯網
上載者:User

本文執行個體講述了Java基於線程實現帶有滾動效果的Label標籤。分享給大家供大家參考。具體如下:

import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;/** * Java中用線程實現帶有滾動效果的Label標籤 */public class Test extends JFrame { private static final long serialVersionUID = -2397593626990759111L; private JPanel pane = null; private MoveLabel label = null; public Test() { super("Test"); pane = new JPanel(); label = new MoveLabel("帶有滾動效果的標籤"); pane.add(label); this.getContentPane().add(pane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 200); this.setVisible(true); } public static void main(String args[]) { new Test(); } /** * 帶有滾動效果的Label標籤,可繼續拓展很多特效,例如顏色變換、速度變換等 */ private class MoveLabel extends JLabel implements Runnable { private static final long serialVersionUID = 1891684760189602720L; private String text = null; private Thread thread = null; private int x = 0; private int w = 0, h = 0; public MoveLabel(String text) {  super(text);  this.text = text;  thread = new Thread(this);  thread.start(); } public String getText() {  return text; } public void setText(String text) {  super.setText(text);  this.text = text; } protected void paintComponent(Graphics g) {  super.paintComponent(g);  g.setColor(this.getBackground());  g.fillRect(0, 0, w = this.getWidth(), h = this.getHeight());  g.setColor(this.getForeground());  g.setFont(this.getFont());  g.drawString(text, x, h - 2); } public void run() {  while (true) {  x -= 2;  if (x < -w) {   x = w;  }  this.repaint();  try {   Thread.sleep(50);  } catch (InterruptedException e) {   e.printStackTrace();  }  } } }}

希望本文所述對大家的java程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.