Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SWT DateTime中没有默认日期_Java_User Interface_Datetime_Swt - Fatal编程技术网

Java SWT DateTime中没有默认日期

Java SWT DateTime中没有默认日期,java,user-interface,datetime,swt,Java,User Interface,Datetime,Swt,我使用的是SWT日期时间组件。实例化时,它将当前日期设置为默认选择。我怎样才能防止这种情况 我希望根本不选择任何日期 谢谢 Patrick您必须手动将实例的字段设置为0或null(无论什么合适)。您还可以实现自己的NodeTime对象(使用null对象模式)来完成同样的任务。我很想用null来表示无时间,但有什么原因不能这样做吗?SWT DateTime控件根本不支持这一点 我从项目中推荐。如果这对任何人仍然有用,我也有同样的问题,这意味着UI上的字段必须显示日期或空值:因为未选择的日期也是有效

我使用的是SWT日期时间组件。实例化时,它将当前日期设置为默认选择。我怎样才能防止这种情况

我希望根本不选择任何日期

谢谢
Patrick

您必须手动将实例的字段设置为0或null(无论什么合适)。您还可以实现自己的NodeTime对象(使用null对象模式)来完成同样的任务。我很想用null来表示无时间,但有什么原因不能这样做吗?

SWT DateTime控件根本不支持这一点


我从项目中推荐。

如果这对任何人仍然有用,我也有同样的问题,这意味着UI上的字段必须显示日期或空值:因为未选择的日期也是有效的输入。 虽然SWT DateTime必须显示某种类型的日期,但通过简单地制作标签和按钮来引入另一个间接层次并不是一个问题——也像DateTime:然后按钮在单独的模式窗口中调用DateTime。用户做出选择后,我们将结果写入应用程序窗口中的标签。您还可以在模式窗口中添加另一个按钮,并将其称为NONE(无)。如果用户单击“无”,则清除应用程序中的标签字段。 您将看到,我首先从标签中提取日期的当前值,以便在模式对话框中初始化DateTime控件。通过这种方式,它的行为就像一个新的复合控件,尽管我承认如果您需要重复多次,它会有点尴尬。例如:

private Button buttonDeadlineDate;
private Label labelDeadlineDate;

// ... then define your "composite" control:

lblNewLabel_5 = new Label(group_2, SWT.NONE);
lblNewLabel_5.setBounds(10, 14, 50, 17);
lblNewLabel_5.setText("Deadline:");

// We make our own composite date control out of a label and a button
// and we call a modal dialog box with the SWT DateTime and
// some buttons.
labelDeadlineDate = new Label(group_2, SWT.BORDER | SWT.CENTER);
labelDeadlineDate.setBounds(62, 10, 76, 20);
// Note that I use the strange font DokChampa because this was the only way to get a margin at the top.
labelDeadlineDate.setFont(SWTResourceManager.getFont("DokChampa", 8, SWT.NORMAL));
labelDeadlineDate.setBackground(SWTResourceManager.getColor(255, 255, 255));  // so it does appear editable
buttonDeadlineDate = new Button (group_2, SWT.NONE);
buttonDeadlineDate.setBounds(136, 11, 20, 20); // x - add 74, y - add 1 with respect to label


// ... And later we have the call-back from the listener on the little button above:

    //========================================
    // Deadline Date
    //========================================

    buttonDeadlineDate.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {

        // Define the dialog shell.
        // Note: DIALOG_TRIM = TITLE | CLOSE | BORDER (a typical application dialog shell)
        final Shell dialog = new Shell (shlTaskScheduler, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                dialog.setText("Enter deadline date (NONE for none)");

        //========================================
        // Position and size the dialog (relative to the application).
        // could have probably also used a single call to dialog.setBounds()
        // instead of calling setLocation() and setSize().
        //========================================
        Point myPoint = new Point(0,0);
        myPoint = shlTaskScheduler.getLocation();
        myPoint.x +=80; // myPoint.x +=30;
        myPoint.y +=320; // myPoint.y +=350;
        dialog.setLocation(myPoint);
        dialog.setSize(270, 220);

        dialog.setLayout (null);

        //========================================
        // Define dialog contents
        //========================================

        // Make controls final they it can be accessed from the listener.

        final DateTime DTDeadlineDate;
        DTDeadlineDate = new DateTime(dialog, SWT.BORDER | SWT.CALENDAR | SWT.DROP_DOWN);
        DTDeadlineDate.setBounds(10, 10, 175, 175);

        final Button buttonNone = new Button (dialog, SWT.PUSH);
        buttonNone.setText ("NONE");
        buttonNone.setBounds(200, 35, 55, 25);

        final Button buttonOK = new Button (dialog, SWT.PUSH);
        buttonOK.setText ("OK");
        buttonOK.setBounds(200, 85, 55, 25);

        //========================================
        // Initialize the DateTime control to
        // the date displayed on the button or today's date.
        //========================================

        // Get the deadline from the main application window
        String newDeadlineDateString = (labelDeadlineDate.getText().toString());
        myLogger.i (className, "got deadline from main application window as " + newDeadlineDateString);

        // If deadline date found, use it to initialize the DateTime control
        // else the DateTime control will initialize itself to the current date automatically.  
        if ((newDeadlineDateString.length() == 10) // probably unnecessary test
        && (isThisDateValid(newDeadlineDateString, "yyyy-MM-dd"))) {

            // parse and extract components
            try {
                String tmpYearString= newDeadlineDateString.substring(0,4);
                String tmpMoString = newDeadlineDateString.substring(5,7);
                String tmpDayString = newDeadlineDateString.substring(8,10);

                int tmpYearInt = Integer.parseInt(tmpYearString);
                int tmpMoInt = Integer.parseInt(tmpMoString);
                int tmpDayInt = Integer.parseInt(tmpDayString);

                DTDeadlineDate.setYear(tmpYearInt);
                DTDeadlineDate.setMonth(tmpMoInt - 1); // the control counts the months beginning with 0! - like the calendar
                DTDeadlineDate.setDay(tmpDayInt);

            } catch(NumberFormatException f) {
                // this should not happen because we have a legal date
                myScreenMessage.e(className, "Error extracting deadline date from screen <" + newDeadlineDateString + ">. Ignoring");
            }
        } else if (newDeadlineDateString.length() > 0) {
            myLogger.w (className, "Illegal current deadline date value or format <" + newDeadlineDateString + ">. Ignoring.");
            // no need to do anything, as the control will initialize itself to the current date
        } else {
            // no need to do anything, as the control will initialize itself to the current date
        }

        //========================================
        // Set up the listener and assign it to the OK and None buttons.
        // Note that the dialog has not been opened yet, but this seems OK.
        //
        // Note that we define a generic listener and then associate it with a control.
        // Thus we need to check in the listener, which control we happen to be in.
        // This is a valid way of doing it, as an alternative to using
        //      addListener() or
        //      addSelectionListener()
        // for specific controls.
        //========================================

        Listener listener = new Listener () {
            public void handleEvent (Event event) {

                if (event.widget == buttonOK) {

                    int newDeadlineDay = DTDeadlineDate.getDay();
                    int newDeadlineMonth = DTDeadlineDate.getMonth() + 1; // the returned month will start at 0
                    int newDeadlineYear = DTDeadlineDate.getYear();

                    String selectedDeadlineDate = String.format ("%04d-%02d-%02d", newDeadlineYear, newDeadlineMonth, newDeadlineDay);
                    if (isThisDateValid(selectedDeadlineDate, "yyyy-MM-dd")) {
                        labelDeadlineDate.setText(selectedDeadlineDate);
                    } else {
                        // This is strange as the widget should only return valid dates...
                        myScreenMessage.e(className, "Illegal deadline date selected: resetting to empty date");
                        labelDeadlineDate.setText("");
                    }

                } else if (event.widget == buttonNone) {
                    // an empty date is also an important value
                    labelDeadlineDate.setText("");
                } else {
                    // this should not happen as there are no other buttons on the dialog
                    myLogger.e(className, "Unexpected widget state: ignoring");
                } 

                // once a button is pressed, we close the dialog    
                dialog.close ();
            }
        };

        // Still need to assign the listener to the buttons         
        buttonOK.addListener (SWT.Selection, listener);
        buttonNone.addListener (SWT.Selection, listener);

        //========================================
        // Display the date dialog.
        //========================================
        dialog.open ();

        //========================================
        // If you need to do this - you can wait for user selection before returning from this listener.
        // Note that this wait is not necessary so that the above button listeners
        // can capture events, but rather so that we do not continue execution and end this
        // function call before the user has made a date selection clicked on a button.
        // Otherwise we would just go on.

        while (!dialog.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        ...

    }
    });
private按钮deadlinedate;
自有标签LabelDadlineDate;
// ... 然后定义“复合”控件:
lblNewLabel_5=新标签(组_2,SWT.NONE);
lblNewLabel_5.立根(10,14,50,17);
lblNewLabel_5.setText(“截止日期:”);
//我们用标签和按钮制作自己的复合日期控件
//我们调用一个带有SWT DateTime和
//一些按钮。
labelDeadlineDate=新标签(组2,SWT.BORDER | SWT.CENTER);
LabeldadlineDate.立根(62,10,76,20);
//请注意,我使用了奇怪的字体DokChampa,因为这是在顶部获得边距的唯一方法。
labeldadlinedate.setFont(SWTResourceManager.getFont(“DokChampa”,8,SWT.NORMAL));
labeldadlinedate.setBackground(SWTResourceManager.getColor(255、255、255));//所以它看起来是可编辑的
buttonDeadlineDate=新按钮(第2组,SWT.NONE);
按钮deadlinedate.setBounds(136,11,20,20);//x-添加74,y-添加1关于标签
// ... 稍后,我们会在上面的小按钮上收到侦听器的回电:
//========================================
//截止日期
//========================================
buttonDeadlineDate.addSelectionListener(新建SelectionAdapter()){
@凌驾
公共无效WidgeSelected(SelectionEvent e){
//定义对话框外壳。
//注意:DIALOG_TRIM=TITLE | CLOSE | BORDER(典型的应用程序对话框外壳)
最终外壳对话框=新外壳(shlTaskScheduler,SWT.dialog|TRIM | SWT.APPLICATION|u model);
setText(“输入截止日期(无表示无)”);
//========================================
//对话框的位置和大小(相对于应用程序)。
//可能还使用了对dialog.setBounds()的单个调用
//而不是调用setLocation()和setSize()。
//========================================
点myPoint=新点(0,0);
myPoint=shlTaskScheduler.getLocation();
myPoint.x+=80;//myPoint.x+=30;
myPoint.y+=320;//myPoint.y+=350;
对话框。设置位置(myPoint);
对话框。设置大小(270、220);
dialog.setLayout(空);
//========================================
//定义对话框内容
//========================================
//将控件设为最终控件,以便从侦听器访问。
最终日期时间DTDeadlineDate;
DTDeadlineDate=新日期时间(对话框,SWT.BORDER | SWT.CALENDAR | SWT.DROP|u下拉菜单);
DTDeadlineDate.setBounds(10,10,175,175);
最终按钮按钮一=新按钮(对话框,SWT.PUSH);
buttonNone.setText(“无”);
按钮一。立根(200,35,55,25);
最终按钮按钮OK=新按钮(对话框,SWT.PUSH);
按钮OK.setText(“确定”);
按钮立根(200,85,55,25);
//========================================
//将DateTime控件初始化为
//按钮上显示的日期或今天的日期。
//========================================
//从主应用程序窗口获取截止日期
字符串newDeadlineDateString=(labelDeadlineDate.getText().toString());
i(className,“从主应用程序窗口获取的截止日期为”+newDeadlineDateString);
//如果找到截止日期,则使用它初始化DateTime控件
//否则DateTime控件将自动初始化为当前日期。
if((newDeadlineDateString.length()==10)//可能不需要测试
&&(isThisDateValid(newDeadlineDateString,“yyyy-MM-dd”)){
//解析和提取组件
试一试{
String tmpYearString=newDeadlineDateString.substring(0,4);
String tmpMoString=newDeadlineDateString.substring(5,7);
String tmpDayString=newDeadlineDateString.substring(8,10);
int-tmpYearInt=Integer.parseInt(tmpYearString);
int-tmpMoInt=Integer.parseInt(tmpMoString);
int-tmpDayInt=Integer.parseInt(tmpDayString);
DTDeadlineDate.setYear(tmpYearInt);
DTDeadlineDate.setMonth(tmpMoInt-1);//该控件计算从0开始的月份,就像日历一样
DTDeadlineDate.setDay(tmpDayInt);
}捕获(数字格式)
private boolean isModified = false;

selectDate = new DateTime(this, SWT.DATE | SWT.DROP_DOWN);

    SelectionListener selListener = new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            isModified = true;
        }
    };

    selectDate.addSelectionListener(selListener);