Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please code in Java Sample Output: Tasks Involved Having created the Frame, you'll now create the user interface by placing controls in the frame. Follow
Please code in Java
Sample Output:
Tasks Involved Having created the Frame, you'll now create the user interface by placing controls in the frame. Follow the steps to continue with the Stopwatch example: We'll 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: eridy - 0 gridy - 1 sridy - 2 gridx - 0 startButton stopButton exitButton gridx = 1 gridx - 2 startLabel startTextField stopLabel stopTextField elapsedLabel elapsedTextField 1) Declare the nine controls 2) Replace the setSize line (from LA#1-Part1-I code) with the line establishing the grid layout: getContentPane ().set Layout(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 you've set the number of columns for the text field controls. At this point, your interface has a finished look. 4) Add the control Listener: In every project you'll construct, you need to "listen" for the event when the user closes the window. The adapter that implemente evente for the frame (window) is called the Window Adapter 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 window Closing 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 (action Performed) 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. Goals To successfully complete this assessment and obtain all the marks, you must: a) Declare 3 class level variables under the lines declaring frame controls b) In the frame constructor, add the Window Closing' event method which tells the application when to stop. Similarly, create the event method for the startButton and stopButton after their respective constructor methods. c) User should not be allowed to click the stop button before the start button and once, start button has been clicked, user must click the stop button. d) Be able to compile and execute this code from the command line. You will be called upon to do so, so have your command prompt ready during your lab session. X Stopwatch Application Start Starting Time: Stop Stopped Time: Elapsed Time: Exit Start pressed: X Stopwatch Application Start Starting Time: 1612968704757 stop stopped Time: Exit Elapsed Time: Stop pressed: Stopwatch Application Start Starting Time:1612968704757 stop stopped Time:161296872306|| Exit Elapsed Time: 18312 Start pressed a second time: X Stopwatch Application Start Starting Time: 1612968739402 Stop Stopped Time: Exit Elapsed Time
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started