datetimedemo.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 958 行 · 第 1/2 页
JAVA
958 行
/*
* @(#)DateTimeDemo.java 1.1 98/06/16
*
* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
* (C) Copyright IBM Corp. 1996 - All Rights Reserved
*
* Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* The original version of this source code and documentation is copyrighted
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
* materials are provided under terms of a License Agreement between Taligent
* and Sun. This technology is protected by multiple US and International
* patents. This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.text.*;
/**
* DateTimeDemo demonstrates how Date/Time formatter works.
*/
public class DateTimeDemo extends DemoApplet
{
/**
* The main function which defines the behavior of the DateTimeDemo
* applet when an applet is started.
*/
public static void main(String argv[]) {
DemoApplet.showDemo(new DateTimeFrame(null));
}
/**
* This creates a DateTimeFrame for the demo applet.
*/
public Frame createDemoFrame(DemoApplet applet) {
return new DateTimeFrame(applet);
}
}
/**
* A Frame is a top-level window with a title. The default layout for a frame
* is BorderLayout. The DateTimeFrame class defines the window layout of
* DateTimeDemo.
*/
class DateTimeFrame extends Frame implements WindowListener, ActionListener, ItemListener, KeyListener
{
private static final String creditString
= "";
private static final String copyrightString
= "";
private static final String copyrightString2
= "";
private static final int FIELD_COLUMNS = 45;
private static final boolean DEBUG = false;
private static final int millisPerHour = 60 * 60 * 1000;
private boolean isLocalized = false;
private boolean validationMode = false;
private Locale curLocale = Locale.US;
private SimpleDateFormat format;
private Locale[] locales;
private DemoApplet applet;
// Mapping tables for displaying Rep. Cities for given timezones.
private static final int kAdjCityIndex[]
// many-to-1 mapping:
// locale index --> rep city index
= { 1,
3,
2,
4,
0,
5, // eg, Locale Index: 5 --> Rep. City index: 5
0,
0,
6};
private static final int kZoneOffsets[]
// 1-to-1 maping:
// kZoneOffsets returns the zone offset for a given rep. city index.
= { 1*millisPerHour,
-8*millisPerHour,
0,
-5*millisPerHour,
-7*millisPerHour,
-6*millisPerHour,
9*millisPerHour};
private static final String kZoneIDs[]
// 1-1 maping:
// kZoneIDs returns the zone ID string for a given rep. city index.
= {"ECT",
"PST",
"GMT",
"EST",
"MST",
"CST",
"JST"};
/**
* Constructs a new DateTimeFrame that is initially invisible.
*/
public DateTimeFrame(DemoApplet applet)
{
super("Date/Time Formatting Demo");
this.applet = applet;
addWindowListener(this);
init();
start();
}
/**
* Initializes the applet. You never need to call this directly, it
* is called automatically by the system once the applet is created.
*/
public void init()
{
// Get G7 locales only for demo purpose. To get all the locales
// supported, switch to calling TimeFormat.getAvailableLocales().
// commented. However, the mapping tables such as kAdjCityIndex
// must be expended as well.
locales = Utility.getG7Locales();
// locales = TimeFormat.getAvailableLocales();
buildGUI();
// Stick the names of the locales into the locale popup menu
Locale displayLocale = Locale.getDefault();
for (int i = 0; i < locales.length; i++) {
if (locales[i].getCountry().length() > 0) {
localeMenu.addItem( locales[i].getDisplayName() );
if (locales[i].equals(Locale.getDefault())) {
localeMenu.select(i);
}
}
}
// For starters, use the default format for the selected locale
// in the menu
setFormatFromLocale(true);
formatText();
}
//------------------------------------------------------------
// package private
//------------------------------------------------------------
void addWithFont(Container container, Component foo, Font font) {
if (font != null)
foo.setFont(font);
container.add(foo);
}
/**
* Called to start the applet. You never need to call this method
* directly, it is called when the applet's document is visited.
*/
public void start()
{
// do nothing
}
/**
* This function is called when it is necessary to display a new
* format pattern. This happens when the state of the "Localized Pattern"
* CheckBox is changed.
*/
public void handleNewFormat()
{
Utility.setText(patternText, format.toPattern() );
}
/**
* This function is called when users change the Calendar (time fields)
* validation mode. When the state of the "Validation Mode" CheckBox
* is changed, the time string in the "New Date" text field will be
* re-parsed, and the parsing result will be displayed in the "1.0 Date"
* text field.
*/
public void validationModeChanged()
{
format.setLenient(validationMode);
parseText();
}
//{{DECLARE_CONTROLS
Panel localePanel;
Panel formatPanel;
CheckboxGroup group1;
CheckboxGroup group2;
Label label1;
Label label2;
Label label3;
Label demo;
Label code;
Choice localeMenu;
Choice dateStyleMenu;
Choice timeStyleMenu;
Choice dateMenu;
Choice cityMenu;
Label dateLabel;
Label cityLabel;
TextField millisText;
Label millisLabel;
Button up;
Button down;
Label localeLabel;
Label dateStyleLabel;
Label timeStyleLabel;
TextField inputText;
TextField outputText;
Label formatLabel;
Label parseLabel;
//Button rightButton;
//Button leftButton;
TextField patternText;
Label label4;
Checkbox getDateInstance;
Checkbox getTimeInstance;
Checkbox getDateTimeInstance;
Checkbox getRoll;
Checkbox getAdd;
Checkbox getLocalized;
Checkbox getValidationMode;
//}}
Color color = Color.white;
public void buildGUI()
{
//{{INIT_CONTROLS
setBackground(color);
setLayout(new FlowLayout()); // shouldn't be necessary, but it is.
// TITLE
Panel titlePanel = new Panel();
label1=new Label("Date/Time Formatting Demo", Label.CENTER);
label1.setFont(Utility.titleFont);
titlePanel.add(label1);
// CREDITS
Panel creditPanel = new Panel();
demo=new Label(creditString, Label.CENTER);
demo.setFont(Utility.creditFont);
creditPanel.add(demo);
titlePanel.add(creditPanel);
Utility.fixGrid(titlePanel,1);
add(titlePanel);
// IO Panel
Panel topPanel = new Panel();
topPanel.setLayout(new FlowLayout());
label3=new Label("New Date", Label.RIGHT);
label3.setFont(Utility.labelFont);
topPanel.add(label3);
outputText=new TextField(FIELD_COLUMNS);
outputText.addKeyListener(this);
outputText.setFont(Utility.editFont);
topPanel.add(outputText);
label2=new Label("1.0 Date", Label.RIGHT);
label2.setFont(Utility.labelFont);
topPanel.add(label2);
// intentional use of deprecated method
inputText=new TextField(new Date().toGMTString(),FIELD_COLUMNS);
inputText.addKeyListener(this);
inputText.setFont(Utility.editFont);
topPanel.add(inputText);
millisLabel=new Label("Millis", Label.RIGHT);
millisLabel.setFont(Utility.labelFont);
topPanel.add(millisLabel);
millisText=new TextField(FIELD_COLUMNS);
millisText.addKeyListener(this);
millisText.setFont(Utility.editFont);
topPanel.add(millisText);
label4=new Label("Pattern", Label.RIGHT);
label4.setFont(Utility.labelFont);
topPanel.add(label4);
patternText=new TextField(FIELD_COLUMNS);
patternText.addKeyListener(this);
patternText.setFont(Utility.editFont);
topPanel.add(patternText);
topPanel.add(new Label(" "));
getLocalized=new Checkbox("Localized Pattern");
getLocalized.addItemListener(this);
getLocalized.setFont(Utility.labelFont);
getValidationMode=new Checkbox("Validation Mode");
getValidationMode.addItemListener(this);
getValidationMode.setFont(Utility.labelFont);
Panel checkBoxesPanel = new Panel();
checkBoxesPanel.setLayout(new GridLayout(1,2,40,0));
checkBoxesPanel.add(getLocalized);
checkBoxesPanel.add(getValidationMode);
topPanel.add(checkBoxesPanel);
Utility.fixGrid(topPanel,2);
add(topPanel);
// DATE
Panel datePanel=new Panel();
datePanel.setLayout(new FlowLayout());
group2= new CheckboxGroup();
getRoll=new Checkbox("Roll",group2, true);
getAdd=new Checkbox("Add",group2, false);
getRoll.addItemListener(this);
getAdd.addItemListener(this);
dateLabel=new Label("Date Fields");
dateLabel.setFont(Utility.labelFont);
Panel upDown = new Panel();
upDown.setLayout(new GridLayout(2,1));
// *** If the images are not found, we use the label.
up = new Button("^");
down = new Button("v");
up.addActionListener(this);
down.addActionListener(this);
up.setBackground(Color.lightGray);
down.setBackground(Color.lightGray);
upDown.add(up);
upDown.add(down);
Panel rollAddBoxes = new Panel();
rollAddBoxes.setLayout(new GridLayout(2,1));
rollAddBoxes.add(getRoll);
rollAddBoxes.add(getAdd);
Panel rollAddPanel = new Panel();
rollAddPanel.setLayout(new FlowLayout());
rollAddPanel.add(rollAddBoxes);
rollAddPanel.add(upDown);
dateMenu= new Choice();
dateMenu.addItem( "Year");
dateMenu.addItem( "Month");
dateMenu.addItem( "Day of Month");
dateMenu.addItem( "Hour of Day");
dateMenu.addItem( "Minute");
dateMenu.addItem( "Second");
dateMenu.addItem( "Millisecond");
Panel dateLM = new Panel();
dateLM.setLayout(new GridLayout(2,1));
dateLM.add(dateLabel);
dateLM.add(dateMenu);
datePanel.add(dateLM);
// CITIES
Panel citiesPanel=new Panel();
citiesPanel.setLayout(new FlowLayout());
Panel cityPanel=new Panel();
cityPanel.setLayout(new GridLayout(2,1));
cityMenu= new Choice();
cityMenu.addItemListener(this);
cityMenu.addItem( "Paris");
cityMenu.addItem( "Copenhagen");
cityMenu.addItem( "London");
cityMenu.addItem( "Washington");
cityMenu.addItem( "Toronto");
cityMenu.addItem( "Montreal");
cityMenu.addItem( "Tokyo");
cityLabel=new Label("City");
cityLabel.setFont(Utility.labelFont);
cityPanel.add(cityLabel);
cityPanel.add(cityMenu);
citiesPanel.add(cityPanel);
Panel cityDatePanel = new Panel();
cityDatePanel.add(citiesPanel);
cityDatePanel.add(datePanel);
cityDatePanel.add(rollAddPanel); // choices
Utility.fixGrid(cityDatePanel,1);
add(cityDatePanel);
// BORDER
// true means raised, false = depressed
BorderPanel borderPanel = new BorderPanel(true);
borderPanel.setBackground(Color.lightGray);
borderPanel.setLayout(null);
borderPanel.setSize(8,150);
add(borderPanel);
// LOCALE
// sets up localePanel
localePanel=new Panel();
localePanel.setLayout(new GridLayout(2,1));
localeLabel=new Label("Locale");
localeLabel.setFont(Utility.labelFont);
localeMenu= new Choice();
localeMenu.addItemListener(this);
localePanel.add("loc",localeLabel);
localePanel.add(localeMenu);
// sets up formatPanel
formatPanel=new Panel();
group1= new CheckboxGroup();
getDateInstance=new Checkbox("Date Format",group1, false);
getTimeInstance=new Checkbox("Time Format",group1, false);
getDateTimeInstance=new Checkbox("Date and Time Format",group1, true);
getDateInstance.addItemListener(this);
getTimeInstance.addItemListener(this);
getDateTimeInstance.addItemListener(this);
Panel formatButtons = new Panel();
formatButtons.setLayout(new GridLayout(3,1));
formatButtons.add(getDateInstance);
formatButtons.add(getTimeInstance);
formatButtons.add(getDateTimeInstance);
Panel dateStylePanel=new Panel();
dateStylePanel.setLayout(new GridLayout(2,1));
dateStyleLabel=new Label("Date Style");
dateStyleLabel.setFont(Utility.labelFont);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?