如何写在运算符之后的其他值上?

问题描述

下午好,我使用的是switch结构,但是我不知道如何在case运算符之后正确编写其他值。

displayData代码的开关结构中,我为这些字段分配命令编号,例如:Vin = cp5.addTextlabel,以便以后通过com端口连接时,通过发送此命令显示或更改数据在Vin = cp5.addTextlabel字段中输入命令“ 60”,123。

仅适用于(分钟= cp5.addButton)-10,(葡萄酒= cp5.addTextlabel)-60, (inputPULI = cp5.addNumberBox)-40,(inputNapryzenieKV = cp5.addNumberBox)-70

下面是主要错误函数“ setText(String)”未定义。请参见屏幕截图。

如何解决此问题并正确编写?

ImageButton按钮;

RadioButton CheckBoxuvum; Documentation

复选框P4; Documentation

请参阅GUI文件

任务:

ImageButton(按钮)-20,目标是通过终端或微控制器发送命令来在窗口中更改图片

P4 = cp5.addCheckBox-30 CHECK,目标是通过终端或微控制器发送命令来改变窗口中复选框的状态。

CheckBoxuvum = cp5.addRadioButton-50 +/-,目标是通过终端或微控制器发送命令,以更改窗口中复选框的状态(+/-)。

displaydata代码中,我用一个问题标记了它。

将来,我计划通过com端口从终端发送命令,以接收窗口中这些值的数据状态变化。

错误的屏幕截图:

enter image description here

我将必须上传所有测试项目文件,以便您可以进行测试。

Com_Interface1:

import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
DropdownList serialPortsList;

Serial serialPort;
final int BAUD_RATE = 115200;

char parity = 'N';
int dataBits = 8;
float stopBits = 1.0;

public void setup() {
  size(700,420,JAVA2D);
  surface.setTitle("TEST");
  surface.setResizable(false);
  setupUI();
  smooth();
  frameRate(30);
  writeOutgioing[lengthmas-1]=1; 
  String[] portNames = Serial.list();
  //serialPort.clear(); // Why does not it work?
  for (int i = 0; i < portNames.length; i++) serialPortsList.addItem(portNames[i],i);
}
public void toplug (int theValue) { // Start button on click sends a commad 1.
  println("Button click events: "+theValue);
  strata =!strata;
  if (!strata) {
    connection.setLabel("Пуск");
  } else {
    connection.setLabel("СТОП");
    serialports((int)serialPortsList.getValue());
    writeOutgioing[0]=1;
    writeOut();
  }
}
public void serialports(int theValue) {
  try {
    serialPort = new Serial(this,Serial.list()[theValue],BAUD_RATE,parity,dataBits,stopBits);
    serialPort.bufferUntil('\n');

    println("COM connected: "+ Serial.list()[theValue] );
    /*Send.unlock();
     connection.unlock();*/    //locking buttons in applications if not connected via rs-232.
  }
  catch(Exception e) {
    System.err.println("Error opening serial port" + Serial.list()[theValue]);
    e.printstacktrace();
  }
}

显示数据:

    void displaydata() {

  switch(readIncome[0]) {
  case 10:
    minutess.setCaptionLabel(readIncome[1]+" Мин1.");
    min1=readIncome[1];
    break;
  case 20:
    // What to write?
    // label(readIncome[1]+" Мин."); // ImageButton
    // min=readIncome[1];
    break;
  case 30:
    // What to write?
    // P4.setText("std2"+readIncome[1] ); // CheckBox
    break;
  case 40:
    inputPULI.setLabel("Bullet - "+readIncome[1] );
    break;
  case 70:
    inputNapryzenieKV.setLabel("Voltage - "+readIncome[1] );
    break;
  case 60:
    Vin.setText("Voltage K.V - "+readIncome[1] );
    break;
  case 50:
    // What to write?
    // CheckBoxuvum.setText("UV/UM - "+readIncome[1] ); // RadioButton
    break;
  default:
    println("displayData(): no case selected.");
    break; // technically not necessary,but I like my switches tidy
  }
}

GUI:

int min1 = 0;
int PUL;
float NapryzenieKV;
boolean strata=false;

Button minutess;
ImageButton button; 
NumberBox inputPULI; 
NumberBox inputNapryzenieKV; 
RadioButton CheckBoxuvum; 
CheckBox P4; 
Textlabel Vin; 
Button connection; 
Button Send; 

public void setupUI()
{
  cp5 = new ControlP5(this);
  PFont fontn = createFont("Times New Roman",18);
  PFont p = createFont("Times New Roman",18);
  ControlFont font=new
    ControlFont(p);
  cp5.setFont(font);

  connection = cp5.addButton("toplug")
    .setCaptionLabel("ПУСК")
    .setPosition(387,30)
    .setSize(150,30);

  serialPortsList = cp5.addDropdownList("Порт")
    .setPosition(130,200)
    .setItemHeight(30)
    .setBarHeight(30);

  minutess = cp5.addButton("minutesss")
    .setCaptionLabel("ВЫКЛ")
    .setPosition(145,100)
    .setSize(90,25);

  Send = cp5.addButton("toapply")
    .setCaptionLabel("Apply")
    .setPosition(510,370)
    .setSize(150,30);

  Vin = cp5.addTextlabel("naprazhenie kondencatora")
    .setText("Voltage K.V")
    .setFont(p)
    .setColor(color(#00ffff))
    .setPosition(45,320);

  CheckBoxuvum = cp5.addRadioButton("UV/UM")
    .setPosition(155,360)
    .setSize(15,15)
    .setColorActive(color(255))
    .setItemsPerRow(2)
    .setSpacingColumn(85)
    .addItem("+",1)
    .addItem("-",2);

  P4 = cp5.addCheckBox("std2")
    .setPosition(150,190)
    .setSize(15,15)
    .setItemsPerRow(1)
    .setSpacingColumn(30)
    .setSpacingRow(20)
    .addItem("Check",2);

  inputPULI = cp5.addNumberBox("PUL")
    .setLabel("Bullet")
    .setPosition(150,220)
    .setSize(80,30)
    .setColorValue(0xffffff00)
    .setFont(p)
    .setScrollSensitivity(1.1)
    .setDirection(Controller.HORIZONTAL)
    .setRange(1,199)
    .setValue(3);
  Label labelinputPULI = inputPULI.getCaptionLabel();
  labelinputPULI.setFont(font);
  labelinputPULI.setColor(color(#00ffff));
  labelinputPULI.toupperCase(false);
  labelinputPULI.setText("Пуль");
  labelinputPULI.align(ControlP5.LEFT_OUTSIDE,CENTER);
  labelinputPULI.getStyle().setPaddingLeft(-55);

  inputNapryzenieKV = cp5.addNumberBox("NapryzenieKV")
    .setLabel("Voltage")
    .setPosition(150,270)
    .setSize(80,30)
    .setColorValue(0xffffff00)
    .setFont(p)
    .setScrollSensitivity(1.1)
    .setMin(25)
    .setMax(99)
    .setMultiplier(0.01)
    .setDirection(Controller.HORIZONTAL)
    .setValue(25);
  Label labelinputNapryzenieKV = inputNapryzenieKV.getCaptionLabel();
  labelinputNapryzenieKV.setFont(font);
  labelinputNapryzenieKV.setColor(color(#00ffff));
  labelinputNapryzenieKV.toupperCase(false);
  labelinputNapryzenieKV.setText("Напряжение");
  labelinputNapryzenieKV.align(ControlP5.LEFT_OUTSIDE,CENTER);
  labelinputNapryzenieKV.getStyle().setPaddingLeft(-45);

  textFont(fontn);
  {
    // button dimensions
    int w = 99;
    int h = 25;
    // test with generated images
    button = new ImageButton(140,140,w,h,new PImage[]{
      getimage(w,color(192,32 * 2)),// off
      getimage(w,color(0,32 * 3)),// 10
      getimage(w,32 * 4)),// 20
      getimage(w,32 * 5)),// 30
      getimage(w,32 * 6)),// 40
      getimage(w,32 * 7)),// 50
      getimage(w,32 * 8)),// 60
      });
  }
}
void mousepressed() {
  button.mousepressed(mouseX,mouseY);
  println(button.min);
}
// test images to represent loaded state images
PImage getimage(int w,int h,int c) {
  PImage img = createImage(w,RGB);
  java.util.Arrays.fill(img.pixels,c);
  img.updatePixels();
  return img;
}

// make a custom image button class
class ImageButton {
  // minutes is the data it stores
  int min = 0;
  // images for each state
  PImage[] stateImages;
  // which image to display
  int stateIndex;
  // position
  int x,y;
  // dimensions: width,height
  int w,h;
  // text to display
  String label = "ВЫКЛ";

  ImageButton(int x,int y,int w,PImage[] stateImages) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.stateImages = stateImages;
  }
  void mousepressed(int mx,int my) {
    // check the cursor is within the button bounds
    boolean isOver = ((mx >= x && mx <= x + w) && // check horizontal
      (my >= y && my <= y + h) ); // check vertical

    if (isOver) {

      min += 10;
      stateIndex++;

      if (min>60) {
        min = 0; 
        stateIndex = 0;
        label = "ВЫКЛ";
      } else {
        label = (str(min) + "Мин");
      }
    }
  }
  void draw() {
    // if the images and index are valid
    if (stateImages != null && stateIndex < stateImages.length) {
      image(stateImages[stateIndex],x,y,h);
    } else {
      println("error displaying button state image");
      println("stateImages: ");
      printArray(stateImages);
      println("stateIndex: " + stateIndex);
    }
    // display text
    text(label,x + 17,y + h - 8);
  }
}
public void minutesss() {

  min1 += 10;
  if (min1>60) {
    min1 = 0; 
    minutess.setCaptionLabel("ВЫКЛ");
  } else { 
    minutess.setCaptionLabel(str(min1)+" Мин1");
  }
}
void controlEvent(ControlEvent theEvent) {
  if (theEvent.isFrom(CheckBoxuvum)) {
    //myColorBackground = 0;
    print("got an event from "+CheckBoxuvum.getName()+"\t\n");
    // checkBox uses arrayValue to store the state of 
    // individual checkBox-items. usage:
    println(CheckBoxuvum.getArrayValue());
    int col = 0;
    for (int i=0; i<CheckBoxuvum.getArrayValue().length; i++) {
      int n = (int)CheckBoxuvum.getArrayValue()[i];
      print(n);
      if (n==1) {
        //myColorBackground += CheckBoxuvum.getItem(i).internalValue();
      }
    }
    println();
  }

  if (theEvent.isGroup()) {
    // check if the Event was triggered from a ControlGroup
    println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
  } else if (theEvent.isController()) {
    println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
  }
}

协议:

int lengthmas = 7;
int RC = -128; // -128 separating byte в java.
int[] readIncome = new int[lengthmas];
int[] writeOutgioing = new int[lengthmas];
String incomingData= null;
String outgoingData=null;

void readInc() {
  while ( serialPort.available() > 0) {
    incomingData = serialPort.readStringUntil(RC);
    readIncome = int(split (incomingData,',' )); // the received data is comma separated.
  }
}

void  writeOut() {
  outgoingData=str(writeOutgioing[0])+str(writeOutgioing[1])+str(writeOutgioing[2])+str(writeOutgioing[3])+str(writeOutgioing[4])+str(writeOutgioing[5])+str(writeOutgioing[6]); // sending data as a string.
  serialPort.write(outgoingData);
}

/*Protocol:
 Protocol composition - an array of seven elements,0 - command,1-4 data,5 - remainder of packets for transmission,6 (-128) separating byte.
 The controller responds by sending the same command and data.
 Example: 10(Command),235,2353,564,5(packages left),77777(CRC),-128(RC)
 Commands:
 Modifier:
 10 - minutess = cp5.addButton
 20 - ImageButton button
 30 - CheckBox P4
 40 - NumberBox inputPULI
 70 - NumberBox inputNapryzenieKV
 60 - Textlabel Vin
 50 - RadioButton CheckBoxuvum
 */

发送数据:

public void toapply()
{
}

循环:

void draw() {
  background(50);
  button.draw();
}
{
  if (strata) {
    readInc();
    //readIncome[]
    displaydata();
  }
}

解决方法

问题是default:

开关的运行方式是这样的:它找到与键相对应的大小写,并从此刻开始执行代码,直到遇到break;default:案例是一个包罗万象的东西:它就像一个永远都是真的条件。

为说明起见,我喜欢这个示例:在这里,当用户输入数字时,它将打印从该点到12月的所有月份的名称。

int month = 5;

switch (month) {
    case 1:  println("January");
    case 2:  println("February");
    case 3:  println("March");
    case 4:  println("April");
    case 5:  println("May");
    case 6:  println("June");
    case 7:  println("July");
    case 8:  println("August");
    case 9:  println("September");
    case 10: println("October");
    case 11: println("November");
    case 12: println("December");
             break;
    default: println("Error: no valid month selected.");
             break;
}

注意default的结尾。这是因为由于它始终为“ true”,并且由于开关只会选择一种情况,然后运行直到break语句,所以如果我将其放在其他位置,则在读取时将始终选择它。通过修改示例来查看:

int month = 5;

switch (month) {
    default: println("Whatever,I'm not printing month anymore!");
             break; // no case after this point will ever be read
    case 1:  println("January");
    case 2:  println("February");
    case 3:  println("March");
    case 4:  println("April");
    case 5:  println("May");
    case 6:  println("June");
    case 7:  println("July");
    case 8:  println("August");
    case 9:  println("September");
    case 10: println("October");
    case 11: println("November");
    case 12: println("December");
             break;
}

这正是程序中发生的情况。在DisplayData()中,一开始就有一个default案例,它会覆盖所有其他案例:

void Displaydata() {

  switch(readIncome[0]) {
  case 10:
  default: // <--- HERE THIS IS ALWAYS SELECTED
    minutess.setCaptionLabel(readIncome[1]+" Мин.");
    min=readIncome[1];
    break; // <--- EVERYTHING AFTER THIS IS IGNORED

您可以使用如下正确的语法进行重写:

void Displaydata() {
  switch(readIncome[0]) {
  case 10:
    minutess.setCaptionLabel(readIncome[1]+" Мин.");
    min=readIncome[1];
    break;
  case 20:
    button.label(readIncome[1]+" Мин."); //ImageButton
    min=readIncome[1];
    break;
  case 30:
    if(readIncome[1]==1)P4.setText("on");
    if(readIncome[1]==0)P4.setText("off");// CheckBox
    break;
  case 40:
    inputPULI.setText("Bullet - "+readIncome[1] ); //Numberbox int
    break;
  case 70:
    inputNapryzenieKV.setText("Voltage - "+readIncome[1] ); //Numberbox float
    break;
  case 60:
    Vin.setText("Voltage K.V - "+readIncome[1] );
    break;
  case 50:
    if(readIncome[1]==1)CheckBoxuvum.setText("+"); //RadioButton
    if(readIncome[1]==0)CheckBoxuvum.setText("-");
    break;
  default:
    println("DisplayData(): no case selected.");
    break; // technically not necessary,but I like my switches tidy
  }
}

玩得开心!