Hi I need help with this program I am trying to make for a Java Programming clas
ID: 3788583 • Letter: H
Question
Hi I need help with this program I am trying to make for a Java Programming class. I was given code for a few programs that already work, but wasn't formatted properly. I was tasked with making the code look more "readable" in addition to making sure it works after cleaning it up. But I am having an issue with this one program. It is a digital clock that is supposed to display whatever time my computer says, but in a new window.
Hereis the code I was given, after cleaning it up
import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.util.*;
import java.text.*;
public class DigitalClock
{
public static void main(String[]arguments)
{
Clock Label; date Lable = new ClockLabel("date");
Clock Label; time Lable = new ClockLabel("time");
Clock Label; day Lable = new ClockLabel("day");
JFrame.setDefaultLookAndFeelDecorated(true);
JFramef=newJFrame("DigitalClock");
f.setSize(300,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(newGridLayout(3,1));f.add(dateLable);
f.add(timeLable);f.add(dayLable);
f.getContentPane().setBackground(Color.black);
f.setVisible(true);
}
}
class ClockLabel extends JLabel implements ActionListener {
String type;
Simple Date;
Formats df;
public Clock Label(Stringtype)
{
this.type=type;setForeground(Color.green);
switch(type)
{
case"date":sdf=newSimpleDateFormat("MMMMddyyyy");
setFont(newFont("sans-serif",Font.PLAIN,12));
setHorizontalAlignment(SwingConstants.LEFT);
break;
case"time":sdf=newSimpleDateFormat("hh:mm:ssa");
setFont(newFont("sans-serif",Font.PLAIN,40));
setHorizontalAlignment(SwingConstants.CENTER);
break;
case"day":sdf=newSimpleDateFormat("EEEE");
setFont(newFont("sans-serif",Font.PLAIN,16));setHorizontalAlignment(SwingConstants.RIGHT);
break;default:sdf=newSimpleDateFormat();
break;
Timert=newTimer(1000,this);t.start();
}
}
public void actionPerformed(Action Eventae)
{
Dated=newDate();
setText(sdf.format(d));
}
}
When I try to compile it, I get an issue in line 48. specifically, the line "public Clock Label(Stringtype)"
when I try to compile it, it shows an arrow after the ")" and says identifier expected. I am wondering if someone could help me troubleshoot this
import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.util.*;
import java.text.*;
public class DigitalClock
{
public static void main(String[]arguments)
{
Clock Label; date Lable = new ClockLabel("date");
Clock Label; time Lable = new ClockLabel("time");
Clock Label; day Lable = new ClockLabel("day");
JFrame.setDefaultLookAndFeelDecorated(true);
JFramef=newJFrame("DigitalClock");
f.setSize(300,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(newGridLayout(3,1));f.add(dateLable);
f.add(timeLable);f.add(dayLable);
f.getContentPane().setBackground(Color.black);
f.setVisible(true);
}
}
class ClockLabel extends JLabel implements ActionListener {
String type;
Simple Date;
Formats df;
public Clock Label(Stringtype)
{
this.type=type;setForeground(Color.green);
switch(type)
{
case"date":sdf=newSimpleDateFormat("MMMMddyyyy");
setFont(newFont("sans-serif",Font.PLAIN,12));
setHorizontalAlignment(SwingConstants.LEFT);
break;
case"time":sdf=newSimpleDateFormat("hh:mm:ssa");
setFont(newFont("sans-serif",Font.PLAIN,40));
setHorizontalAlignment(SwingConstants.CENTER);
break;
case"day":sdf=newSimpleDateFormat("EEEE");
setFont(newFont("sans-serif",Font.PLAIN,16));setHorizontalAlignment(SwingConstants.RIGHT);
break;default:sdf=newSimpleDateFormat();
break;
Timert=newTimer(1000,this);t.start();
}
}
public void actionPerformed(Action Eventae)
{
Dated=newDate();
setText(sdf.format(d));
}
}
Explanation / Answer
Correct it by making (String type) and run the program it will execute.
public Clock Label(String type)
This is the change you have to made in your code.
This is the best code for your Question:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class CountDown {
public static void main(String[] args) {
new CountDown();
}
public CountDown() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Timer timer;
private long startTime = -1;
private long duration = 5000;
private JLabel label;
public TestPane() {
setLayout(new GridBagLayout());
timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
long now = System.currentTimeMillis();
long clockTime = now - startTime;
if (clockTime >= duration) {
clockTime = duration;
timer.stop();
}
SimpleDateFormat df = new SimpleDateFormat("mm:ss:SSS");
label.setText(df.format(duration - clockTime));
}
});
timer.setInitialDelay(0);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!timer.isRunning()) {
startTime = -1;
timer.start();
}
}
});
label = new JLabel("...");
add(label);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}