在 Processing IDE 中的按钮内创建一个按钮

Making a button inside of a button in Processing IDE

我希望“pick_one”和“pick_all”按钮位于另一个名为“配置”按钮的通用按钮(如文件 - 或界面中的新页面)内。我在下面尝试了这段代码,但我得到了 unexpected token: void 错误。

有没有办法把按钮放在另一个按钮里面?

import controlP5.*; //import ControlP5 library
import processing.serial.*;

Serial port;

ControlP5 cp5; //create ControlP5 object
PFont font;

void setup(){ //Same as setup in arduino
  
  size(900, 900);                         
  port = new Serial(this, "COM4", 9600);   //Change this to your port
  
  cp5 = new ControlP5(this);
  
  font = createFont ("Georgia Bold", 20);
  
  cp5.addButton("PICK_ALL")  //The button
    .setPosition(100, 100)  //x and y coordinates of upper left corner of button
    .setSize(160, 150)      //(width, height)
    .setFont(font)
  ;     
  
  cp5.addButton("PICK_ONE")  //The button
    .setPosition(100, 400)  //x and y coordinates of upper left corner of button
    .setSize(160, 150)      //(width, height)
    .setFont(font) 
  ;      
  
    cp5.addButton("CONFIGURATION")  //The button
    .setPosition(100, 400)  //x and y coordinates of upper left corner of button
    .setSize(160, 150)      //(width, height)
    .setFont(font) 
  ;      
}

void draw(){  //Same as loop in arduino

  background(150, 0 , 150); //Background colour of window (r, g, b) or (0 to 255)
    
}       
void CONFIGURATION() {

void PICK_ALL(){
  
  port.write('t')     
}

void PICK_ONE(){
  
  port.write('l');
  
}}

错误:
看起来您正在嵌套按钮单击处理函数。这些是单击按钮时触发的操作。它们不控制屏幕上按钮的布局或嵌套。

此外,处理中不允许嵌套函数,这就是您收到错误的原因。

确保 CONFIGURATION 函数的大括号不包含任何其他函数:

void CONFIGURATION() {
} // close this function before starting any others

void PICK_ALL(){
  port.write('t')     
}

void PICK_ONE(){
  port.write('l');
}

整体问题:
在另一个按钮中放置一个按钮意味着什么?我不确定我是否理解您要实现的目标。

认为您可能正在寻找类似Groups or Tabs的东西,它们都允许您将多个控件组合在一起。

您的项目可能看起来像这样:

  Group configGroup = cp5.addGroup("CONFIGURATION")
    .setPosition(100,100)
    .setBackgroundHeight(400)
    .setWidth(180)
    .setBackgroundColor(color(255,50))
  ;
  
  cp5.addButton("PICK_ALL")  // The button
    .setPosition(10, 10)     // x and y relative to the group
    .setSize(160, 150)       // (width, height)
    .setFont(font)
    .setGroup(configGroup)   // add it to the group
  ;     
  
  
  cp5.addButton("PICK_ONE")  // The button
    .setPosition(10, 200)    // x and y relative to the group
    .setSize(160, 150)       // (width, height)
    .setFont(font) 
    .setGroup(configGroup)   // add it to the group
  ;

您可以点击群组的标题栏来隐藏和显示内容。
请注意,按钮的位置现在是相对于组容器的。