Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a Java Swing Frame Stopwatch project to read the current date/time. I would like to change the layout of the GUI so that

This is a Java Swing Frame Stopwatch project to read the current date/time.

I would like to change the layout of the GUI so that it changes the Start/Stop/Exit row into a column(picture below).

Now:

image text in transcribed

I want to be like this:

image text in transcribed

package stopwatch;

import java.awt.Component; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder;

public class Stopwatch extends JFrame { JButton startButton, stopButton, exitButton; JLabel startLabel, stopLabel, elapsedLabel; JTextField startTextField, stopTextField, elapsedTextField; long startTime; long stopTime; Stopwatch() {

JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 3)); panel.setBorder(new EmptyBorder(20, 20, 20, 20));

//Buttons startButton = new JButton("START"); stopButton = new JButton("STOP"); exitButton = new JButton("EXIT"); startButton.addActionListener(new StartListener()); stopButton.addActionListener(new StopListener()); exitButton.addActionListener(new ExitListener()); panel.add(startButton); panel.add(stopButton); panel.add(exitButton); //Labels startLabel = new JLabel("START", SwingConstants.CENTER); stopLabel = new JLabel("STOP", SwingConstants.CENTER); elapsedLabel = new JLabel("ELAPSED", SwingConstants.CENTER); panel.add(startLabel); panel.add(stopLabel); panel.add(elapsedLabel); //Text fields startTextField = new JTextField(20); stopTextField = new JTextField(20); elapsedTextField = new JTextField(20); panel.add(startTextField); panel.add(stopTextField); panel.add(elapsedTextField); Component add = add(panel);

setVisible(true);

this.setSize(400, 180); }

// Main Method public static void main(String args[]) { Stopwatch stopwatch = new Stopwatch(); } class StartListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //startTime = System.nanoTime(); startTime = System.currentTimeMillis()/1000; //seconds startTextField.setText(String.valueOf(startTime)); } } class StopListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //stopTime = System.nanoTime(); stopTime = System.currentTimeMillis()/1000; stopTextField.setText(String.valueOf(stopTime)); long executedTime = stopTime - startTime; elapsedTextField.setText(String.valueOf(executedTime)); } } class ExitListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println("Exit"); } } }

ta I X START STOP EXIT START STOP ELAPSED 1613440636 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions