如何制作两个字段的条件? [英] How to make a condition of two fields?

查看:70
本文介绍了如何制作两个字段的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,我需要添加几个条件来验证当前代码中的两个字段.

  1. Min 字段不得超过 Max 字段.最小值<最大值/5000<4000 禁止进入.
  2. Max 字段不得小于 Min 字段.Max
  3. Min 字段不应超过 Max 字段的间隔,可以输入少 500 个单位.例如:Min 4500 和 Max 5000、4400/5000、4250/5000、4501/5000 及以上禁止进入.
  4. Min 和 Max 字段不得相等.Min = Max 禁止进入.

代码:

import controlP5.*;控制P5 cp5;文本域 O;文本域面向对象;无效设置(){大小(700, 400);PFont font = createFont("arial", 18);cp5 = 新的 ControlP5(this);O = cp5.addTextfield(MIN").setPosition(20, 100).setSize(200, 40);O.setInputFilter(ControlP5.INTEGER).setFont(字体).setColor(颜色(255, 0, 0));OO = cp5.addTextfield(MAX").setPosition(20, 170).setSize(200, 40);OO.setInputFilter(ControlP5.INTEGER).setFont(字体);文本字体(字体);}无效画(){if (keyPressed && OO.isFocus()) {浮动 n;尝试 {n = Float.parseFloat(OO.getText().replace(',', '.'));如果 (!(n >= 1 && n <= 12000)) {抛出新的 NumberFormatException();//抛出以捕捉下面}}捕获(异常 e2){字符串 t;如果 (OO.getText().length() > 1) {t = OO.getText().substring(0, OO.getText().length() - 1);} 别的 {t=";}OO.setText(t);}}if (keyPressed && O.isFocus()) {浮动 n;尝试 {n = Float.parseFloat(O.getText().replace(',', '.'));如果 (!(n >= 1 && n <= 11500)) {抛出新的 NumberFormatException();//抛出以捕捉下面}}捕获(异常 e2){字符串 t;如果 (O.getText().length() > 1) {t = O.getText().substring(0, O.getText().length() - 1);} 别的 {t=";}O.setText(t);}}背景(0);填充(255);}

解决方案

总的来说,您似乎在尝试让用户输入一系列有效值(其中最小值始终小于最大值).已经有一个 ControlP5 控制器:

一个限制是范围不能 >500. 如果这是一个要求,您仍然可以手动约束这些值(通过设置范围 min(low)/max(high) 值):

import controlP5.*;//范围常量最终 int RANGE_MIN = 4000;最终 int RANGE_MAX = 5000;//最小/最大值之间的最小允许差异最终 int RANGE_MIN_DIFFERENCE = 500;最终 int RANGE_MID = RANGE_MIN + ((RANGE_MAX - RANGE_MIN)/2);控制P5 cp5;范围范围;int rangeMinValue;int rangeMaxValue;无效设置(){大小(700, 400);PFont font = createFont("arial", 18);cp5 = 新的 ControlP5(this);range = cp5.addRange(yourCustomRange")//禁用广播,因为 setRange 和 setRangeValues 会触发一个事件.setBroadcast(假).setFont(字体).setPosition(50,50)//注意尺寸与最小/最大范围成比例以避免浮点值.setSize(500,40)//在此处设置最小值 - 最大范围.setRange(RANGE_MIN, RANGE_MAX)//示例:设置初始(推荐)范围值.setRangeValues(RANGE_MIN, RANGE_MIN + RANGE_MIN_DIFFERENCE)//初始化后,我们再次打开广播.setBroadcast(真);文本字体(字体);}无效画(){背景(0);填充(255);}无效控制事件(控制事件事件){if(event.isFrom(yourCustomRange")) {//最小值和最大值存储在一个数组中.//使用 controller().arrayValue() 访问这个数组.//min 在索引 0 处,max 在索引 1 处.int rangeMinInt = int(event.getController().getArrayValue(0));int rangeMaxInt = int(event.getController().getArrayValue(1));//如果值在所需范围内,则更新全局值if(rangeMaxInt - rangeMinInt >= RANGE_MIN_DIFFERENCE){rangeMinValue = rangeMinInt;rangeMaxValue = rangeMaxInt;}别的{//否则检查范围的哪一侧应该被约束(右/最大)或(左/最小)以覆盖用户输入如果(rangeMaxInt > RANGE_MID){range.setLowValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);}别的{range.setHighValue(rangeMinInt + RANGE_MIN_DIFFERENCE);}}//要使用的值println("range:",rangeMinValue,"->",rangeMaxValue);}}

如果占用太多空间,您可以使用

最小 500 差异的逻辑约束值不是 100% 严格,可能还有一些我没有考虑过的其他边缘情况.它更像是一种说明解决问题的方法的方式,因此您可以更好地这样做.

我建议您通过 Processing >例子 >贡献的图书馆 >ControlP5 并运行示例,尤其是控制器.您可以优先考虑那些听起来更接近您当前问题的选项,但您可以通过这些选项获得经验,这样您就可以选择最适合您的问题的控制器/UI 元素.

该示例可能不包括控制器拥有的每个方法的用法,但是底部有一个注释列表,您可以轻松地立即复制/粘贴/运行.此外,您当然还有 完整文档

Good afternoon, I need to add several conditions to validate two fields in the current code.

  1. The Min field must not exceed the Max field. Min <Max / 5000 <4000 prohibited when entering.
  2. The Max field must not be less than the Min field. Max <Min / 4000 <5000 prohibited when entering.
  3. The Min field should not exceed the interval from the Max field, 500 units less can be entered. For instance: Min 4500 and Max 5000, 4400/5000, 4250/5000, 4501/5000 are prohibited to enter and above.
  4. The Min and Max fields must not be equal. Min = Max is prohibited when entering.

code:

import controlP5.*;

ControlP5 cp5;
Textfield O;
Textfield OO;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  O = cp5.addTextfield("MIN")
    .setPosition(20, 100)
    .setSize(200, 40);
  O.setInputFilter(ControlP5.INTEGER)
    .setFont(font)
    .setColor(color(255, 0, 0));

  OO = cp5.addTextfield("MAX")
    .setPosition(20, 170)
    .setSize(200, 40);
  OO.setInputFilter(ControlP5.INTEGER)
    .setFont(font);

  textFont(font);
}

void draw() {
  if (keyPressed && OO.isFocus()) {
    float n;
    try {
      n = Float.parseFloat(OO.getText().replace(',', '.'));
      if (!(n >= 1 && n <= 12000)) {
        throw new NumberFormatException(); // throw to catch below
      }
    } 
    catch (Exception e2) {
      String t;
      if (OO.getText().length() > 1) {
        t = OO.getText().substring(0, OO.getText().length() - 1);
      } else {
        t = "";
      }
      OO.setText(t);
    }
  }
  if (keyPressed && O.isFocus()) {
    float n;
    try {
      n = Float.parseFloat(O.getText().replace(',', '.'));
      if (!(n >= 1 && n <= 11500)) {
        throw new NumberFormatException(); // throw to catch below
      }
    } 
    catch (Exception e2) {
      String t;
      if (O.getText().length() > 1) {
        t = O.getText().substring(0, O.getText().length() - 1);
      } else {
        t = "";
      }
      O.setText(t);
    }
  }
  background(0);
  fill(255);
}

解决方案

Overall it sounds like you're trying to have the user enter a range of valid values (where the minimum is always smaller than the maximum). There's already a ControlP5 controller for that: Range

Other than allowing to set a min and max value within a range is the constraint to keep a difference between max and min value of at least 500.

You could get away with making the Range slider handles 0px wide, essentially disabling them which means the range you set at the start (via setRangeValues) will be maintained:

import controlP5.*;

ControlP5 cp5;

Range range;

int rangeMinValue;
int rangeMaxValue;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  range = cp5.addRange("yourRange")
             // disable broadcasting since setRange and setRangeValues will trigger an event
             .setBroadcast(false) 
             .setFont(font)
             .setPosition(50,50)
             // notice the dimensions are proportional to the min/max range to avoid floating point values
             .setSize(500,40)
             // set minimum - maximum range here
             .setRange(4000,5000)
             // example: set initial (recommended) range values
             .setRangeValues(4000, 4500)
             // workaround to disable left/right handles contraining the range to 500
             .setHandleSize(0)
             // after the initialization we turn broadcast back on again
             .setBroadcast(true)
             ;
  
  textFont(font);
}

void draw() {
  background(0);
  fill(255);
}

void controlEvent(ControlEvent event) {
  if(event.isFrom("yourRange")) {
    // min and max values are stored in an array.
    // access this array with controller().arrayValue().
    // min is at index 0, max is at index 1.
    rangeMinValue = int(event.getController().getArrayValue(0));
    rangeMaxValue = int(event.getController().getArrayValue(1));
    println("range:",rangeMinValue,"->",rangeMaxValue);
  }
  
}

The one limitation is that ranges can't be > 500. If that's a requirment, you can still manually constrain the values (by setting the range min(low)/max(high) values):

import controlP5.*;

// range constants
final int RANGE_MIN = 4000;
final int RANGE_MAX = 5000;
// the smallest allowed difference between min/max values
final int RANGE_MIN_DIFFERENCE = 500;
final int RANGE_MID = RANGE_MIN + ((RANGE_MAX - RANGE_MIN) / 2);

ControlP5 cp5;

Range range;

int rangeMinValue;
int rangeMaxValue;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  range = cp5.addRange("yourCustomRange")
             // disable broadcasting since setRange and setRangeValues will trigger an event
             .setBroadcast(false) 
             .setFont(font)
             .setPosition(50,50)
             // notice the dimensions are proportional to the min/max range to avoid floating point values
             .setSize(500,40)
             // set minimum - maximum range here
             .setRange(RANGE_MIN, RANGE_MAX)
             // example: set initial (recommended) range values
             .setRangeValues(RANGE_MIN, RANGE_MIN + RANGE_MIN_DIFFERENCE)
             // after the initialization we turn broadcast back on again
             .setBroadcast(true)
             ;
  
  textFont(font);
}

void draw() {
  background(0);
  fill(255);
}

void controlEvent(ControlEvent event) {
  if(event.isFrom("yourCustomRange")) {
    // min and max values are stored in an array.
    // access this array with controller().arrayValue().
    // min is at index 0, max is at index 1.
    int rangeMinInt  = int(event.getController().getArrayValue(0));
    int rangeMaxInt  = int(event.getController().getArrayValue(1));
    // if the values are within the desired range, update global values
    if(rangeMaxInt - rangeMinInt >= RANGE_MIN_DIFFERENCE){
      rangeMinValue = rangeMinInt;
      rangeMaxValue = rangeMaxInt;
    }else{
      // otherwise check which side of the range should be constrained (right/max) or (left/min) to overwrite user input
      if(rangeMaxInt > RANGE_MID){
        range.setLowValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);
      }else{
        range.setHighValue(rangeMinInt + RANGE_MIN_DIFFERENCE);
      }
    }
    // values to use
    println("range:",rangeMinValue,"->",rangeMaxValue);
  }
  
}

If that takes too much space you can use Numberbox which compared to the text field has a few advantages:

  • you don't need to worry about string to integer conversion (it handles numbers by default)
  • you can set min/max values

Here's an example:

import controlP5.*;

ControlP5 cp5;

// range constants
final int RANGE_MIN = 4000;
final int RANGE_MAX = 5000;
// the smallest allowed difference between min/max values
final int RANGE_MIN_DIFFERENCE = 500;
final int RANGE_MID = RANGE_MIN + ((RANGE_MAX - RANGE_MIN) / 2);

int minValue;
int maxValue;

Numberbox inputMin;
Numberbox inputMax;

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 18);

  cp5 = new ControlP5(this);

  inputMin = cp5.addNumberbox("minValue")
             .setPosition(100,100)
             .setSize(100,20)
             .setFont(font)
             .setScrollSensitivity(1.1)
             // set initial acceptable range
             .setMin(RANGE_MIN)
             .setMax(RANGE_MAX)
             // set default value
             .setValue(4000)
             ;
  
  inputMax = cp5.addNumberbox("maxValue")
             .setPosition(100,150)
             .setSize(100,20)
             .setFont(font)
             .setScrollSensitivity(1.1)
             // set initial acceptable range
             .setMin(RANGE_MIN)
             .setMax(RANGE_MAX)
             // set default value
             .setValue(RANGE_MID + 1)
             ;
  
  
  textFont(font);
}

void draw() {
  constrainRangeInputs();
  background(0);
  fill(255);
  text("minValue: " + minValue + "\n" +
       "maxValue: " + maxValue, 10, 15);
}

void constrainRangeInputs(){
  int rangeMinInt = (int)inputMin.getValue();
  int rangeMaxInt = (int)inputMax.getValue();
  // 
  if(abs(rangeMaxInt - rangeMinInt) < RANGE_MIN_DIFFERENCE){
    if(rangeMaxInt > RANGE_MID){
      inputMin.setValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);
    }else{
      inputMax.setValue(rangeMinInt + RANGE_MIN_DIFFERENCE);
    }
  }
}

The logic constrain values to a minimum 500 difference is not 100% tight, there may some other edge cases I haven't considered. It's more of a way to illustrate ways of solving the problem so you're better equipt to do so.

I would recommend going through Processing > Examples > Contributed Libraries > ControlP5 and running the examples, in particular the controllers. You can prioritise the ones that sound closer to your current problem, but it's worth getting experience with the options so you can choose the best controllers/UI element to fit your problem.

The example may not include usage of every method the controller has, however there's a comment list at the bottom you could easily copy/paste/run immediately. Additionally of course you have the full documentation

这篇关于如何制作两个字段的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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