Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this hw problem: Implement a GUI program that acts as a time display. The

ID: 3914383 • Letter: I

Question

I have this hw problem: Implement a GUI program that acts as a time display. The program should print the current time when the user clicks on a button within the application. The program should also change the background of the GUI each time the user clicks. A minimum of four colors should be used. I put together the following code. Please let me know what changes I should make and why:

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Random;

public class TimeViewFrame extends JFrame implements ActionListener {

private JLabel timeLabel;

private JButton timeButton;

private JPanel timePanel;

private ActionListener listener;

private Color[] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW}

private static final int FRAME_WIDTH = 400

private static final int FRAME_HEIGHT = 200

public TimeViewFrame() {

setSize(FRAME_WIDTH, FRAME_HEIGHT);

JPanel timePanel = new JPanel;

timePanel.setLayout(new BorderLayout());

JButton timeButton = new JButton(“Current Time”);

timeButton.addActionListener(listener);

timePanel.add(timeButton, BorderLayout.Center);

JLabel timeLabel = new JLabel ();

timePanel.add(timeLabel, BorderLayout.South);

add(timePanel);

TimeViewFrame.setDefaultCloseOperation(JFRAME.EXIT_ON_CLOSE);

TimeViewFrame.setVisible(true);

}

}

public static void main(String [] args) {

new TimeViewFrame();

}

public void actionPerformed(ActionEvent event) {

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

       Date now = new Date();

String currentTime = sdf.format(now);

timeLabel.setText(“Current Time: “ + currentTime);

Random r = new Random();

int random = r.nextInt(colors.length);

timePanel.setBackground(colors[random]);

}

Explanation / Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class TimeViewFrame extends JFrame implements ActionListener {

private JLabel timeLabel;

private JButton timeButton;

private JPanel timePanel;

//private ActionListener listener;

private Color[] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};

private static final int FRAME_WIDTH = 400;

private static final int FRAME_HEIGHT = 200;

public TimeViewFrame() {

setSize(FRAME_WIDTH, FRAME_HEIGHT);

timePanel = new JPanel();
timeLabel = new JLabel ();
timeButton = new JButton("Current Time");
timePanel.setLayout(new BorderLayout());

timeButton.addActionListener(this);

timePanel.add(timeButton, BorderLayout.CENTER);

timePanel.add(timeLabel, BorderLayout.SOUTH);

add(timePanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}


public static void main(String [] args) {

new TimeViewFrame();

}

public void actionPerformed(ActionEvent event) {

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

Date now = new Date();

String currentTime = sdf.format(now);
timeLabel.setText("Current Time: " + currentTime);
Random r = new Random();
int random = r.nextInt(colors.length);
timePanel.setBackground(colors[random]);
}
}

The problem with your code was that there were some syntax errors and you were declaring the variables again in your constructor and not using the class variables.

Do give a thumbs up