2014-02-05 93 views
0

我已閱讀過其他文章中的此代碼,並且它正在使用JPanel和JLabel在單獨的Jframe上進行工作。現在,在這段代碼的幫助下,我想將它應用到我的JFrame的JLabel中。 在我的JFrame的名字是AddBatch,JPanel的是pnl_addBatch和JLabel的是lbl_addBatch [所有使用懶惰拖拉設置爲自己的位置而下降:)]在我的Jframe中將這個「Marquee代碼」添加到JLabel中

import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 


public class MarqueeTest { 

    private void display() { 
     JFrame f = new JFrame("MarqueeTest"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     String s = "Tomorrow, and tomorrow, and tomorrow, " 
     + "creeps in this petty pace from day to day, " 
     + "to the last syllable of recorded time; ... " 
     + "It is a tale told by an idiot, full of " 
     + "sound and fury signifying nothing."; 
     MarqueePanel mp = new MarqueePanel(s, 32); 
     f.add(mp); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
     mp.start(); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new MarqueeTest().display(); 
      } 
     }); 
    } 
} 

class MarqueePanel extends JPanel implements ActionListener { 

    private static final int RATE = 12; 
    private final Timer timer = new Timer(1000/RATE, this); 
    private final JLabel label = new JLabel(); 
    private final String s; 
    private final int n; 
    private int index; 

    public MarqueePanel(String s, int n) { 
     if (s == null || n < 1) { 
      throw new IllegalArgumentException("Null string or n < 1"); 
     } 
     StringBuilder sb = new StringBuilder(n); 
     for (int i = 0; i < n; i++) { 
      sb.append(' '); 
     } 
     this.s = sb + s + sb; 
     this.n = n; 
     label.setFont(new Font("Serif", Font.ITALIC, 36)); 
     label.setText(sb.toString()); 
     this.add(label); 
    } 

    public void start() { 
     timer.start(); 
    } 

    public void stop() { 
     timer.stop(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     index++; 
     if (index > s.length() - n) { 
      index = 0; 
     } 
     label.setText(s.substring(index, index + n)); 
    } 
} 
+0

請告訴我這個問題?你有嘗試用MarqueePanel的實例替換你的pnl_addBatch嗎?作爲一個派生類,你不應該改變任何其他的東西,也許可以提供你想要選取的字符串給構造函數,以及int。 –

回答

1

選取框功能不必駐留在此MarqueePanel。這可能只是一個例子。但是您可以更改該類,以便它在其構造函數中接受JLabel(並且不再擴展JPanel)。這樣一來,你就可以申請這個跑馬燈效果,以任何一個JLabel - 也已經存在的:

JLabel lbl_addBatch = createdSomewhere(); 

// Add marquee effect to the existing label: 
Marquee marquee = new Marquee(lbl_addBatch, s, 32); 
marquee.start(); 

改變相應的代碼:

import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 


public class MarqueeTest { 

    private void display() { 
     JFrame f = new JFrame("MarqueeTest"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     String s = "Tomorrow, and tomorrow, and tomorrow, " 
     + "creeps in this petty pace from day to day, " 
     + "to the last syllable of recorded time; ... " 
     + "It is a tale told by an idiot, full of " 
     + "sound and fury signifying nothing."; 

     JLabel lbl_addBatch = new JLabel(); 
     JPanel pnl_addBatch = new JPanel(); 
     pnl_addBatch.add(lbl_addBatch); 

     Marquee marquee = new Marquee(lbl_addBatch, s, 32); 
     marquee.start(); 

     f.add(pnl_addBatch); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new MarqueeTest().display(); 
      } 
     }); 
    } 
} 

class Marquee implements ActionListener { 

    private static final int RATE = 12; 
    private final Timer timer = new Timer(1000/RATE, this); 
    private final JLabel label; 
    private final String s; 
    private final int n; 
    private int index; 

    public Marquee(JLabel label, String s, int n) { 
     if (s == null || n < 1) { 
      throw new IllegalArgumentException("Null string or n < 1"); 
     } 
     StringBuilder sb = new StringBuilder(n); 
     for (int i = 0; i < n; i++) { 
      sb.append(' '); 
     } 
     this.label = label; 
     this.s = sb + s + sb; 
     this.n = n; 
     label.setFont(new Font("Serif", Font.ITALIC, 36)); 
     label.setText(sb.toString()); 
    } 

    public void start() { 
     timer.start(); 
    } 

    public void stop() { 
     timer.stop(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     index++; 
     if (index > s.length() - n) { 
      index = 0; 
     } 
     label.setText(s.substring(index, index + n)); 
    } 
}