我想使它具有适应性,例如2月在2000年有29天,但是到2001年它将变为28天。
我想在使用JComboBox
如何使用组合框制作活动日历?
JComboBox jcb,jcb1,jcb2;
db(){
JFrame jf = new JFrame("register");
jf.setLayout=(new FlowLayout());
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String aa1="0"+1+"-"+"0"+2+"-"+2000;
date = LocalDate.parse(aa1,dtf);
Integer day[] = new Integer[date.lengthOfMonth()];
for(int i=0;i<date.lengthOfMonth();i++) {
day[i]=i+1;
}
jcb = new JComboBox<>(day);
Integer month[] = new Integer[12];
for(int i=0;i<12;i++) {
month[i]=i+1;
}
jcb1 = new JComboBox<>(month);
Integer year[] = new Integer[80];
for(int i=0;i<80;i++) {
year[i]=i+1940;
}
jcb2 = new JComboBox<>(year);
jf.add(jcb);
jf.add(jcb1);
jf.add(jcb2);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(300,300,300,300);
jf.setVisible(true);
}
答案 0 :(得分:1)
正如@Andrew Thompson在评论中提到的那样,用于日期选择的JComboBoxes不是一个好主意。看看Which one is the best Java datepicker.
但是,如果您仍然坚持使用组合框,则要实现所需的功能,就必须在月/年组合框中添加public class Test extends JFrame implements ActionListener {
private JComboBox<Integer> yearBox;
private JComboBox<Integer> monthBox;
private JComboBox<Integer> dayBox;
public Test() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
yearBox = new JComboBox<>();
for (int i = 1940; i <= LocalDateTime.now().getYear(); i++) {
yearBox.addItem(i);
}
yearBox.addActionListener(this);
monthBox = new JComboBox<>();
for (int i = 1; i <= 12; i++) {
monthBox.addItem(i);
}
monthBox.addActionListener(this);
dayBox = new JComboBox<>();
add(new JLabel("year:"));
add(yearBox);
add(new JLabel("month:"));
add(monthBox);
add(new JLabel("day:"));
add(dayBox);
//Start with current year selected
yearBox.setSelectedIndex(yearBox.getItemCount() - 1);
setSize(400, 400);
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
int year = (int) yearBox.getSelectedItem();
int month = (int) monthBox.getSelectedItem();
int daysInThisMonth = LocalDate.of(year, month, 1).lengthOfMonth();
int previousSelection = dayBox.getSelectedItem() != null ? (int) dayBox.getSelectedItem() : 1;
dayBox.removeAllItems();
for (int i = 1; i <= daysInThisMonth; i++) {
dayBox.addItem(i);
}
if (previousSelection >= dayBox.getItemCount())
//select last index of month
dayBox.setSelectedIndex(dayBox.getItemCount() - 1);
else
dayBox.setSelectedItem(previousSelection);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
}
,以便重新确定天数的模型(项目)组合框。
看看这个例子:
declare cur cursor for Select TableName From TABLE_LIST
declare @tablename nvarchar(max)
declare @sqlstring nvarchar(max)
open cur
fetch next from cur into @tablename
while @@fetch_status=0
begin
set @sqlstring = 'SELECT ''' + @tablename + ''' AS ''TABLE'', [LAST_REFRESHED] FROM ' + @tablename
exec sp_executesql @sqlstring
fetch next from cur into @tablename
end
close cur
deallocate cur
;