import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
public class DigitalClock extends JFrame {
private static final long serialVersionUID = 1L;
JTextField time;
JPanel panel;
public DigitalClock()
{
super("Java Clock by(Nadir Ali Khan)");
setTitle("Java Clock");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(true);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new FlowLayout());
time = new JTextField(10);
time.setEditable(false);
time.setFont(new Font("Arial", Font.PLAIN, 48));
time.setSize(500, 500);
panel.add(time);
add(panel);
Timer t = new Timer(1000, new Listener());
t.start();
}
class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Calendar a = Calendar.getInstance();
int hour = a.get(Calendar.HOUR_OF_DAY);
int min = a.get(Calendar.MINUTE);
int sec = a.get(Calendar.SECOND);
time.setText(hour + ":" + min + ":" + sec);
}
}
}
public class Mainmain
{
public static void main(String[]args)
{
new Mainmain();
}
public Mainmain()
{
new DigitalClock();
}
}
Code compiled fine but when I run it, the window has to be resized slightly before I can see the time displayed. If I set the Resizeable property to false, the time is not displayed. I want to display the time without having to resize the window.