Results 1 to 2 of 2

Thread: looping for specified amount of time

Threaded View

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    looping for specified amount of time

    A Simple program i made has a text box were you specify the number of times to make the loop, a text box of the text to display in the loop and this is displayed in a JTextArea. It might be easer if i post the code:


    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.util.*;
    
    public class LoopApplet extends JApplet implements ActionListener 
    {
      JButton perform;
      JLabel timesLabel;
      JLabel textLabel;
      JTextField txtNumOfTimes;
      JTextField txtText;
      JTextArea message;
      
      public void init() 
      {
    	Container pane = getContentPane();
    	FlowLayout flo = new FlowLayout();
    	pane.setLayout(flo);
    	
    	JPanel row1 = new JPanel();
    	timesLabel = new JLabel("# of Times");
    	row1.add(timesLabel);
    	txtNumOfTimes = new JTextField(15);
    	row1.add(txtNumOfTimes);
    	pane.add(row1);	
    	
    	JPanel row2 = new JPanel();
    	textLabel  = new JLabel("Text :");
    	row2.add(textLabel);
    	txtText = new JTextField(15);
    	row2.add(txtText);
    	pane.add(row2);
    	
    	JPanel row3 = new JPanel();
    	perform = new JButton("Perform");
    	perform.addActionListener(this);
    	row3.add(perform);
    	pane.add(row3);
    
    	JPanel row4 = new JPanel();
    	message = new JTextArea(5,15);
    	message.setLineWrap(true);
            message.setWrapStyleWord(true);
            JScrollPane scroll = new JScrollPane(message, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            row4.add(scroll);
    	pane.add(row4);
    	setContentPane(pane);
    	setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e) 
      {
    	String num = txtNumOfTimes.getText();
    	int numTimes = Integer.parseInt(num);
    	
    	String text = txtText.getText();
       String source = e.getActionCommand();
    	 try {
    	 	for (int x=0; x<numTimes; x++)
    		{
    			message.append(x + " " + text + "\n");
    		}
    
    	     } catch (NumberFormatException nfe)
    	       {
    			txtText.setText("NumberFormatException");
    		}
     }

    I want to change the code in the actionperformed event so that it loops for an amount of time(what the user inputs in the textbox) but i have failed every time i try this. I know how to loop for a given amount of time but not sure how to apply it to this. Here is an example of looping for 1minute.

    Code:
    import java.util.*;
    
    class Repeat {
      public static void main(String[] arguments) {
       String sentence = "poop";
       int count = 1;
       Calendar start = Calendar.getInstance();
       int startMinute = start.get(Calendar.MINUTE);
       int startSecond = start.get(Calendar.SECOND);
       start.roll(Calendar.MINUTE, true);
       int nextMinute = start.get(Calendar.MINUTE);
       int nextSecond = start.get(Calendar.SECOND);
       while (count < 1000000) {
         System.out.println(sentence);
         GregorianCalendar now = new GregorianCalendar();
          if (now.get(Calendar.MINUTE) >= nextMinute)
             if (now.get(Calendar.SECOND) >+ nextSecond)
                 break;
            count++;
       }
       System.out.println("\nDisplayed " + count
                           + " times. ");
     }
    }
    How could i make this into my program??????????
    Last edited by System_Error; Jul 25th, 2004 at 06:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width