thieumao

Loading

Oct 12th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1.  
  2. import java.awt.Font;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.List;
  8. import java.util.concurrent.ExecutionException;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.SwingUtilities;
  14. import javax.swing.SwingWorker;
  15.  
  16. public class MainFrame extends JFrame {
  17.  
  18.     private JLabel countLabel1 = new JLabel("0");
  19.     private JLabel statusLabel = new JLabel("Task not completed.");
  20.     private JButton startButton = new JButton("Start");
  21.  
  22.     public MainFrame(String title) {
  23.         super(title);
  24.         setLayout(new GridBagLayout());
  25.         countLabel1.setFont(new Font("serif", Font.BOLD, 28));
  26.         GridBagConstraints gc = new GridBagConstraints();
  27.         gc.fill = GridBagConstraints.NONE;
  28.         gc.gridx = 0;
  29.         gc.gridy = 0;
  30.         gc.weightx = 1;
  31.         gc.weighty = 1;
  32.         add(countLabel1, gc);
  33.         gc.gridx = 0;
  34.         gc.gridy = 1;
  35.         gc.weightx = 1;
  36.         gc.weighty = 1;
  37.         add(statusLabel, gc);
  38.         gc.gridx = 0;
  39.         gc.gridy = 2;
  40.         gc.weightx = 1;
  41.         gc.weighty = 1;
  42.         add(startButton, gc);
  43.         startButton.addActionListener(new ActionListener() {
  44.             public void actionPerformed(ActionEvent arg0) {
  45.                 start();
  46.             }
  47.         });
  48.         setSize(200, 400);
  49.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  50.         setVisible(true);
  51.     }
  52.  
  53.     private void start() {
  54.         Thread worker = new Thread() {
  55.             public void run() {
  56.  
  57.                 // Simulate doing something useful.
  58.                 for (int i = 0; i <= 10; i++) {
  59.                     // Bad practice
  60.                     countLabel1.setText(Integer.toString(i));
  61.  
  62.                     try {
  63.                         Thread.sleep(1000);
  64.                     } catch (InterruptedException e) {
  65.                     }
  66.                 }
  67.  
  68.                 // Bad practice
  69.                 statusLabel.setText("Completed.");
  70.             }
  71.         };
  72.  
  73.         worker.start();
  74.     }
  75.  
  76.     public static void main(String[] args) {
  77.         SwingUtilities.invokeLater(new Runnable() {
  78.             @Override
  79.             public void run() {
  80.                 new MainFrame("SwingWorker Demo");
  81.             }
  82.         });
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment