Ok im making a program that gets the time and im trying to put it into a applet and display it. I know it will print it out and it works. But i cant get the applet to work.
CODE
CODE
package clock;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewJPanel extends javax.swing.JPanel
{
private Date now;
private String time;
public NewJPanel()
{
initComponents();
try
{
while(true)
{
initDate();
int h = now.getHours();
int m = now.getMinutes();
int s = now.getSeconds();
Thread.sleep(1000);
if (h > 12)
{
h -= 12;
}
if (m < 10)
{
String minFirst = Integer.toString(m);
String minSecond = "0" + minFirst;
time = h + ":" + minSecond + ":" + s;
}
if (m > 10)
{
time = h + ":" + m + ":" + s;
}
jLabel1.setText(time);
System.out.println(time);
}
}
catch (InterruptedException ex)
{
Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void initDate()
{
now = new Date();
}
I have my main
CODE
package clock;
import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class Start extends JApplet
{
public static void main (String args[])
{
JApplet ja = new JApplet();
NewJPanel jp = new NewJPanel();
jp.setVisible(true);
ja.add(jp);
}
}
package clock;
import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class Start extends JApplet
{
public static void main (String args[])
{
JFrame f = new JFrame("Clock");
NewJPanel p = new NewJPanel();
f.setPreferredSize(new Dimension(500, 500));
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
I can also try it in a JFram but that does not pop up in a label.
So how can i get the time into a label on a Panel Frame Or Applet?
This post has been edited by Maverick519: 24 Sep, 2008 - 10:19 AM