【问题标题】:how to read LocalTime variable in a Text widget如何在文本小部件中读取 LocalTime 变量
【发布时间】:2021-03-19 17:04:08
【问题描述】:
  import org.eclipse.swt.widgets.Text;
  ...
  private Text textora;
   ...
  LocalTime ora = textora.getLocalTime();

我无法从文本格式中读取 LocalTime 变量以供输入

错误:

Multiple markers at this line
- The method getLocalTime() is undefined for the 
 type Text
- The method getTime() is undefined for the type 
 Text

【问题讨论】:

  • Text 只是纯文本,只有 getText 方法。您必须解析该文本才能获得日期。
  • 如果您希望用户输入日期,您可能还想查看 SWT DateTime 控件。
  • 我想要的不是日期,而是时间
  • DateTime 表示日期或时间。对于Text,您仍然需要解析时间。

标签: java eclipse swt


【解决方案1】:

SWT Text 控件只是纯文本,只有一个getText 方法来获取输入的字符串。要将文本转换为LocalTime,您必须解析文本:

String timeStr = textora.getString();

LocalTime time = LocalTime.parse(timeStr);

如果字符串不是有效时间,parse 将抛出 DateTimeParseException 异常。

您也可以使用 SWT DateTime 控件来允许输入时间:

import org.eclipse.swt.widgets.DateTime;

...

DateTime dt = new DateTime(parent, SWT.TIME);

...

LocalTime time = LocalTime.of(dt.getHours(), dt.getMinutes(), dt.getSeconds());

【讨论】:

    猜你喜欢
    • 2015-12-13
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2018-01-23
    相关资源
    最近更新 更多