//靠,程序太长,只好分开贴了
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.plaf.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
public class UTCalendar extends JComponent implements ActionListener
{
class Time extends JComponent implements ActionListener, FocusListener
{
class MinuteField extends JTextField
{
class MinuteDocument extends PlainDocument
{ public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null)
return;
str = getText(0, offs) + str + getText(offs, getLength() - offs);
int i = 0;
try
{
i = Integer.parseInt(str);
}
catch (NumberFormatException numberformatexception)
{
}
if (i < 0 || i > 59)
return;
remove(0, getLength());
str = Integer.toString(i);
if (i < 10)
str = "0" + str;
super.insertString(0, str, a);
} MinuteDocument()
{
}
} protected Document createDefaultModel()
{
return new MinuteDocument();
} public MinuteField(String t)
{
super(t);
}
} class HourField extends JTextField
{
class HourDocument extends PlainDocument
{ public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null)
return;
str = getText(0, offs) + str + getText(offs, getLength() - offs);
int i = 0;
try
{
i = Integer.parseInt(str);
}
catch (NumberFormatException numberformatexception)
{
}
if (i == 0)
i = 12;
else
if (i > 12 || i < 1)
return;
remove(0, getLength());
super.insertString(0, Integer.toString(i), a);
} HourDocument()
{
}
} protected Document createDefaultModel()
{
return new HourDocument();
} public HourField(String t)
{
super(t);
}
} class AMPMField extends JTextField
{
class AMPMDocument extends PlainDocument
{ public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null)
return;
if (str.charAt(0) == 'p' || str.charAt(0) == 'P')
str = "PM";
else
if (str.charAt(0) == 'a' || str.charAt(0) == 'A')
str = "AM";
else
return;
remove(0, getLength());
super.insertString(0, str, a);
} AMPMDocument()
{
}
} protected Document createDefaultModel()
{
return new AMPMDocument();
} public AMPMField(String t)
{
super(t);
}
} public void setHours(int hours)
{
if (hours < 12)
{
txtAoP.setText("AM");
}
else
{
txtAoP.setText("PM");
if (hours > 12)
hours -= 12;
}
txtHrs.setText(Integer.toString(hours));
} public void setMinutes(int minutes)
{
txtMns.setText(Integer.toString(minutes));
} public int getHours()
{
int hours = 0;
try
{
hours = Integer.parseInt(txtHrs.getText());
}
catch (NumberFormatException numberformatexception)
{
}
if (txtAoP.getText().charAt(0) == 'P')
{
if (hours < 12)
hours += 12;
}
else
if (hours == 12)
hours = 0;
return hours;
} public int getMinutes()
{
try
{
return Integer.parseInt(txtMns.getText());
}
catch (NumberFormatException numberformatexception)
{
return 0;
}
} public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == up && focus != null)
{
if (focus == txtAoP)
{
String t = txtAoP.getText();
txtAoP.setText(t.charAt(0) != 'P' ? "PM" : "AM");
}
else
if (focus == txtHrs)
{
int i = getHours() + 1;
if (i > 23)
i = 0;
setHours(i);
}
else
if (focus == txtMns)
{
int i = getMinutes() + 1;
if (i > 59)
i = 0;
setMinutes(i);
}
}
else
if (source == down)
if (focus != null && focus == txtAoP)
{
String t = txtAoP.getText();
txtAoP.setText(t.charAt(0) != 'P' ? "PM" : "AM");
}
else
if (focus == txtHrs)
{
int i = getHours() - 1;
if (i < 0)
i = 23;
setHours(i);
}
else
if (focus == txtMns)
{
int i = getMinutes() - 1;
if (i < 0)
i = 59;
setMinutes(i);
}
} public void focusGained(FocusEvent fe)
{
if (fe.getComponent() instanceof JTextField)
focus = fe.getComponent();
} public void focusLost(FocusEvent focusevent)
{
} private JTextField txtHrs;
private JTextField txtMns;
private JTextField txtAoP;
private JButton up;
private JButton down;
private Component focus; public Time()
{
Font font = new Font("SansSerif", 0, 10);
txtHrs = new HourField("12");
txtMns = new MinuteField("00");
txtAoP = new AMPMField("AM");
Dimension dim = txtHrs.getPreferredSize();
txtHrs.setBorder(BorderFactory.createEmptyBorder());
txtHrs.setFont(font);
txtHrs.addFocusListener(this);
txtHrs.setHorizontalAlignment(0);
txtMns.setBorder(BorderFactory.createEmptyBorder());
txtMns.setFont(font);
txtMns.addFocusListener(this);
txtMns.setHorizontalAlignment(0);
txtAoP.setBorder(BorderFactory.createEmptyBorder());
txtAoP.setFont(font);
txtAoP.addFocusListener(this);
txtAoP.setHorizontalAlignment(0);
int w = 18;
add(txtHrs);
txtHrs.setBounds(1, 1, w, dim.height);
JLabel colon = new JLabel(":");
colon.setFont(font);
int wc = colon.getPreferredSize().width;
add(colon);
colon.setBounds(w + 1, 1, wc, dim.height);
add(txtMns);
txtMns.setBounds(w + wc + 1, 1, w, dim.height);
add(txtAoP);
txtAoP.setBounds(2 * (w + wc) + 1, 1, w, dim.height);
up = new BasicArrowButton(1);
up.addActionListener(this);
add(up);
up.setBounds(3 * (w + wc) + 1, 1, dim.height / 2, dim.height / 2);
down = new BasicArrowButton(5);
down.addActionListener(this);
add(down);
down.setBounds(
3 * (w + wc) + 1,
1 + dim.height / 2,
dim.height / 2,
dim.height / 2);
setPreferredSize(
new Dimension(w * 3 + wc * 2 + 6 + dim.height / 2, dim.height + 2));
setBorder(BorderFactory.createLineBorder(Color.black));
focus = txtHrs;
}
} public UTCalendar()
{
this(true);
} public UTCalendar(boolean bPopup)
{
calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.set(11, 0);
calendar.set(12, 0);
calendar.set(13, 0);
calendar.set(14, 0);
if (bPopup)

解决方案 »

  1.   

    {
    text = new JTextField();
    text.setEditable(false);
    text.setBackground(Color.white);
    text.setText(df_all.format(calendar.getTime()));
    arrow = new BasicArrowButton(5);
    arrow.addActionListener(this);
    setLayout(null);
    add(text);
    add(arrow);
    popup = new JPopupMenu();
    createCalendar(popup);
    }
    else
    {
    setBorder(BorderUIResource.getEtchedBorderUIResource());
    setLayout(new DefaultMenuLayout(this, 1));
    createCalendar(this);
    setupCalendar();
    }
    time.setHours(calendar.get(11));
    time.setMinutes(calendar.get(12));
    } public Dimension getPreferredSize()
    {
    if (text != null)
    {
    Dimension txtDim = text.getPreferredSize();
    return new Dimension(
    (int) ((double) txtDim.width * 1.2D) + arrow.getPreferredSize().width,
    txtDim.height);
    }
    else
    {
    return new Dimension(200, 168);
    }
    } public void setEnabled(boolean enable)
    {
    super.setEnabled(enable);
    enable &= text != null;
    popup.setEnabled(enable);
    arrow.setEnabled(enable);
    text.setEnabled(enable);
    } public void setDate(Date date)
    {
    calendar.setTime(date);
    calendar.set(13, 0);
    calendar.set(14, 0);
    setupCalendar();
    time.setHours(calendar.get(11));
    time.setMinutes(calendar.get(12));
    } public Date getDate()
    {
    calendar.set(11, time.getHours());
    calendar.set(12, time.getMinutes());
    return calendar.getTime();
    } public void setBounds(int x, int y, int width, int height)
    {
    super.setBounds(x, y, width, height);
    if (popup != null)
    {
    int w = arrow.getPreferredSize().width;
    text.setSize(width - w, height);
    arrow.setSize(w, height);
    text.setLocation(0, 0);
    arrow.setLocation(width - w, 0);
    }
    } private void createCalendar(JComponent panel)
    {
    JPanel topPanel = new JPanel(new BorderLayout());
    topPanel.setBackground(Color.lightGray);
    title = new JLabel();
    title.setHorizontalAlignment(0);
    title.setFont(new Font("SansSerif", 1, 12));
    title.setForeground(Color.black);
    topPanel.add(title, "Center");
    prev = new BasicArrowButton(7);
    prev.addActionListener(this);
    topPanel.add(prev, "West");
    next = new BasicArrowButton(3);
    next.addActionListener(this);
    topPanel.add(next, "East");
    JPanel bottomPanel = new JPanel(new GridLayout(7, 7));
    bottomPanel.setBackground(Color.white);
    javax.swing.border.Border border = new EmptyBorder(1, 6, 1, 6);
    Font font = new Font("SansSerif", 0, 10);
    DateFormatSymbols dfs = new DateFormatSymbols();
    dfs.getMonths();
    String dayNames[] = dfs.getShortWeekdays();
    for (int i = 1; i <= 7; i++)
    {
    JLabel d = new JLabel(dayNames[i]);
    d.setFont(font);
    d.setForeground(Color.black);
    bottomPanel.add(d);
    } days = new JButton[6][7];
    for (int r = 0; r < 6; r++)
    {
    for (int c = 0; c < 7; c++)
    {
    JButton b = new JButton();
    b.addActionListener(this);
    b.setBorder(border);
    b.setFont(font);
    b.setBackground(Color.white);
    bottomPanel.add(b);
    days[r][c] = b;
    } } panel.add(topPanel);
    panel.add(bottomPanel);
    JPanel timePanel = new JPanel(new BorderLayout(1, 1));
    timePanel.setBackground(Color.white);
    JLabel l = new JLabel(" ");
    timePanel.add(l, "Center");
    time = new Time();
    panel.add(timePanel);
    } private void setupCalendar()
    {
    Date date = calendar.getTime();
    title.setText(df_my.format(date));
    day = calendar.get(5);
    month = calendar.get(2);
    year = calendar.get(1);
    calendar.set(5, 1);
    int firstDay = calendar.get(7);
    calendar.set(5, day);
    int maxDays = calendar.getActualMaximum(5);
    int c = firstDay - 1;
    int r = 0;
    for (int i = 0; i < c; i++)
    days[0][i].setVisible(false); for (int i = 1; i <= maxDays; i++)
    {
    label0 : {
    if (c == 7)
    {
    c = 0;
    r++;
    }
    days[r][c].setText("" + i);
    if (year == calendar.get(1))
    {
    if (month == calendar.get(2) && i == day)
    {
    days[r][c].setForeground(Color.red);
    break label0;
    }
    }
    days[r][c].setForeground(Color.black);
    }
    days[r][c].setVisible(true);
    c++;
    } while (r < 6)
    {
    while (c < 7)
    {
    days[r][c].setVisible(false);
    c++;
    } r++;
    c = 0;
    } if (popup != null)
    text.setText(df_all.format(date));
    } public void actionPerformed(ActionEvent e)
    {
    Object source = e.getSource();
    if (source == arrow)
    {
    setupCalendar();
    popup.show(this, 0, getHeight());
    }
    else
    if (source == next)
    {
    calendar.set(2, calendar.get(2) + 1);
    setupCalendar();
    }
    else
    if (source == prev)
    {
    calendar.set(2, calendar.get(2) - 1);
    setupCalendar();
    }
    else
    {
    JButton b = (JButton) source;
    day = Integer.parseInt(b.getText());
    month = calendar.get(2);
    year = calendar.get(1);
    calendar.set(5, day);
    setupCalendar();
    if (popup != null)
    popup.setVisible(false);
    fireActionPerformed();
    }
    } public void addActionListener(ActionListener l)
    {
    listenerList.add(
    class$java$awt$event$ActionListener != null
    ? class$java$awt$event$ActionListener
    : (class$java$awt$event$ActionListener =
    class1("java.awt.event.ActionListener")),
    l);
    } public void removeActionListener(ActionListener l)
    {
    listenerList.remove(
    class$java$awt$event$ActionListener != null
    ? class$java$awt$event$ActionListener
    : (class$java$awt$event$ActionListener =
    class1("java.awt.event.ActionListener")),
    l);
    } protected void fireActionPerformed()
    {
    Object listeners[] = listenerList.getListenerList();
    ActionEvent e = null;
    for (int i = listeners.length - 2; i >= 0; i -= 2)
    if (listeners[i]
    == (class$java$awt$event$ActionListener != null
    ? class$java$awt$event$ActionListener
    : (class$java$awt$event$ActionListener =
    class1("java.awt.event.ActionListener"))))
    {
    if (e == null)
    e = new ActionEvent(this, 1001, "Set");
    ((ActionListener) listeners[i + 1]).actionPerformed(e);
    } } static Class class1(String x0)
    {
    try
    {
    return Class.forName(x0);
    }
    catch (ClassNotFoundException x1)
    {
    throw new NoClassDefFoundError(x1.getMessage());
    }
    } private static DateFormat df_all = DateFormat.getDateInstance(2);
    private static SimpleDateFormat df_my = new SimpleDateFormat("MMMM yyyy");
    private JPopupMenu popup;
    private JLabel title;
    private JTextField text;
    private JButton days[][];
    private BasicArrowButton arrow;
    private BasicArrowButton prev;
    private BasicArrowButton next;
    private java.util.Calendar calendar;
    private Time time;
    private int day;
    private int month;
    private int year;
    static Class class$java$awt$event$ActionListener; /* synthetic field */  public static void main(String[] args)
    {
    JFrame frm = new JFrame();
    frm.getContentPane().add(new UTCalendar());
    frm.setBounds(100, 100, 400, 300);
    frm.setVisible(true);
    }
    }