importjava.awt.Color;importjava.awt.Cursor;importjava.awt.event.FocusEvent;importjava.awt.event.FocusListener;importjava.awt.event.MouseAdapter;importjava.awt.event.Mouse...
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Popup;
public class A extends JFrame {
static JTextField tdate;
private boolean isShow = false;
private Popup pop;
private JTextField showDate;
private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");;
MyCalendar mycalendar;
public A() throws ParseException{
super("测试");
setSize(610, 550);
setLayout(null);
tdate = new JTextField(sdf.format(new Date()));
tdate.setBounds(180, 18, 180, 20);
Date dt= sdf.parse(tdate.getText().toString().trim());
mycalendar=new MyCalendar(tdate,dt);
tdate.setRequestFocusEnabled(true);
tdate.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
tdate.requestFocusInWindow();
}
});
tdate.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent me) {
if (tdate.isEnabled()) {
tdate.setCursor(new Cursor(Cursor.HAND_CURSOR));
tdate.setForeground(Color.RED);
}
}
public void mouseExited(MouseEvent me) {
if (tdate.isEnabled()) {
tdate.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
tdate.setForeground(Color.BLACK);
}
}
public void mousePressed(MouseEvent me) {
if (tdate.isEnabled()) {
tdate.setForeground(Color.CYAN);
if (isShow) {
mycalendar.hidePanel();
} else {
System.out.println("isshow is false");
mycalendar.showPanel(tdate);
}
}
}
public void mouseReleased(MouseEvent me) {
if (tdate.isEnabled()) {
tdate.setForeground(Color.BLACK);
}
}
});
tdate.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
mycalendar.hidePanel();
}
public void focusGained(FocusEvent e) {
}
});
add(tdate);
}
public static void main(String arg[]) throws Exception {
A g = new A();
g.setVisible(true);
}
}
展开