荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: ant (江南笑笑生), 信区: Homepage
标  题: 网页时钟代码
发信站: BBS 荔园晨风站 (Sat May  8 08:12:35 1999), 转信

转信站: argo (local)





import java.util.*;

import java.awt.*;

import java.applet.*;

// import java.io.*;



public class Clock2 extends Applet implements Runnable {

  // Globals

  Thread timer = null; // The timer thread

  Color foregroundCol; // The foreground color

  Color DefaultforegroundCol = new Color(0, 0, 0); // The default Foreground (B

  Color backgroundCol; // The background color

  Color DefaultbackgroundCol = new Color(192, 192, 192); // 'Browser Gray'

  Date dummy = new Date(); // The first date to format the output from

  Font showfont; // The font used for display

  String lastdate = dummy.toLocaleString(); // The date to erase

  String DateFmt = "%a, %B %e %T";



  String[] weekDays = new String[7];

  String[] months = new String[12];



/**

Get a color parameter from the html page (<param ....>)

If the parameter is not given in the <applet> container,

return the default color.

*/

private int getColorFromParam(String parmName, Color defaultCol)

{

  Integer IntColor;

  String parmValue = this.getParameter(parmName); // Get the value

  if (parmValue != null) // Was the <param name="[parmName]" ...> there?

  {

    Integer intColor = Integer.valueOf(parmValue, 16);

    return(intColor.intValue()); // return the 32-bit color value

  }

  return (defaultCol.getRGB()); // return the default color value

}





/**

Prepare everything, so we will run correctly.

This includes getting and setting the parameters

obtained from the <param ...> tags.

*/

public void init()

{

  String FontFamily = "TimesRoman"; // The default Font Family

  int FontSize = 12; // The default Font Size

  int FontWeight = Font.PLAIN; // The default Font Weight

  // Set up the colors to use (FG & BG)

  backgroundCol = new Color(this.getColorFromParam("BGCol", DefaultbackgroundCo

);

  foregroundCol = new Color(this.getColorFromParam("FGCol", DefaultforegroundCo

);

  this.setBackground(backgroundCol); // Set the background Color



  // If the "FontFamily" param is present

  if (this.getParameter("FontFamily") != null)

  {

    FontFamily = this.getParameter("FontFamily"); // Change the Font Family

  }



  // If the "FontSize" param is present

  if (this.getParameter("FontSize") != null)

  {

    // Convert the value string into an Integer

    Integer IntFontSize = new Integer(this.getParameter("FontSize"));

    // and then to an int

    FontSize = IntFontSize.intValue();

  }



  // If the "FontWeight" param is present

  if (this.getParameter("FontWeight") != null)

  {

    String SFontWeight = new String(this.getParameter("FontWeight"));

    SFontWeight = SFontWeight.toLowerCase(); // convert to lowercase

    // Set the appropriate values. NOTE: You can't or values together...

    if (SFontWeight.equals("bold")) { FontWeight = Font.BOLD; }

    if (SFontWeight.equals("italic")) { FontWeight = Font.ITALIC; }

    if (SFontWeight.equals("plain")) { FontWeight = Font.PLAIN; }

  }

  showfont = new Font(FontFamily, FontWeight, FontSize); // Create the desired

nt.



  // if the "DateFmt" param is present

  if (this.getParameter("DateFmt") != null)

  {

    DateFmt = this.getParameter("DateFmt");

  }



  // Initialize the Formatting Arrays for date display



  // Days (Abbreviated is obtained by x.substring(0,3))

  weekDays[0] = new String("Sunday");

  weekDays[1] = new String("Monday");

  weekDays[2] = new String("Tuesday");

  weekDays[3] = new String("Wednesday");

  weekDays[4] = new String("Thursday");

  weekDays[5] = new String("Friday");

  weekDays[6] = new String("Saturday");



  // Months (Abbreviated is obtained by x.substring(0,3))

  months[0] = new String("January");

  months[1] = new String("February");

  months[2] = new String("March");

  months[3] = new String("April");

  months[4] = new String("May");

  months[5] = new String("June");

  months[6] = new String("July");

  months[7] = new String("August");

  months[8] = new String("September");

  months[9] = new String("October");

  months[10] = new String("November");

  months[11] = new String("December");



}





// Left Pad a number

private String padElement(int expr, char padChar)

{

  String result = "";

  // I'm just padding 2 digit numbers

  if (expr < 10) result = result.concat(String.valueOf(padChar));

  result = result.concat(String.valueOf(expr));

  return(result);

}

  {

    if (fmt.charAt(i) == '%') // We've hit a formatting command...

    {

      i++; // Move past the '%' sign

      if (fmt.length() <= i) // If the last char of the format is a lone '%'

      {

        formattedDate = formattedDate.concat("?");

        continue; // Forget the rest of the loop

      }

      // Figure out the format.

      switch (fmt.charAt(i))

      {

      case 'a': // Short Weekday name

        formattedDate = formattedDate.concat(weekDays[weekDay].substring(0,3));

        break;

      case 'A': // Long Weekday name

        formattedDate = formattedDate.concat(weekDays[weekDay]);

        break;

      case 'b': // Short Month name

      case 'h': // It's alias

        formattedDate = formattedDate.concat(months[month].substring(0,3));

        break;

      case 'B': // Long Month name

        formattedDate = formattedDate.concat(months[month]);

        break;

      case 'c': // The locale time/date string

        formattedDate = formattedDate.concat(d.toLocaleString());

        break;

      case 'C': // The default time/date string

        formattedDate = formattedDate.concat(d.toString());

        break;

      case 'd': // 2 digit month number

        formattedDate = formattedDate.concat(padElement(monthDay, '0'));

        break;

      case 'D': // Shortcut for %m/%d/%y

        formattedDate = formattedDate.concat(padElement(month + 1, '0'));

        formattedDate = formattedDate.concat(String.valueOf('/'));

        formattedDate = formattedDate.concat(padElement(monthDay, '0'));

        formattedDate = formattedDate.concat(String.valueOf('/'));

        formattedDate = formattedDate.concat(padElement(year, '0'));

        break;

      case 'e': // Month Number

        formattedDate = formattedDate.concat(padElement(monthDay, ' '));

        break;

      case 'H': // Hour -- 00 to 23

        formattedDate = formattedDate.concat(padElement(hour, '0'));

        break;

      case 'I': // Hour -- 01 to 12

        formattedDate = formattedDate.concat(padElement(US_Hour, '0'));

        break;

        // case 'j': // day of year 001 to 366 ; left out

        // (java doesn't have the d.getYearDay() function.

      case 'm': // Month numbers -- 01 to 12

        formattedDate = formattedDate.concat(padElement(month + 1, '0'));

        break;

      case 'M': // Minutes -- 00 to 59

        formattedDate = formattedDate.concat(padElement(minute, '0'));

        break;

      // case 'n': // Insert a newline; I guess drawString doesn't do \x stuff

      //   formattedDate = formattedDate.concat("\n");

      //   break;

      case 'p': // AM or PM

        formattedDate = formattedDate.concat(String.valueOf((hour < 12 ? "AM" :

PM")));

        break;

      case 'r': // Shortcut for %I:%M:%S %p

        formattedDate = formattedDate.concat(padElement(US_Hour, '0'));

        formattedDate = formattedDate.concat(String.valueOf(':'));

        formattedDate = formattedDate.concat(padElement(minute, '0'));

        formattedDate = formattedDate.concat(String.valueOf(':'));

        formattedDate = formattedDate.concat(padElement(second, '0'));

        formattedDate = formattedDate.concat(String.valueOf(' '));

        formattedDate = formattedDate.concat(String.valueOf((hour < 12 ? "AM" :

PM")));

        break;

      case 'R': // Shortcut for %H:%M

        formattedDate = formattedDate.concat(padElement(hour, '0'));

        formattedDate = formattedDate.concat(String.valueOf(':'));

        formattedDate = formattedDate.concat(padElement(minute, '0'));

        break;

      case 'S': // Second -- 00 to 61 (leap seconds)

        formattedDate = formattedDate.concat(padElement(second, '0'));

        break;

      // case 't': // Insert a tab character; It seems drawString doesn't do \x

tuff

      //   formattedDate = formattedDate.concat("\t");

      //   break;

      case 'T': // Shortcut for %H:%M:%S

        formattedDate = formattedDate.concat(padElement(year, '0'));

        break;

      case 'Y': // long year (1996)

        formattedDate = formattedDate.concat(padElement(longYear, '0'));

        break;

        // 'Z' not supported

      case '%': // Just in case you want to show the '%' sign

        formattedDate = formattedDate.concat("%");

        break;

      default:

        formattedDate = formattedDate.concat("??");

        break;

      }

    }

    else // A regular character

    {

      formattedDate = formattedDate.concat(String.valueOf(fmt.charAt(i)));

    }

  } // end for



  return(formattedDate);

}



/**

Paint is the main part of the program

First get the current date, and set the correct font;

then erase the old time/date, and draw the new one

*/

public void paint(Graphics g)

{

  String today;

  Date dat = new Date();



  g.setFont(showfont);



  today = formatDate(DateFmt, dat);

  // today = dat.toLocaleString();



  // Clean up the old text

  g.setColor(backgroundCol);

  g.drawString(lastdate, 5, 20);

  // Draw the new text

  g.setColor(foregroundCol);

  g.drawString(today, 5, 20);

  // Get ready to erase the old text.

  lastdate = today;

}



// start the thread

public void start()

{

  if(timer == null)

    {

      timer = new Thread(this);

      timer.start();

    }

}



/**

When the timer Thread is set to null, the applet will stop;

see start()

*/

public void stop()

{

  timer = null;

}





public void run()

{



  // Sleep in the timer thread...

  while (timer != null) {

    try {timer.sleep(100);} catch (InterruptedException e){}

    repaint(); // and do the redraw.

  }

******************************************************************

下面是网页

******************************************************************

<html>



<head>

<meta name="GENERATOR" content="Microsoft FrontPage 3.0">

<title>clock</title>

</head>



<body>



<h2 align="center"><strong>Clock.class 使用方法</strong></h2>



<h3>FontFamily</h3>



<p><b>Syntax</b>: <code>&lt;PARAM NAME=&quot;FontFamily&quot;

VALUE=&quot;FamilyName&quot;&gt;</code><br>

where <code>FamilyName</code> is the name of the Font you want to use. Currentl

 the

fonts I can get to work are:



<ul>

  <li>Courier </li>

  <li>Helvetica </li>

  <li>TimesRoman </li>

</ul>



<p><b>Default:</b> TimesRoman</p>



<h3>FontSize</h3>



<p><b>Syntax</b>: <code>&lt;PARAM NAME=&quot;FontSize&quot; VALUE=&quot;Size&qu

;&gt;</code><br>

where <code>Size</code> is the point size you want the font to display in.</p>



<p><b>Default</b>: 12</p>



<h3>FontWeight</h3>



<p><b>Syntax</b>: <code>&lt;PARAM NAME=&quot;FontWeight&quot; VALUE=&quot;Weigh

quot;&gt;</code><br>

where <code>Weight</code> is the weight to apply to the Font. Valid values are:



<ul>

  <li>plain </li>

  <li>bold </li>

  <li>italic </li>

</ul>



<p>It is not possible to combine the weights.</p>



<p><b>Default</b>: plain </p>



<h3>BGCol</h3>



<p><b>Syntax</b>: <code>&lt;PARAM NAME=&quot;BGCol&quot; VALUE=&quot;Color&quot

gt;</code><br>

where Color is the hexadecimal representation of the background color you wish

 use.</p>



<p><b>Format</b>: RRGGBB</p>



<p><b>Default</b>: 'Netscape Gray' (c0c0c0)</p>



<h3>FGCol</h3>



<p><b>Syntax</b>: <code>&lt;PARAM NAME=&quot;FGCol&quot; VALUE=&quot;Color&quot

gt;</code><br>

where Color is the hexadecimal representation of the foreground (text) color yo

wish to

use.</p>



<p><b>Format</b>: RRGGBB</p>



<p><b>Default</b>: Black (000000)</p>



<h3>DateFmt</h3>



<p><b>Syntax</b>: <code>&lt;PARAM NAME=&quot;DateFmt&quot; VALUE=&quot;Format&q

t;&gt;</code><br>

where <code>Format</code> is the format of the string you want to display.</p>



<p>The string can contain text, as well as formatting fields. Formatting fields

egin with

the '%' character. Example: <code>%H</code> will display the current hour.



<ul>

  <li><b><code>a</code></b> abbreviated weekday name (3 characters) </li>

  <li><b><code>A</code></b> full weekday name </li>

  <li><b><code>b</code></b> abbreviated month name (3 characters) </li>

  <li><b><code>B</code></b> full month name </li>

  <li><b><code>c</code></b> locale's appropriate date and time representation <

i>

  <li><b><code>C</code></b> default date and time format </li>

  <li><b><code>d</code></b> day of month - 01 to 31 </li>

  <li><b><code>D</code></b> date as %m/%d/%y </li>

  <li><b><code>e</code></b> day of month - 1 to 31 (single digits are preceded

 a blank) </li>

  <li><b><code>h</code></b> abbreviated month name (alias for %b) </li>

  <li><b><code>H</code></b> hour - 00 to 23 </li>

  <li><b><code>I</code></b> hour - 01 to 12 </li>

  <li><b><code>m</code></b> month of year - 01 to 12 </li>

  <li><b><code>M</code></b> minute - 00 to 59 </li>

  <li><b><code>p</code></b> string containing ante-meridiem or post-meridiem in

cator (AM or

    PM) </li>

  <li><b><code>r</code></b> time as %I:%M:%S %p </li>

  <li><b><code>R</code></b> time as %H:%M </li>

  <li><b><code>S</code></b> second - 00 to 61, allows for leap seconds </li>

  <li><b><code>T</code></b> time as %H:%M:%S </li>

  <li><b><code>w</code></b> day of week - Sunday = 0 </li>

  <li><b><code>y</code></b> year within century - 00 to 99 </li>

  <li><b><code>Y</code></b> year as ccyy (4 digits) </li>

  <li><b><code>%</code></b> to display the '%' character in the display </li>

</ul>



<p>The following conversions from date(1) are not supported:



<ul>

  <li><b><code>j</code></b> </li>

  <li><b><code>u</code></b> </li>

  <li><b><code>W</code></b> </li>

  <li><b><code>n</code></b> </li>

  <li><b><code>t</code></b> </li>

</ul>



<p><b>Default</b>: <code>&quot;%a, %B %e %T&quot;</code></p>

</body>

</html>



--


--
少年心事当拿云,谁念幽寒坐呜呃!

※ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: 192.168.1.235]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店