JDot
package puntini; import java.awt.Color; import javax.swing.JLabel; public class JDot extends JLabel implements Runnable { private String s = "Attendere"; private boolean running = false; public JDot() { this.setText(s); this.setOpaque(true); this.setBackground(Color.YELLOW); } public void start() { if (!running) { running = true; Thread t = new Thread(this); t.setDaemon(true); t.start(); } } public void stop() { running = false; } @Override public void run() { while (running) { s += '.'; this.setText(s); try { Thread.sleep(1000); } catch (InterruptedException e) { } } } }
Main
package puntini; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.SpringLayout; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Main extends JFrame { private JDot jDot; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Main frame = new Main(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Main() { setTitle("Puntini"); setBounds(100, 100, 450, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SpringLayout springLayout = new SpringLayout(); getContentPane().setLayout(springLayout); jDot = new JDot(); springLayout.putConstraint(SpringLayout.NORTH, jDot, 10, SpringLayout.NORTH, getContentPane()); springLayout.putConstraint(SpringLayout.WEST, jDot, 10, SpringLayout.WEST, getContentPane()); getContentPane().add(jDot); JButton btnAvvia = new JButton("Avvia"); btnAvvia.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { jDot.start(); } }); springLayout.putConstraint(SpringLayout.NORTH, btnAvvia, 6, SpringLayout.SOUTH, jDot); springLayout.putConstraint(SpringLayout.WEST, btnAvvia, 0, SpringLayout.WEST, jDot); getContentPane().add(btnAvvia); JButton btnFerma = new JButton("Ferma"); btnFerma.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jDot.stop(); } }); springLayout.putConstraint(SpringLayout.NORTH, btnFerma, 0, SpringLayout.NORTH, btnAvvia); springLayout.putConstraint(SpringLayout.WEST, btnFerma, 6, SpringLayout.EAST, btnAvvia); getContentPane().add(btnFerma); } }