Question: Having created the Frame, youll now create the user interface by placing controls in the frame. Follow the steps to continue with the Stopwatch example:
Having created the Frame, youll now create the user interface by placing controls in the frame. Follow the steps to continue with the Stopwatch example: Well place 9 controls in the frame: three buttons, three labels and three text fields. The buttons will start and stop the timing. The labels and text fields will be used to display the timing results: You must place these controls in a 3 x 3 grid as given below: 1) Declare the nine controls 2) Replace the setSize line (from LA#1-Part1-I code) with the line establishing the grid layout: getContentPane().setLayout(new GridBagLayout()); 3) Set the properties of the nine controls: a) startButton: text, gridx and gridy b) stopButton : text, gridx, gridy c) exitButton : text, gridx, gridy d) startLabel : text, gridx, gridy e) stopLabel : text, gridx, gridy f) elapsedLabel : text, gridx, gridy g) startTextField : text, columns, gridx, gridy h) stopTextField : text, columns, gridx, gridy i) elapsedTextField : text, columns, gridx, gridy Notice how each control is positioned within the grid along with how youve set the number of columns for the text field controls. At this point, your interface has a finished look. CST8221 JAP Lab Assessment #2 Lab Submission Date on BRS February 14th, 2021 (23:59) 2 4) Add the control Listener: In every project youll construct, you need to listen for the event when the user closes the window. The adapter that implements events for the frame (window) is called the WindowAdapter and it works with the WindowListener. There are certain window events that can be listened for. In our case, we want to listen for the windowClosing event. The code that adds this event method to our application is: addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { [Java code when and how to close the window] } }); For Swing components, like the button, label and text field used here, event methods (actionPerformed) are added using the ActionListener. If the component is named ctrlName, the method is added using: ctrlName.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { [Java code to execute] } }); The method has a single argument (ActionEvent e), which tells us about which particular event has occurred (each control can respond to a number of events). 5) Write the code for every event a response is needed for. In this application, there are 3 such events clicking on each of the buttons.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
