import java.awt.*; import javax.swing.*; public class Denko extends JPanel implements Runnable { private JLabel label; private volatile Thread thread = null; private String msg = "0123456789ABCDEF "; public Denko() { label = new JLabel(msg); add(label); JButton startBtn = new JButton("start"); startBtn.addActionListener(e -> startThread()); JButton stopBtn = new JButton("stop"); stopBtn.addActionListener(e -> stopThread()); setLayout(new FlowLayout()); add(startBtn); add(stopBtn); startThread(); } private void startThread() { if (thread == null) { thread = new Thread(this); thread.start(); } } private void stopThread() { thread = null; } public void run() { Thread thisThread = Thread.currentThread(); for (int i = 0; thread == thisThread; i = (i + 1) % msg.length()) { String str = msg.substring(i) + msg.substring(0, i); SwingUtilities.invokeLater(() -> label.setText(str)); try { Thread.sleep(100); // 100 ミリ秒お休み } catch (InterruptedException e) {} } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("電光掲示板"); frame.add(new Denko()); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }); } }