JFormattedTextField :输入持续时间值

编程入门 行业动态 更新时间:2024-10-25 17:29:49
本文介绍了JFormattedTextField :输入持续时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用 JFormattedTextField 来允许用户将时间 duration 值输入到表单中.示例有效值为:

I want to use a JFormattedTextField to allow the user to input time duration values into a form. Sample valid values are:

2h 30m72h 15m6h0h

但是我在这方面取得的成功有限.有人可以建议如何做到这一点吗?如果使用 JTextField 也可以实现此结果,我就可以了.

However I am having limited success with this. Can some one please suggest how this can be accomplished? I am OK if this result can be achieved using a JTextField as well.

谢谢!

如果值得的话,这是我目前的尝试:

If it is worth anything, here's my current attempt:

mFormattedText.setFormatterFactory( new DefaultFormatterFactory( new DateFormatter( new SimpleDateFormat("H mm"))));

这有点像:

  • 我无法让 h 和 m 显示为纯文本(我尝试转义) *
  • 小时数有最大值
  • I cannot get h and m to appear as plain text (I tried escaping) *
  • The number of hours has a max

*:见@nanda 的回答

*: See @nanda's answer

推荐答案

代码:

public static void main(String[] args) { JFrame jFrame = new JFrame(); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setLayout(new BorderLayout()); jFrame.setPreferredSize(new Dimension(500, 500)); final JFormattedTextField comp = new JFormattedTextField(); comp.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(new SimpleDateFormat( "H'h' mm'm'")))); comp.setValue(Calendar.getInstance().getTime()); comp.addPropertyChangeListener("value", new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { System.out.println(comp.getValue()); } }); jFrame.getContentPane().add(comp, BorderLayout.CENTER); jFrame.pack(); jFrame.setVisible(true); }

更多推荐

JFormattedTextField :输入持续时间值

本文发布于:2023-10-30 22:56:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:持续时间   JFormattedTextField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!