import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class MyClock extends Applet
{
MyPanel mp;
public void init()
{
mp = new MyPanel(getParameter("format"));
add(mp);
}
}
class MyPanel extends Panel
{
MyThread tm;
Color b, f;
SimpleDateFormat formatter;
String previousDateText = "";
String dateText;
MyPanel(String df)
{
super();
formatter = new SimpleDateFormat(df);
validate();
setBackground(new Color(0).black);
setForeground(new Color(0).yellow);
b = this.getBackground();
f = this.getForeground();
tm = new MyThread(this);
tm.start();
}
public Dimension getPreferredSize()
{
return new Dimension(this.getFontMetrics(this.getFont()).
stringWidth(getNow()) + 25, 30);
}
public void paint(Graphics g)
{
if (g != null)
{
g.setColor(b);
g.drawString(previousDateText, 10, 15);
g.setColor(f);
dateText = getNow();
g.drawString(dateText, 10, 15);
previousDateText = dateText;
}
}
public String getNow()
{
return formatter.format(new Date());
}
}
class MyThread extends Thread
{
MyPanel mp;
public MyThread(MyPanel a)
{
mp = a;
}
public void run()
{
while (true)
{
try
{
mp.repaint();
this.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format" VALUE = "yyyy-MM-dd hh:mm:ss">
</APPLET><P>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format" VALUE = "h:mm a">
</APPLET><P>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format"
VALUE = "yyyy.MMMMM.dd GGG hh:mm aaa">
</APPLET><P>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format" VALUE = "H:mm:ss:SSS">
</APPLET><P>
</BODY>
</HTML>
[Applet1]
import java.awt.*;
public class Applet1 extends java.applet.Applet
{
TextField inputText;
Button b;
public void init()
{
setLayout(new FlowLayout());
inputText = new TextField("", 15 );
b = new Button("Send to Applet 2");
add(inputText);
add(b);
}
public boolean action(Event ev, Object arg)
{
if (ev.target instanceof Button)
{
String textMsg = inputText.getText().trim();
Applet2 applet2 = (Applet2) getAppletContext().
getApplet("applet2");
if ( applet2 != null )
{
applet2.AppendText( textMsg );
return true;
}
else
return false;
}
return false;
}
}
[Applet2]
import java.awt.*;
public class Applet2 extends java.applet.Applet
{
TextArea textBox;
public void init()
{
setLayout(new FlowLayout());
textBox = new TextArea( 5, 40 );
add( textBox );
}
public void AppendText( String msg )
{
String newLine = new String( "\n" );
textBox.appendText( msg );
textBox.appendText( newLine );
}
}
[HTML]
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE = "Applet1.class" NAME = "applet1" HEIGHT = 200
WIDTH = 150>
</APPLET>
<APPLET CODE = "Applet2.class" NAME = "applet2" HEIGHT = 200
WIDTH = 400>
</APPLET>
</BODY>
</HTML>
Trích từ "Thủ thuật lập trình Java"
import java.awt.*;
import java.util.*;
import java.text.*;
public class MyClock extends Applet
{
MyPanel mp;
public void init()
{
mp = new MyPanel(getParameter("format"));
add(mp);
}
}
class MyPanel extends Panel
{
MyThread tm;
Color b, f;
SimpleDateFormat formatter;
String previousDateText = "";
String dateText;
MyPanel(String df)
{
super();
formatter = new SimpleDateFormat(df);
validate();
setBackground(new Color(0).black);
setForeground(new Color(0).yellow);
b = this.getBackground();
f = this.getForeground();
tm = new MyThread(this);
tm.start();
}
public Dimension getPreferredSize()
{
return new Dimension(this.getFontMetrics(this.getFont()).
stringWidth(getNow()) + 25, 30);
}
public void paint(Graphics g)
{
if (g != null)
{
g.setColor(b);
g.drawString(previousDateText, 10, 15);
g.setColor(f);
dateText = getNow();
g.drawString(dateText, 10, 15);
previousDateText = dateText;
}
}
public String getNow()
{
return formatter.format(new Date());
}
}
class MyThread extends Thread
{
MyPanel mp;
public MyThread(MyPanel a)
{
mp = a;
}
public void run()
{
while (true)
{
try
{
mp.repaint();
this.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format" VALUE = "yyyy-MM-dd hh:mm:ss">
</APPLET><P>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format" VALUE = "h:mm a">
</APPLET><P>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format"
VALUE = "yyyy.MMMMM.dd GGG hh:mm aaa">
</APPLET><P>
<APPLET CODE = "MyClock.class" HEIGHT = 25 WIDTH = 200>
<PARAM NAME = "format" VALUE = "H:mm:ss:SSS">
</APPLET><P>
</BODY>
</HTML>
[Applet1]
import java.awt.*;
public class Applet1 extends java.applet.Applet
{
TextField inputText;
Button b;
public void init()
{
setLayout(new FlowLayout());
inputText = new TextField("", 15 );
b = new Button("Send to Applet 2");
add(inputText);
add(b);
}
public boolean action(Event ev, Object arg)
{
if (ev.target instanceof Button)
{
String textMsg = inputText.getText().trim();
Applet2 applet2 = (Applet2) getAppletContext().
getApplet("applet2");
if ( applet2 != null )
{
applet2.AppendText( textMsg );
return true;
}
else
return false;
}
return false;
}
}
[Applet2]
import java.awt.*;
public class Applet2 extends java.applet.Applet
{
TextArea textBox;
public void init()
{
setLayout(new FlowLayout());
textBox = new TextArea( 5, 40 );
add( textBox );
}
public void AppendText( String msg )
{
String newLine = new String( "\n" );
textBox.appendText( msg );
textBox.appendText( newLine );
}
}
[HTML]
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE = "Applet1.class" NAME = "applet1" HEIGHT = 200
WIDTH = 150>
</APPLET>
<APPLET CODE = "Applet2.class" NAME = "applet2" HEIGHT = 200
WIDTH = 400>
</APPLET>
</BODY>
</HTML>
Trích từ "Thủ thuật lập trình Java"