swing 实现点击文本框弹出选择日期的界面

2024-12-21 21:12:04
推荐回答(3个)
回答1:

没有实现的类,可以自己写一个。
这有一个类。你可以试一下:
package com;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.DocumentListener;
import javax.swing.text.DateFormatter;

import com.shiji.pastoral.common.arithmetic.CalculateDate;
import com.shiji.pastoral.common.arithmetic.GlobalStatus;
import com.shiji.pastoral.server.common.method.DebugPrint;

/**
* 日期控件
*
* @author 2008-5-23 上午11:38:14
*/
public class DatePanel extends JPanel {

private CalculateDate cal = new CalculateDate();

public JFormattedTextField hotelFormattedTextField;

public JButton dateButton;

private Window dialog;

private String labelName; // 每个Date的名称都不一样

private int x; // 跳出日期控件的x轴

private int y; // 跳出日期控件的y轴

public static String initDate = "1900-01-01";// CalculateDate.dateToString(CalculateDate.NULLDATE);

// // 系统默认Date

private boolean judge;

private String type = "-";

private static String time;

private JRadioButton enableRadioButton;

private boolean allowEmptyStr = true;// 是否允许文本框为空

public FocusAdapter f;

/**
* 系统初始化构造函数
*
* @author 2008-7-22 上午11:38:27
*
*/
public DatePanel() {
this(false);
}

public DatePanel(JDialog dialog) {
this(dialog, true);
}

public DatePanel(boolean showRadioBox) {
super();
dialog = DFrame.frame;
initGUI();
enableRadioButton.setVisible(showRadioBox);
}

/**
* 旧的方法,不建议使用,最终还是要删除的。。。by Jimmy
*
* @deprecated
* @param dialog
* @param labelName
* @param judge
*/
public DatePanel(Window dialog, String labelName, boolean judge,
boolean showRadioBox) {
super();
this.dialog = dialog;
this.labelName = labelName;
this.judge = judge;
initGUI();
enableRadioButton.setVisible(showRadioBox);
}

public DatePanel(Window dialog, String labelName, boolean judge) {
this(dialog, labelName, judge, false);
}

public DatePanel(Window dialog, boolean judge) {
super();
this.dialog = dialog;
this.labelName = "";
this.judge = judge;
initGUI();
enableRadioButton.setVisible(false);
}

private void initGUI() {

this.setPreferredSize(new java.awt.Dimension(139, 21));
BorderLayout thisLayout = new BorderLayout();
this.setLayout(thisLayout);
this.add(getDateButton(), BorderLayout.EAST);
this.add(getHotelFormattedTextField(), BorderLayout.CENTER);
this.add(getEnableRadioButton(), BorderLayout.WEST);

}

private JFormattedTextField getHotelFormattedTextField() {
// MaskFormatter mf = null;
// try {
// mf = new MaskFormatter("####-##-##");
// // mf.setValueContainsLiteralCharacters(false);
// } catch (Exception e) {
// e.printStackTrace();
// }

/*
* hotelFormattedTextField = new JFormattedTextField();
* hotelFormattedTextField.setDocument(new DateLimited(
* hotelFormattedTextField, new SimpleDateFormat("yyyy" + type + "MM" +
* type + "dd"))); hotelFormattedTextField.setText(initDate);
* hotelFormattedTextField.addFocusListener(new FocusAdapter() { public
* void focusLost(FocusEvent evt) { if
* (hotelFormattedTextField.getText().indexOf(" ") != -1 && judge) {
* JOptionPane.showMessageDialog(dialog, "Date is null!");
* hotelFormattedTextField.setText(" " + type + " " + type + " ");
* hotelFormattedTextField.requestFocus(); } } });
*/
// if(dateFormatType.trim().isEmpty()||dateFormatType==null)
// dateFormatType = CalculateDate.dateFormatType;// "yyyy-MM-dd";
DateFormat dateFormat = new SimpleDateFormat(
CalculateDate.dateFormatType);
DateFormatter df = new DateFormatter(dateFormat);
hotelFormattedTextField = new JFormattedTextField(df);
hotelFormattedTextField
.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
hotelFormattedTextField.setBorder(new SoftBevelBorder(
BevelBorder.LOWERED, null, null, null, null));
hotelFormattedTextField.setFont(new java.awt.Font(null, 1, 12));
hotelFormattedTextField.setValue(CalculateDate.HotelDate);
if (allowEmptyStr)
hotelFormattedTextField.setText("");// 设置为空时间
else
hotelFormattedTextField.setValue(CalculateDate.HotelDate);
hotelFormattedTextField.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent evt) {
if (hotelFormattedTextField.getText().trim().isEmpty()
&& allowEmptyStr) {
// DebugPrint.onePrint("现在输入的是空串");
hotelFormattedTextField
.setFocusLostBehavior(JFormattedTextField.PERSIST);// 不执行任何操作,不获取新的
// AbstractFormatter
// 也不更新该值。
} else {
hotelFormattedTextField
.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);// 恢复显示以匹配
// getValue,这可能丢失当前的编辑内容
if (hotelFormattedTextField.isEditValid()) {
// DebugPrint.onePrint("现在输入的是有效值");
hotelFormattedTextField.setFocusLostBehavior(JFormattedTextField.PERSIST);
if (CalculateDate.isNullDate(getDate())) {
// DebugPrint.onePrint("现在输入的时间是空时间");
hotelFormattedTextField.setText("");
}else if(getDate().compareTo(CalculateDate.NULLDATE)<0){
DefinedOptionPane.showMessageDialog(null,GlobalStatus.lang("Invalid Date can't less than 1900-01-01"), 1);
setDate(CalculateDate.getHotelDate(GlobalStatus.userStructure.getHotelID()));
}else if(getDate().compareTo(CalculateDate.MAXDATE)>0){
DefinedOptionPane.showMessageDialog(null,GlobalStatus.lang("Invalid Date can't more than 3000-12-31"), 1);
setDate(CalculateDate.getHotelDate(GlobalStatus.userStructure.getHotelID()));
}
} else {
if (!hotelFormattedTextField.getText().trim().isEmpty()) {
// DebugPrint.onePrint("现在输入的是无法效值");
DefinedOptionPane
.showMessageDialog(null,
GlobalStatus.lang("Invalid Date: ")
+ hotelFormattedTextField
.getText(), 1);
// hotelFormattedTextField.setText(initDate);
}
}

}
}
});

return hotelFormattedTextField;
}

private int getX(MouseEvent evt) {
int x1 = evt.getXOnScreen() - evt.getX()
- hotelFormattedTextField.getWidth();
int x2 = DFrame.width - CalendarDialog.width;
DebugPrint.jimmyPrintMsg(x1, x2);
return x1 > 0 ? Math.min(x1, x2) : 0;
}

private int getY(MouseEvent evt) {
int y1 = evt.getYOnScreen() + dateButton.getHeight() - evt.getY();
int y2 = DFrame.height - CalendarDialog.height;
if (y1 > y2)
y1 = evt.getYOnScreen() - evt.getY() - CalendarDialog.height;
return y1;
}

private JButton getDateButton() {
if (dateButton == null) {
dateButton = new JButton();
dateButton.setIcon(new ImageIcon(getClass().getClassLoader()
.getResource("img/down.png")));
dateButton.setPreferredSize(new java.awt.Dimension(20, 21));
dateButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
x = getX(evt);
y = getY(evt);
}
});
dateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
time = CalculateDate.dateToString(new CalendarDialog(
dialog, x, y, getDate()).getDate());
} catch (RuntimeException e) {
time = CalculateDate.dateToString(new CalendarDialog(
dialog, x, y, new Date()).getDate());
}
hotelFormattedTextField.setText(time.length() == 10
? time
: initDate);
hotelFormattedTextField.requestFocus();
time = initDate;
}
});
}
return dateButton;
}

/**
* 设置日期..不推荐用这个。。没测试过one
*
* @author 2008-8-5 上午10:37:33
*
* @param date
*/
public void setDate(String date) {
// setDate(new CalculateDate().stringToDate(date));
// hotelFormattedTextField.setValue(CalculateDate.dateFormat(new
// CalculateDate().stringToDate(date), dateFormatType));
if (date.trim().isEmpty())
hotelFormattedTextField.setText("");
else
setDate(CalculateDate.stringToDateExp(date));
}

public String getText() {
return CalculateDate.dateToString(getDate());
}

/**
* 获取日期
*
* @author 2008-8-5 上午10:37:46
*
* @return
*/
public Date getDate() {
// Date returndate;
// returndate = new CalculateDate().stringToDate(hotelFormattedTextField
// .getText());
if (hotelFormattedTextField.getText().trim().isEmpty()) {
// DebugPrint.onePrint("现在的日期是" + cal.stringToDate("1900-01-01"));
return cal.stringToDate("1900-01-01");
} else {
// DebugPrint.onePrint("现在的日期是"+
// cal.stringToDate(hotelFormattedTextField.getText()));
return cal.stringToDate(hotelFormattedTextField.getText());
}
}

/**
*
*
* @author Administrator 2008-9-16 下午02:33:24
*
* @param date
*/
public void setDate(Date date) {
// hotelFormattedTextField.setText(date != null ? new CalculateDate()
// .dateToString(date) : initDate);
if (CalculateDate.isNullDate(date))
hotelFormattedTextField.setText("");
else
hotelFormattedTextField.setText(CalculateDate.dateToString(date));
}

/**
* 设置文本框是否能为空
*
* @author One 2009-6-4上午10:09:14
*
* @param allow
*/
public void setAllowEmptyStr(boolean allow) {
allowEmptyStr = allow;
}

/**
* 设置错误日期的颜色
*
* @author One 2009-6-10下午04:27:58
*
* @param error
*/
public void setColour(boolean error) {
if (error)
hotelFormattedTextField.setForeground(Color.RED);
else
hotelFormattedTextField.setForeground(Color.BLACK);

}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
hotelFormattedTextField.setEnabled(enabled);
dateButton.setEnabled(enabled);
enableRadioButton.setSelected(enabled);
}

private JRadioButton getEnableRadioButton() {
if (enableRadioButton == null) {
enableRadioButton = new JRadioButton();
enableRadioButton.setSelected(true);
enableRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setEnabled(enableRadioButton.isSelected());
}
});
}
return enableRadioButton;
}

public static void main(String[] args) {
JDialog frame = new JDialog();
frame.getContentPane().add(new DatePanel());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

/**
* @author rainbow 2009-7-14 下午12:01:55
* @param listener
*
*/
public void addDocumentListener(DocumentListener listener) {
hotelFormattedTextField.getDocument().addDocumentListener(listener);
}

/**
* @author rainbow 2009-7-14 下午12:01:58
* @param listener
*
*/
public void removeDocumentListener(DocumentListener listener) {
hotelFormattedTextField.getDocument().removeDocumentListener(listener);
}
}

回答2:

JOptionPane 用这个类试试.

回答3:

org.jdesktop.swingx.JXDatePicker
搜一下swingx就可以找到这个包了