Processing Java: 防止 selectInput 无限期打开

Processing Java: Prevent selectInput from opening indefinitely

我正在使用 selectInput() 创建“打开文件”按钮。问题是每次用户选择文件时程序都会打开 windows。我该如何防止这种情况发生?

void setup()
{
  size(500, 500);
  background(255);
}

void draw()
{
  noStroke();
  fill(255, 0, 0);
  rect(0, 0, 50, 20);

  if (mousePressed)
  {
    if (mouseX <= 50 && mouseY <= 20)
    {
      selectInput("Select a file to open:", "fileSelected");
    }
  }
}

void fileSelected(File selection)
{
  if (selection != null)
  {
    String absolutePath = selection.getAbsolutePath();
    String[] locations = split(absolutePath, "\");
    String fileName = locations[locations.length - 1];

    //addFile(fileList);
    println(fileName);
  }
}

使用JOptionPane.showMessageDialog()

否则你可以使用一个变量来跟踪是否已经打开了一个对话框,但这是一个混乱的解决方案。

另一个解决方案是将 conditionals 和下面的 selectInput 放在 Processing 的 mousePressed() 函数中。