Advertisement
TermSpar

Dynamic Loop

Feb 20th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.*;
  5. public class Loop1 {
  6.     public Loop1(){
  7.         createLoop();
  8.     }
  9.    
  10.     public void createLoop(){
  11.         JFrame frame = new JFrame("Set Loop Size");
  12.         frame.setVisible(true);
  13.         frame.setSize(400, 400);
  14.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.        
  16.         JPanel panel = new JPanel();
  17.        
  18.         JButton btn = new JButton();
  19.         btn.setText("Set Loop Size");
  20.         btn.addActionListener(new ActionListener(){
  21.            
  22.             public void actionPerformed(ActionEvent e){
  23.                 String loopSize = JOptionPane.showInputDialog("Enter Loop Size");
  24.                 int i = Integer.parseInt(loopSize);
  25.                 int k = 0;
  26.                 while(k < i){
  27.                     k += 1;
  28.                     JOptionPane.showMessageDialog(null, "Loop Number: " + k);
  29.                 }
  30.                
  31.             }
  32.            
  33.         });
  34.        
  35.         panel.add(btn);
  36.         frame.add(panel);
  37.        
  38.     }
  39.    
  40.     public static void main(String args[]){
  41.         new Loop1();
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement