0

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

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

我想要做的是当我按下重置按钮时形状会消失。我想擦除 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();
}
4

1 回答 1

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-07-22T18:21:34.203 回答