Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   finishing up a program (http://www.go4expert.com/forums/finishing-program-t10151/)

jimJohnson 21Apr2008 01:42

finishing up a program
 
I am finishing up a program and having a few issues....I can send my instructions so it may seem easier to what I want...the first issue deals with the for loop for the 2nd for loop in the actionperformed when i click on go it does not change any of the boxes to yellow

Also when I check for errors it does not check with the code I have...I know it says on the instructions to use try\catch but I am just going to use if statements because I am not very familar with the try\catch and will accept some points takin off...any help with this by tonight id really appreciate it as long as noone is too busy...Thanks

instructions:

This will incorporate arrays, for loops, and Frames all in one.

Create a panel containing an array of 16 TextArea components that change color to correspond with the start, stop, and step values entered by the user. Perform the following tasks to create the Checkerboard Array application shown below. When the user enters the start, stop, and step fields and then clicks the Go button, the results are also shown below.

1. Call your application Checkerboard.java
2. You will need the following variables… declare them as private:
a. 16 component TextArea array
b. a Panel to hold the array
c. 3 TextField components with length of 10
d. 3 int variables to receive the start, stop, and step values
e. 3 Labels to display the words Start, Stop, and Step
f. a Go button
g. a Clear button
h. a Panel to hold the 3 TextFields, 3 Labels, and the 2 Buttons
3. Create a constructor method to:
a. construct each of the components declared above and initializes the start, stop, and step variables to zero (when constructing the TextArea components, use the following parameters: null, 3, 5, 3)
b. set the Frame layout to BorderLayout
c. write a for loop to loop the array and set each of the 16 TextArea components in that array so they cannot be edited. In the same loop, set each of the TextArea components text to be 1 more than the index number. Also in this same loop, set the background of each of the TextArea components to white.
d. set the Panel for the TextArea components to GridLayout with 4 rows, 4 columns, and both gaps set to 10
e. set the Panel for the TextFields, Labels, and button to GridLayout with 3 rows, 3 columns, and both gaps set to 5
f. add the components to their respective Panels
g. make the buttons clickable
h. place the Panels in the Frame… put one in the NORTH and one in the CENTER
i. Enter the addWindowListener() method described in the chapter… this is the method that overrides the click of the X so it terminates the application
4. In your actionPerformed() method:
a. convert the data in your TextFields to int and store them in the variables declared above
b. write a loop that goes through the array setting every background color to blue
c. write another loop that’s based on the user inputs. Each time the loop is executed, change the background color to yellow (so… start your loop at the user’s specified starting condition. You’ll stop at the user’s specified stopping value. You’ll change the fields to yellow every time you increment your loop based on the step value. REMEMBER: Your displayed values are 1 off from your index numbers!!)

5. Write a main() method that creates an instance of the Checkerboard Frame.
a. set the bounds for the frame to 50, 100, 300, 400
b. set the title bar caption to Checkerboard Array
c. use the setVisible() method to display the application Frame during execution
6. After you get all of this complete, include error handling to make sure:
a. the values entered in the TextFields are valid integers
b. the start value is greater than or equal to 1 and less than or equal to 16
c. the stop value is greater than or equal to 1 and less than or equal to 16
d. the step value is greater than or equal to 1 and less than or equal to 16
e. the start condition is less than the stop condition
f. when an error occurs, give an error message in a dialog box that is specific to their error, remove the text from the invalid field, and put the cursor in that field so the user has a chance to re-enter… this can be accomplished by using multiple try/catch statements
g. only change the colors if the numbers are valid
7. Create a clear button as seen in the example below. This button should:
a. clear out all 3 TextFields
b. change the background color of all TextArea array elements to white
c. put the cursor in the start field
8. Document!!

Code:


//packages to import
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;


public class Checkerboard extends Frame implements ActionListener
{
        private Panel topPanel;
        private TextArea topDisplay[];
        private Panel bottomPanel;
        private TextField startField = new TextField(10);
        private TextField stopField = new TextField(10);
        private TextField stepField = new TextField(10);
        private Label startLabel = new Label ("Start");
        private Label stopLabel = new Label ("Stop");
        private Label stepLabel = new Label ("Step");
        private Button goButton;
        private Button clearButton;
        private boolean clearText;
        private boolean first;
        private int start;
        private int stop;
        private int step;

        //constructor methods
        public Checkerboard()

        {

                //construct components and initialize beginning values
                topPanel = new Panel();
                topDisplay = new TextArea[16];


                goButton = new Button("Go");
                clearButton = new Button("Clear");
                first = true;
                bottomPanel = new Panel();
                int start = 0;
                int stop = 0;
                int step = 0;
                bottomPanel.add(startField);
                bottomPanel.add(stopField);
                bottomPanel.add(stepField);
                bottomPanel.add(startLabel);
                bottomPanel.add(stopLabel);
                bottomPanel.add(stepLabel);
                bottomPanel.add(goButton);
                goButton.addActionListener(this);
                bottomPanel.add(clearButton);
                clearButton.addActionListener(this);
                clearText = true;

                //set layouts for the Frame and Panels
                setLayout(new BorderLayout());
                topPanel.setLayout(new GridLayout(4, 4, 10, 10));
                bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));

                //construct the Display
                for(int i = 0; i <= 15; i++)
                        {
                                topDisplay[i] = new TextArea(null, 3, 5, 3);
                                topDisplay[i].setText(String.valueOf(i+1));
                                topDisplay[i].setEditable(false);
                                topPanel.add(topDisplay[i]);

                        }

                //add components to frame
                add(topPanel, BorderLayout.NORTH);
                add(bottomPanel, BorderLayout.CENTER);


                //allow the x to close the application
                addWindowListener(new WindowAdapter()
                        {
                                public void windowClosing(WindowEvent e)
                                        {
                                                System.exit(0);
                                        }
                        } //end window adapter
                        );



        }

        public static void main(String args[])
        {

                Checkerboard f = new Checkerboard();
                f.setTitle("Checkerboard Array");
                f.setBounds(50, 100, 300, 400);
                f.setLocationRelativeTo(null);
                f.setVisible(true);





        } //end main

        public void actionPerformed(ActionEvent e)
        {


                //test go
                String arg = e.getActionCommand();

                //go button was clicked
                if(arg.equals("Go"))
                {
                        //convert data in TextField to int
                        int start = Integer.parseInt(startField.getText());
                        int stop = Integer.parseInt(stopField.getText());
                        int step = Integer.parseInt(stepField.getText());

                        if((start <= 1) && (start > 16))
                        {
                                JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
                                startField.setText(" ");
                                startField.requestFocus();
                        }

                        if ((stop < 1) && (stop > 16))
                        {
                                JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
                                stopField.setText(" ");
                                stopField.requestFocus();
                        }
                        if ((step < 1) && (step > 16))
                        {
                                JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
                                stepField.setText(" ");
                                stepField.requestFocus();
                        }

                        if (start < stop)
                        {
                                JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE);
                                startField.setText(" ");
                                stopField.setText(" ");
                                stepField.setText(" ");
                                startField.requestFocus();
                        }


                        for(int i = 0; i <=16; i++)
                        topDisplay[i].setBackground(Color.blue);

                        for(int i = start; i <= stop; step++)
                        topDisplay[i].setBackground(Color.yellow);








                } //end the if go



                //clear button was clicked
                if(arg.equals("Clear"))
                {
                        clearText = true;
                        startField.setText("");
                        stopField.setText("");
                        stepField.setText("");
                        first = true;
                        setBackground(Color.white);
                        startField.requestFocus();

                } //end the if clear



        }//end action listener


}//end class



All times are GMT +5.5. The time now is 01:20.