/** * Loop2Ex * * This program demonstrates loops structures. * * @author Brad Rippe */ import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Container; import javax.swing.UIManager; public class Loop2Ex { public static void main(String[] args) { /** DON'T BE CONCERNED WITH THIS SECTION **/ JFrame frame = new JFrame( "Loop 2 Demo" ); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { } frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); final JTextArea textArea = new JTextArea( 35, 35 ); JScrollPane scrollPane = new JScrollPane( textArea ); scrollPane.setPreferredSize( new Dimension( 600, 300 ) ); frame.getContentPane().add( scrollPane, BorderLayout.CENTER ); JButton codeButton = new JButton( "Show Code" ); codeButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { textArea.setFont( new Font("Courier", Font.BOLD, 16 ) ); textArea.setText( "String message = \"\";\n" + "int j = 0;\n" + "int i = 0;\n" + "for( i=1; i <= 20; i++, j--) {\n" + " message += \"for loop #\" +i+ \" j is \" +j+ \"\\n\";\n" + " if( j < -5 ) {\n" + " continue;\n" + " }\n" + " message += \"2nd display #\" + i+ \" j is \" +j+ \"\\n\";\n" + "} //end for"); } }); frame.getContentPane().add( codeButton, BorderLayout.SOUTH ); frame.pack(); frame.setVisible(true); testLoop( frame, textArea ); } public static void testLoop(JFrame frame, JTextArea tA) { /***** THIS IS THE SECTION OF INTEREST *****/ String message = ""; int j = 0; int i = 0; for( i=1; i <= 20; i++, j--) { message += "for loop #" +i+ " j is " +j+ "\n"; if( j < -5 ) { continue; } message += "2nd display #" + i+ " j is " +j+ "\n"; } //end for tA.setText( message ); } }