• Post Reply Bookmark Topic Watch Topic
  • New Topic

time display gui  RSS feed

 
Katherine Mejia
Greenhorn
Posts: 5
AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I decided to make a little program to practice action events. Its supposed to display the time, when you click the button. However, it only displays a minimized JFrame. What am i doing wrong ?
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch! ,  There is a code tag used to put your code in. Please use that tag whenever you want to post the code. While writing post you get a menu above, in that Code button precedes List button. It attracts more people to answer your question, If your code is readable
This is how your code would look like if you put code tag around your code. You can use preview button to see how it looks before submitting.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Welcome to the ranch!
The code you posted (while not being formatted well) did copy/paste well, and compiled
I've run it through a code formatter, and added the code tags for you.  This time :-)

The reason you get a blank frame is that it never adds anything to the frame.
It never adds anything because it never runs the initGUI() method (add a System.out.println to check if you don't believe me)
So why wouldn't it be running initGUI?
Or why wouldn't it be running the method that calls initGUI ?


Next step:  when you push the button, you run the following code.



Which part of this code tells it to display the time on screen?
Where on the screen would you display the time anyway?  What sort of component should be used to display the time?

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Stop using Date. Dreadful class with a dreadful public interface. Use one of the newer Date‑Time classes instead; for time you probably want LocalTime.
Another formatting thing which I think may have caused a common beginner's error. Classes shoul‍d always begin with CapitalLettersOrUpperCase. Please correct that, then try to identify your constructor.

Always use a real layout manager, never null, and use the layout to set the size of components. You can call setPreferredSize on each component and then call pack on the containing frame, in which case you will probably get a sensible size for the frame.
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its supposed to display the time


Where? All your code does is play with a couple of variables, but you never do anything with the variables.

The first thing you can try is to simply display the date on the console using something like:



If you want to display the time on the frame then you would need to create a JLabel and add the label to the frame. Then you would use code like:



I suggest you start by reading the Swing tutorial . The tutorial contains many working examples that you can modify.

You might start with "How to Use Labels" or "How to Write an ActionListener"
 
Katherine Mejia
Greenhorn
Posts: 5
AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Hi! Welcome to the ranch!
The code you posted (while not being formatted well) did copy/paste well, and compiled
I've run it through a code formatter, and added the code tags for you.  This time :-)

The reason you get a blank frame is that it never adds anything to the frame.
It never adds anything because it never runs the initGUI() method (add a System.out.println to check if you don't believe me)
So why wouldn't it be running initGUI?
Or why wouldn't it be running the method that calls initGUI ?


Next step:  when you push the button, you run the following code.



Which part of this code tells it to display the time on screen?
Where on the screen would you display the time anyway?  What sort of component should be used to display the time?



Thanks. I would want to display the time below where the button is.i would want it to be displayed like regular text. Eventually, I'd like to be able to format the text to use a different and larger font, centered in the frame.
Sorry about not using the code button. It's next to impossible to select text on this phone, but i plan on posting on my computer from now on.
 
Katherine Mejia
Greenhorn
Posts: 5
AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

Stop using Date.  . . . Always use a real layout manager, never null, and use the layout to set the size of components. . . ..


Thanks that was helpful
 
Katherine Mejia
Greenhorn
Posts: 5
AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Its supposed to display the time


Where? All your code does is play with a couple of variables, but you never do anything with the variables.  . . .



Cool, I'll look it up.
 
Katherine Mejia
Greenhorn
Posts: 5
AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another question. I made the gui work,and the components work out, and how to represent the time as a string,  but I don't why this code:



gives me this error:

Cannot find symbol:
Class: now
location: Class LocalTime


 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "now" method of LocalTime is a static method, not a class. You just say

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!