// output in List box, for loop
import java.awt.*;
import java.awt.event.*;
public class Counter extends Frame implements ActionListener
{
public static void main(String[] args) // main starts the program
{
new Counter(); // construct a Counter
}
//---- Create Components -----------
List nums = new List(15); // Create a List, named nums
Button quit = new Button("Exit Program"); // Create a Button named quit
//--- Constructor - runs when a new Counter is created ----
public Counter()
{
//--- setting up window ----------
setLayout(new FlowLayout());
setSize(150,300);
add(nums);
add(quit);
quit.addActionListener(this);
setVisible(true); // remember to make Frame visible
//-- Calculations ----------------
int total = 0;
for (int c=1; c < 100; c = c + 2) // count 1,3,5,...,99
{
total = total + c; // adding up numbers
nums.add(c + ""); // adds c to the List
// c + "" converts number to String
}
nums.add("Total = " + total);
}
//--- Respond to Buttons -------------
public void actionPerformed(ActionEvent e)
{ System.exit(0); // quit Button ends the program
}
}
|