Java - 强制文本字段格式 - UX - 00:00:00;00 [英] Java - Enforce TextField Format - UX - 00:00:00;00

查看:26
本文介绍了Java - 强制文本字段格式 - UX - 00:00:00;00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看我当前应用的 UI 下图

Please see below image of the UI of my current application

目前,我正在后端执行文本字段的数据格式.如果 RegEx 匹配 (00:00:00;00),则会抛出错误.

Currently, I am enforcing Data Format of the text fields in the back end. If the RegEx does match (00:00:00;00), it will throw an error.

我的问题是从用户体验的角度来看,是否有可能使文本框的输入以不需要用户输入::;"的方式可用人物?我正在尝试实现功能,以便他们可以在框中输入 10002000,它会在框架上以视觉方式格式化为 (10:00:20;00).

My question is from a UX perspective, is it possible to make the input for the text box usable in a way that does not require the user to input the ': : ;' characters? I am trying to accomplish functionality so they could type into the box, 10002000, and it will format as (10:00:20;00) visually on the frame.

package xml.editor;
import java.awt.*;
import java.awt.event.*;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class GUI extends JPanel implements ActionListener 
{

TextField startTime, durationField;
Button save;

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Parser xmlEditor;
private String startTimeValue;
private String durationValue;


// Constructor Requires FilePath
// File Path fed from Main

public GUI(String fp){
    init(fp);
}


public void init(String fp) {

    // Create the Parser

    this.setXmlEditor(new Parser(fp));

    add(new Label("Start Time"));

    // Current Values of Fields to be changed

    startTimeValue = xmlEditor.getStartTimeValue();
    durationValue = xmlEditor.getDurationValue();

    // Text Fields start with current value
    startTime = new TextField(startTimeValue);

    add(new Label("Duration"));
    durationField = new TextField(durationValue);

    save = new Button("save");
    save.addActionListener(this);


    add(startTime);
    add(durationField);
    add(save);

}

public void displayError(String error){
    /* 
     * Error Handling UI Layout
     */

    JFrame f = new JFrame(error);
    Container contentPane = f.getContentPane();
    contentPane.add(new Label(error));

    f.pack();
    f.setVisible(true);

    //error
}

public void actionPerformed(ActionEvent e) 
{


    if(checkTextForm(startTime.getText())){
        xmlEditor.updateStartTimeValue(startTime.getText());
    }else{
        displayError("Start Time Format Error");
    }
    if(checkTextForm(durationField.getText())){
        xmlEditor.updateDurationValue(durationField.getText());
    }else{
        displayError("End Time Format Error");
    }

    if((checkTextForm(startTime.getText()) && checkTextForm(durationField.getText()))){
        System.exit(0);

    }






}

public boolean checkTextForm(String text){



    if(text.matches("\\d\\d:\\d\\d:\\d\\d;\\d\\d")){
        System.out.println("testtest");
        return true;
    }
    else{
        return false;
    }


}


public Parser getXmlEditor() {
    return xmlEditor;
}

public void setXmlEditor(Parser xmlEditor) {
    this.xmlEditor = xmlEditor;
}

}

如果有任何建议从哪里开始,或者我应该使用特定对象而不是 TextField,请告诉我.

Please let me know if there are any suggestions where to get started, or if there is a specific object I should be using instead of TextField.

谢谢,

推荐答案

如何使用 JFormattedTextFieldMaskFormatter.

JFormattedTextField formattedTextField = new JFormattedTextField();     
try {
    MaskFormatter maskFormatter = new MaskFormatter("##:##:##;##");
    maskFormatter.setPlaceholderCharacter('0');
    maskFormatter.install(formattedTextField);
} catch (ParseException e) {
    e.printStackTrace();
}

更多信息位于 http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html

演示代码:

JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

JFormattedTextField field1 = new JFormattedTextField();
field1.setColumns(10);
JFormattedTextField field2 = new JFormattedTextField();
field2.setColumns(10);
try {
    MaskFormatter mask1 = new MaskFormatter("##:##:##;##");
    mask1.setPlaceholderCharacter('0');
    mask1.install(field1);

    MaskFormatter mask2 = new MaskFormatter("##:##:##;##");
    mask2.setPlaceholderCharacter('0');
    mask2.install(field2);
} catch (ParseException e) {
    e.printStackTrace();
}
panel.add(field1);
panel.add(field2);

frame.add(panel);
frame.pack();
frame.setVisible(true);

这篇关于Java - 强制文本字段格式 - UX - 00:00:00;00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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