import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class arrayExample extends JFrame
{
private JPanel contentPanel;
private JLabel lblInput;
private JTextField txtInput;
private JTextField txtOutput;
private JButton btnShow;
private JButton btnAdd;
private String temp; //the variable of the input box
private int counter = 0;
String student [] = new String [10];
private void add (Container con, Component widget, int left, int top, int width,
int height)
{
widget.setBounds(left, top, width, height);
con.add(widget);
}
arrayExample()
{
contentPanel = (JPanel)getContentPane();
contentPanel.setLayout(null);
lblInput = new JLabel ("Enter Name");
add(contentPanel, lblInput, 10, 0, 100, 20);
txtInput = new JTextField();
add(contentPanel, txtInput, 0, 30, 100, 20);
btnAdd = new JButton ("Add Name");
add(contentPanel, btnAdd, 10, 70, 80, 20);
btnShow = new JButton ("Show Names");
add(contentPanel, btnShow, 10, 110, 110, 20);
txtOutput = new JTextField();
add(contentPanel, txtOutput, 200, 0, 200, 300);
//details of the window
setTitle("Class List");
setSize(500, 500);
setLocation (new Point (150, 150));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
//adds the input from text box into the array
btnAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent add)
{
temp = txtInput.getText();
student[counter] = temp;
counter = counter + 1;
txtInput.setText("");
}
});
//controls the function of the Show button
//prints out the information put into the input field
btnShow.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent add)
{
for (int i = 0; i < student.length; i++)
{
txtOutput.setText(student[i]);
System.out.println(i + ": " + student[i]);
}
}
});
}
public static void main (String [] args)
{
new arrayExample();
}
}