【问题标题】:how to reset the value of Textfield in controlP5? or I want to reset the shape如何在 controlP5 中重置 Textfield 的值?或者我想重置形状
【发布时间】:2021-07-22 14:10:10
【问题描述】:

我正在尝试创建一个使用处理来绘制形状的应用程序。 我即将达到我的目标。 但是,重置按钮无法按预期工作。

在我的代码中,文本字段消失而不是形状消失。

我想要做的是当我按下重置按钮时形状消失。 我想擦除 TextField 的内容或擦除形状。


import controlP5.*;

ControlP5 cp5;
Button bt0;
Textarea ta0;
Textfield tf0;
Textfield tf1;
DropdownList dl0;

String[]items ={"circle", "square"};

void setup() {
  size(500, 650);
  cp5 = new ControlP5(this);

  bt0 = cp5.addButton("bt0");
  bt0.setPosition(360, 500);
  bt0.setSize(120, 30);
  bt0.setFont(createFont("SansSerif", 20));
  bt0.setLabel("Clear");
  bt0.setColorCaptionLabel(#000000);
  bt0.setColorBackground(#dddddd);

  //TextField
  tf0 = cp5.addTextfield("tf0");
  tf0.setPosition(20, 500);
  tf0.setSize(100, 30);
  tf0.setFont(createFont("SansSerif", 20));
  tf0.setLabel("X");
  tf0.setColorCaptionLabel(#000000);
  tf0.setColor(#000000);
  tf0.setColorBackground(#ffffff);

  tf1 = cp5.addTextfield("tf1");
  tf1.setPosition(130, 500);
  tf1.setSize(100, 30);
  tf1.setFont(createFont("SansSerif", 20));
  tf1.setLabel("Y");
  tf1.setColorCaptionLabel(#000000);
  tf1.setColor(#000000);
  tf1.setColorBackground(#ffffff);

  dl0 = cp5.addDropdownList("dl0");
  dl0.setPosition(250, 500);
  dl0.setSize(100, 200);
  dl0.setBarHeight(35);
  dl0.setItemHeight(35);
  dl0.setFont(createFont("SansSerif", 20));
  dl0.setLabel("shape select");
  dl0.setColorCaptionLabel(#000000);
  dl0.setColorValue(#000000);
  dl0.setColorBackground(#FFFFFF);
  dl0.addItems(items);
  dl0.setValue(-1);
  dl0.close();
}

void draw() {

  int value = int(dl0.getValue());
  background(#AAAAAA);

  if ((value >=0)&&(value < items.length)) {
    fill(#FFFFFF);
    stroke(1);
    if (items[value] == "circle") {
      x = int(tf0.getText());
      y = int(tf1.getText());
      ellipse(x, y, 100, 100);
    } else if (items[value] == "square") {
      x = int(tf0.getText());
      y = int(tf1.getText());
      rect(x, y, 100, 100);
    }
  }
}

// clear button clicked
void bt0() {
////textField remove..!  I want to erase the contents of TextField and erase the shape
  //cp5.get("tf0").remove(); 
  //cp5.get("tf1").remove();
}

【问题讨论】:

    标签: java processing control-p5


    【解决方案1】:

    您可以使用setText 方法设置CP5 TextField 的值。您可以将它们都设置为 0 或空字符串来“重置”它们。

    由于您根据下拉控件的值绘制形状,因此您还需要重置它,否则您只会在 0,0 处绘制选定的形状(因为这些是文本字段的新值)。

    这样的事情应该可以工作:

    void bt0() {
      // clear the textfields
      tf0.setText("");
      tf1.setText("");
    
      // clear the value of the dropdown and reset the label
      dl0.setValue(-1);
      dl0.setLabel("shape select");
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 2021-10-14
      • 2021-06-16
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多