限制用户在JTextField中输入时间的最佳方式 [英] Best way to constrain user to enter a time in a JTextField

查看:206
本文介绍了限制用户在JTextField中输入时间的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个java应用程序中,有一个字段,用户应该输入一个时间。我知道,我可以简单地把它保持为一个正常的 JTextField ,在验证时我可以检查值等等...



但我认为有一个比这更好的方法。



例如,如果这是一个日期的问题,那么 JXDatePicker (附带 swingx )可以非常方便地使用。最终,用户选择保证是一个日期。



同样,也必须有更好的时间选择方法。如果您知道更好的解决方案,有人会分享您的知识。



任何想法都赞赏。谢谢!

解决方案

你可以使用一个 JFormattedTextField 看看下面的例子,这将创建一个 JFormattedTextField ,它将只接受数字,并将它们放在XXhXXminXXs 中,然后添加一个 ActionListener JFormattedTextField ,并尝试在 ENTER 时解析为有效的时间对象,现在我只显示使用 JFormattedTextField ActionListener 我没有尝试转换或转换和解析有效时间: / p>

  import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.MaskFormatter;

public class FormattedTextFieldExample {

public FormattedTextFieldExample(){
initComponents();
}

private void initComponents(){
JFrame frame = new JFrame(JFormattedTextField Example);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MaskFormatter mask = null;
try {
mask = new MaskFormatter(## h ## min ## s); //数字值为
mask.setPlaceholderCharacter('#');
} catch(ParseException e){
e.printStackTrace();
}

//
//创建一个接受有效时间的格式化文本字段。
//
final JFormattedTextField timeField = new JFormattedTextField(mask);

//添加ActionListener进行输入时
timeField.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
Object source = ae.getSource();
if(source == timeField){
//解析到这里的有效时间
System.out.println(timeField.getText() );
}
}
});

frame.add(timeField);

frame.pack();
frame.setVisible(true);
}

public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run (){
new FormattedTextFieldExample();
}
});
}
}


In one of my java applications, there is a field where the user is supposed to enter a time. I know, I can simply keep it as a normal JTextField and at the time of validation I can check the value and so on...

But I think there is a better way than that..

For example, if this is a matter with a date, then JXDatePicker (which comes with swingx) can be used very handily. Eventually, the users selection is guaranteed to be a date.

Likewise, there must be a better way for time selection too. Will someone share your knowledge if you know a better solution.

Any thought is appreciated. Thank you!

解决方案

you could use a JFormattedTextField have a look at the example below, this will create a JFormattedTextField which will accept only numbers and put them in the form XXhXXminXXs then add a ActionListener to the JFormattedTextField and in that attempt to parse to a valid time object when ENTER for now I only show the use of the JFormattedTextField and the ActionListener I didn't attempt to convert or any converting and parsing for valid time:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.MaskFormatter;

public class FormattedTextFieldExample {

    public FormattedTextFieldExample() {
        initComponents();
    }

    private void initComponents() {
        JFrame frame = new JFrame("JFormattedTextField Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("##h##min##s");//the # is for numeric values
            mask.setPlaceholderCharacter('#');
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //
        // Create a formatted text field that accept a valid time.
        //
        final JFormattedTextField timeField = new JFormattedTextField(mask);

        //Add ActionListener for when enter is pressed
        timeField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                Object source = ae.getSource();
                if (source == timeField) {
                    //parse to a valid time here
                    System.out.println(timeField.getText());
                }
            }
        });

        frame.add(timeField);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FormattedTextFieldExample();
            }
        });
    }
}

这篇关于限制用户在JTextField中输入时间的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆