Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Code compiles fine but doesn't run Properly

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code compiles fine but doesn't run Properly

    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.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Code compiles fine but doesn't run Properly

    Add a call to revalidate() at the end of your JFrames constructor.

Similar Threads

  1. Replies: 6
    Last Post: November 25th, 2011, 03:58 PM
  2. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 12:36 PM
  3. HELP! Gui is running fine, don't know exactly how to code.
    By cc11rocks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2011, 02:12 PM
  4. File IO Compiles, but wont work?
    By StarKannon in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 2nd, 2011, 09:05 AM
  5. making a .bat file that compiles your java files
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2010, 12:55 AM