import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * Framework or outline of problem described on: * http://www.-ib-computing.com/java/arrays/objectArrays.html * * @author Mr J wrote the outline * @version 1.0 */ public class XandShuffle extends Applet implements ActionListener { // arrays of text fields and Labels static final int MAX = 6; private TextField[] tfNames = new TextField[MAX]; private TextField[] tfScores = new TextField[MAX]; private Label[] lbNumbers = new Label[MAX]; private Label lbTitle = new Label("Array problem"); private Label lbScores = new Label ("Scores:"); private Label lbNames = new Label ("Names:"); private Button eliminate = new Button("Eliminate!"); private Button shuffle = new Button("Shuffle"); private Label messages = new Label("System messages will appear here"); // data arrays private int scores[] = new int[] { 5, 55, 32, 90, 80, 49}; private String names[] = new String[] {"Jimbo", "Jumbo", "Mumbo", "Mambo", "Hambo", "Himbo"}; /** * Initialize and objects to the Applet */ public void init() { // Panel with name labels Panel label = new Panel(); label.setLayout(new GridLayout(3,1)); label.add(lbTitle); label.add(lbScores); label.add(lbNames); // Panel with score and name boxes Panel main = new Panel(); main.setLayout(new GridLayout(3,6)); for(int x = 0; x < MAX; x++) { tfScores[x] = new TextField(10); tfNames[x] = new TextField(10); lbNumbers[x] = new Label("" + x + "."); main.add(lbNumbers[x]); } for(int x = 0; x < MAX; x++) { main.add(tfScores[x]); } for(int x = 0; x < MAX; x++) { main.add(tfNames[x]); } add(label); add(main); add(eliminate); add(shuffle); add(messages); // Causes button presses to be detected eliminate.addActionListener(this); shuffle.addActionListener(this); // initialise display updateDisplay(scores, names); } /** * When an event occurs on an object with an ActionListener attached, this * method is carried out. * * @param e carries details about the event that occurred */ public void actionPerformed(ActionEvent e) { // sample action... messages.setText("OK, let's solve this problem!"); } /** * Put values from arrays in display TextFields * * @param scores the int array of scores * @param names a String array of names */ private void updateDisplay(int[] scores, String[] names) { for(int x = 0; x < MAX; x++) { tfScores[x].setText("" + scores[x]); tfNames[x].setText(names[x]); } } /** * read values into arrays from TextFields * This is useful if you want to test your algorithms with * other sets of data... * * @param text field array of scores * @param text field array of names */ private void readDisplay(TextField[] tfS, TextField[] tfN) { for(int x = 0; x < MAX; x++) { scores[x] = Integer.parseInt(tfS[x].getText().trim()); names[x] = tfN[x].getText().trim(); } } }