我在图像中显示了2个日期选择器。
首先我选择已发布,然后返回日期选择器会显示在isuue日期之后的日期。返回datepicker
时,发布到后缀日期不可见
datepicker的代码......
JLabel lblIssueDate = new JLabel("Issue Date");
lblIssueDate.setBounds(85, 47, 137, 14);
issuePanel.add(lblIssueDate);
issueDate = new JXDatePicker();
java.util.Date utilDate=new Date();
java.sql.Date sqldate=new java.sql.Date(utilDate.getTime());
issueDate.setDate(sqldate);
issueDate.setBounds(232, 44, 184, 20);
issuePanel.add(issueDate);
JLabel lblReturnDate = new JLabel("Return Date");
lblReturnDate.setBounds(85, 113, 137, 14);
issuePanel.add(lblReturnDate);
returnDate = new JXDatePicker();
returnDate.setBounds(232, 110, 184, 20);
issuePanel.add(returnDate);
答案 0 :(得分:3)
基本上,您可以通过JXMonthView
设置日期选择器的上下可接受范围。这将限制用户可以选择的日期
例如......
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.JXDatePicker;
public class TestDatePicker {
public static void main(String[] args) {
new TestDatePicker();
}
public TestDatePicker() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JXDatePicker issueDate;
private final JXDatePicker returnDate;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
JLabel lblIssueDate = new JLabel("Issue Date");
add(lblIssueDate, gbc);
issueDate = new JXDatePicker();
returnDate = new JXDatePicker();
issueDate.addPropertyChangeListener("date", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("date");
Date selectedDate = issueDate.getDate();
returnDate.getMonthView().setLowerBound(selectedDate);
}
});
java.util.Date utilDate = new Date();
java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());
issueDate.setDate(sqldate);
gbc.gridx++;
add(issueDate, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblReturnDate = new JLabel("Return Date");
add(lblReturnDate, gbc);
gbc.gridx++;
add(returnDate, gbc);
}
}
}
现在,如果您希望能够在IssueDate
之前选择日期,则可以改为使用setUpperBound
。
如果您还要排除所选日期,则需要使用Calendar
并按所需方向将其滚动一天以包含它...
我还强烈建议使用适当的布局管理器,因为它们将处理系统之间的各种差异以及字体渲染的方式等等。
Swing也可以与布局管理器一起使用,所以选择忽略它们会产生很多有趣的副作用......