Java--SWT下拉列表设置年月日,实现日期选择控件
1、效果图
2.代码如下:
package com.yc.base_0229;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.ibm.icu.util.Calendar;
import org.eclipse.swt.widgets.Combo;
import java.awt.Dimension;
import java.awt.Toolkit;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
/**
* 下拉列表实现年月日的选择
* @author lenovo
*
*/
public class Test09 {
protected Shell shell;
private Combo combo_year;
private Combo combo_month;
private Combo combo_day;
private int year=0;
private int month=0;
private int day=0;
Calendar c=Calendar.getInstance();//定义calendar全局变量来获取年月日
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Test09 window = new Test09();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(607, 264);
shell.setText("选择日期");
//窗口居中
Dimension ds=Toolkit.getDefaultToolkit().getScreenSize();
shell.setLocation((ds.width-shell.getSize().x)/2,(ds.height-shell.getSize().y)/2);
//年
combo_year = new Combo(shell, SWT.NONE);
combo_year.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if(!"".equals(combo_year.getText().trim())){
year=Integer.parseInt(combo_year.getText().trim());
c.set(Calendar.YEAR,year);
}
}
});
combo_year.setBounds(40, 69, 97, 32);
//设置年份
int start=1965;//设置开始年份
int end=Calendar.getInstance().get(Calendar.YEAR);//获取现在的年份
String[] y=new String[end-start+1];//定义一个String数组存储年份
for(int i=0;i<y.length;i++){
y[i]=String.valueOf(start++);
}
combo_year.setItems(y);//把String数组的值设置到combo
combo_year.select(y.length-1);//定义默认选择最后一项
//月
combo_month = new Combo(shell, SWT.NONE);
combo_month.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
//通过失去焦点获取该月的天数
month=Integer.parseInt(combo_month.getText().trim());
c.set(Calendar.MONTH,month-1);//在Calendar里面月份是从0开始的
day=c.getActualMaximum(Calendar.DAY_OF_MONTH);//获取在year年,month月下的天数
//将天数设置进combo_day下拉列表中
String []d=new String[day];
for(int i=0;i<d.length;i++){
d[i]=String.valueOf(i+1);
}
combo_day.setItems(d);
combo_day.select(0);//默认选择第一项
}
});
combo_month.setBounds(222, 69, 97, 32);
//设置月份
String []m=new String[12];
for(int i=0;i<12;i++){
m[i]=String.valueOf(i+1);
}
combo_month.setItems(m);
combo_month.select(0);//默认选择第一项
//天数
combo_day = new Combo(shell, SWT.NONE);
combo_day.setBounds(410, 69, 97, 32);
Label label = new Label(shell, SWT.NONE);
label.setBounds(143, 72, 28, 24);
label.setText("年");
Label label_1 = new Label(shell, SWT.NONE);
label_1.setText("月");
label_1.setBounds(327, 72, 28, 24);
Label label_2 = new Label(shell, SWT.NONE);
label_2.setText("日");
label_2.setBounds(513, 72, 28, 24);
Button btnOk = new Button(shell, SWT.NONE);
btnOk.setBounds(443, 148, 114, 34);
btnOk.setText("OK");
}
}
这里做的比较简单,以上是我个人自己实现的功能。