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");

    }    
}
 

这里做的比较简单,以上是我个人自己实现的功能。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用 SWT 库编写的应用程序窗口代码,其中包括班级、姓名和学号的文本框: ```java import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class ApplicationWindow { private Text textClass; private Text textName; private Text textNumber; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Application Window"); shell.setLayout(new GridLayout(2, false)); // 添加班级标签和文本框 Label labelClass = new Label(shell, SWT.NONE); labelClass.setText("Class:"); textClass = new Text(shell, SWT.BORDER); textClass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); // 添加姓名标签和文本框 Label labelName = new Label(shell, SWT.NONE); labelName.setText("Name:"); textName = new Text(shell, SWT.BORDER); textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); // 添加学号标签和文本框 Label labelNumber = new Label(shell, SWT.NONE); labelNumber.setText("Number:"); textNumber = new Text(shell, SWT.BORDER); textNumber.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); // 添加确认按钮 Button button = new Button(shell, SWT.PUSH); button.setText("OK"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { String classStr = textClass.getText(); String nameStr = textName.getText(); String numberStr = textNumber.getText(); System.out.println("Class: " + classStr + ", Name: " + nameStr + ", Number: " + numberStr); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } ``` 上述代码创建了一个基本的 SWT 应用程序窗口,其中包括班级、姓名和学号的文本框以及一个确认按钮。在单击确认按钮后,将在控制台上显示输入文本框中的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值