Hi,
Have been reading along with Java and doing ok but I'm having difficults understanding some elements regarding GUI's = mainly Java frames and how to send results to them.
I have been working on some code from a book - and making sure I understand it wanted to look through and see how it all works.However I'm not sure how some of the results have been send to the frame.
This is what I have used so far...
ClockPanel.java -
ClockFrameCode:import javax.swing.*;
import java.util.*;
// C. Rogers Cadenhead
public class ClockPanel extends JPanel {
public ClockPanel() {
super();
String currentTime = getTime();
JLabel time = new JLabel ("Time: ");
JLabel current = new JLabel(currentTime);
add(time);
add(current);
}
String getTime() {
String time;
//get current time and date
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
int month = now.get(Calendar.MONTH) +1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
String timed = "" ;
if (hour > 12 ) {
// send result to jpanel!
}
else {
System.out.println("It's the morning!");
}
String monthName = "";
switch (month) {
case (1):
monthName = "January";
break;
case (2):
monthName= "Febuary";
break;
case(3):
monthName= "March";
break;
case(4):
monthName= "April";
break;
case(5):
monthName= "May";
break;
case(6):
monthName= "June";
break;
case(7):
monthName= "July";
break;
case(8):
monthName= "Aughust";
break;
case(9):
monthName= "September";
break;
case(10):
monthName= "October";
break;
case(11):
monthName= "November";
break;
case(12):
monthName= "December";
break;
}
time = monthName + " " + day + ", " + year + " " + hour + ":" + minute + " " + timed;
return time;
}
}
If you notice on the ClockPanel.java script there is an If statement which I have added in.Code:import java.awt.*;
import javax.swing.*;
// C. Rogers Cadenhead
public class ClockFrame extends JFrame {
public ClockFrame() {
super ("Clock");
setSize(225, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
ClockPanel time = new ClockPanel();
add(time);
setVisible(true);
}
public static void main (String[] arguments) {
ClockFrame clock = new ClockFrame();
}
}
Now the result of that would either be - System.out.println("It's the morning!"); or System.out.println("It's the afternoon!"); (the afternoon part I replaced with a comment while I was working on it.
However when I run that obviously the panel opens to display the time and date - but the result of the if statement opens in the console (within net beans).
I need to try and work out how the original result is sent to the java panel. I understand that the ClockFrame script calls the time string (think its a string...) from the ClockPanel script.
I believe that I need to add something to the ClockPanel script under the first string (again if my terminology is correct).
Am I looking in the correct area there?
So would add something within:
I presume then I would call entry in here in the time string at the bottom of the page...Code:public class ClockPanel extends JPanel {
public ClockPanel() {
super();
String currentTime = getTime();
JLabel time = new JLabel ("Time: ");
JLabel current = new JLabel(currentTime);
add(time);
add(current);
}
I think I might have got into a pickle with my varied understanding and hope someone can point me in the right direction. I'm ok finding it out I could just do with some direction of what I maybe need to read up on more for example.
Thanks everyone :)-:
