1

首先让我说这是我第一次在 Stackoverflow 上发帖,因为这个伟大的社区已经回答了我的大部分问题。

我正在尝试制作一个非常简单的程序,其中选择按钮并根据按钮保存字符串。

我可以选择 GUI1 上的按钮,然后显示我的 GUI2,但我在 GUI1 中创建的按钮仍然在后台并且仍然启用。

我想删除它们或完全禁用它们,以免按钮重叠。

Button want;
Button dwant;

Button eat;
Button sleep;
Button play;
Button read;
Button swim;


boolean showGUI1 = true;
boolean showGUI2 = false;

color bgCol = color(255);

void setup()
{
 size(550, 900);

 want = new Button("Want",140,300,250,150); 
 dwant = new Button("Dont Want",140,590,250,150);

 eat = new Button("Eat",140,300,200,100); 
 sleep = new Button("Sleep",140,400,200,100); 
 play = new Button("Play",140,500,200,100); 
 read = new Button("Read",140,600,200,100); 
 swim = new Button("Swim",140,700,200,100); 

}

void draw()
{
  background(bgCol);

  if (showGUI1)
{
     want.display();
     dwant.display();
  }

  if (showGUI2)
  {
 eat.display();
 sleep.display();
 play.display();
 read.display();
 swim.display();

  }

}

    void mouseReleased()
    {
      if(want.mouseReleased())  
  {
    bgCol = color(255,0,0);
    choice= "Want";
    showGUI1 = false;
    showGUI2 = true;

  }

  if(dwant.mouseReleased())
  {
    bgCol = color(0,255,0);
    choice= "Dont Want";
    showGUI1 = false;
    showGUI2 = true;

   }

//action  GUI2 showing
    if(eat.mouseReleased())  
  {
    action= "Eat";

    showGUI2 = false;

  }

  if(sleep.mouseReleased())
  {
    action= "Sleep";

    showGUI2 = false;

  }

  if(play.mouseReleased())  
  {
    action= "Play";

    showGUI2 = false;

  }

  if(read.mouseReleased())
  {
    action= "Read";

    showGUI2 = false;

  }

  if(swim.mouseReleased())
  {
    action= "Swim";

    showGUI2 = false;

}

对于我的按钮,我使用以下代码

Button(String nm, int x, int y, int w, int h)
{
super(nm, x, y, w, h);
}

void display()
{
  if(currentImage != null)
  {
    float imgWidth = (extents.y*currentImage.width)/currentImage.height;

  pushStyle();
  imageMode(CORNER);
  tint(imageTint);
  image(currentImage, pos.x, pos.y, imgWidth, extents.y);
  stroke(bgColor);
  noFill();
  rect(pos.x, pos.y, imgWidth,  extents.y);
  noTint();
  popStyle();
}
else
{
  pushStyle();
  stroke(lineColor);
  fill(bgColor);
  rect(pos.x, pos.y, extents.x, extents.y);

  fill(lineColor);
  textAlign(CENTER, CENTER);
  text(name, pos.x + 0.5*extents.x, pos.y + 0.5* extents.y);
  popStyle();
}

}

我也可以将 ControlP5 库用于按钮,但即便如此我也不知道如何激活“controlP5.Controller : Button hide()”(隐藏功能)

如果有人能提供一些启示,将不胜感激

4

1 回答 1

0

好吧,要删除它们,您可以使用 button.setVisibility(View.GONE),它将删除它们并且它们不会占用任何布局空间。但是,如果您想隐藏它们但保留它们占用的空间,请使用 button.setVisibility(View.INVISIBLE)。

如果在另一边您仍想看到它们,但只是禁用它们,请使用 button.setEnabled(false)。

希望能帮助到你

于 2013-11-10T16:05:12.380 回答