1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| package tests;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
/**
*
*/
/**
* @author Warnotte Renaud
*
*/
public class appTest extends JFrame
{
private JPanel contentPane;
private JButton btnConnection;
private JLabel label;
private JLabel label2;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
appTest frame = new appTest();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public appTest()
{
initialize();
}
private void initialize() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
contentPane.add(getBtnConnection(), BorderLayout.SOUTH);
label = new JLabel("0");
contentPane.add(label, BorderLayout.CENTER);
label2 = new JLabel("0");
contentPane.add(label2, BorderLayout.EAST);
}
private JButton getBtnConnection() {
if (btnConnection == null) {
btnConnection = new JButton("Connection");
btnConnection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
(new TacheDeFond()).execute();
}
});
}
return btnConnection;
}
class TacheDeFond extends SwingWorker<String, Object> {
@Override
public String doInBackground() {
btnConnection.setEnabled(false);
for (int i = 30 ; i > 0 ; i--)
{
label.setText("Delay = " + i);
for (int j = 0 ; j < 10; j++)
{
label2.setText(""+Math.random());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "";
}
@Override
protected void done() {
try {
label.setText("You can now login");
btnConnection.setEnabled(true);
} catch (Exception ignore) {
}
}
}
} |
Partager