2014-02-05 7 views
0

이 코드는 다른 게시물에서 읽었으며 JPanel 및 JLabel을 사용하여 개별 Jframe에서이 코드를 읽었습니다. 이제이 코드의 도움으로 JFrame을 JFrame에 적용하고 싶습니다. 내 JFrame의 이름 AddBatch입니다 는 JPanel의는 pnl_addBatch과의 JLabel는 lbl_addBatch [모두 그 게으른 드래그를 사용하여 자신의 위치로 설정 :) 드롭] 천막 기능이 MarqueePanel에 상주 할 필요가 없습니다내 Jframe의 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

을 무슨 문제? pnl_addBatch를 MarqueePanel 인스턴스로 바꾸려고 시도 했습니까? 파생 된 클래스이기 때문에 다른 것을 변경할 필요가 없습니다. 생성자에 전달하려는 문자열과 int를 제공하지 말아야합니다. –

답변

1

입니다. 이것은 아마도 예제로만 의미가있었습니다. 하지만 클래스를 변경하여 생성자에서 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)); 
    } 
}