gpt4 book ai didi

java - 第二个按钮对象覆盖第一个(处理)

转载 作者:行者123 更新时间:2023-12-02 12:06:41 24 4
gpt4 key购买 nike

enter image description here哎呀!

我是处理方面的新手,所以如果我的问题不够清楚,我深表歉意。

我正在制作一种 toastr ,它必须在单击开始按钮时启动,并在单击停止按钮时弹出面包。我创建了一个按钮类,并在主程序中创建了一个新的按钮对象。为了进行测试,当单击开始按钮时,我将背景颜色从白色变为红色。

开始按钮可以工作,但现在我正在尝试创建一个新的按钮对象(停止按钮),但是当我创建停止按钮对象时,它会覆盖开始按钮对象。首先,我认为这可能是因为它们的启动按钮位于停止按钮下方,但事实并非如此。一旦我处理,图像加载也需要很长时间。

希望你能帮助我:)

  int s = 0; 
int m = 0;


Button stop_button;
Button on_button;
int clk = 1;



void setup()
{
on_button = new Button("Start", 230, 300, 60, 30);
stop_button = new Button("Start", 230, 300, 60, 30);
size(600, 600);
background(255, 255, 255);


}


void draw () {
stroke(240, 242, 179);
fill(240, 242, 179);
rect(190,150,210,130, 30);

stroke(126, 191, 167);
fill(126, 191, 167);
rect(170,200,250,170, 30);



//BUTTON CODE
if (on_button.MouseIsOver()) {
}
on_button.Draw();
}


void mousePressed()
{
if (on_button.MouseIsOver())
print("Clicked: ");
println(clk++);
time();
}

}

void time ()
{
background(255, 0, 0);

}

//按钮类

class Button {
String label; // button label
float x; // top left corner x position
float y; // top left corner y position
float w; // width of button
float h; // height of button

// constructor
Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
label = labelB;
x = xpos;
y = ypos;
w = widthB;
h = heightB;
}

void Draw() {
fill(218);
stroke(141);
rect(x, y, w, h, 10);
textAlign(CENTER, CENTER);
fill(0);
text(label, x + (w / 2), y + (h / 2));
}

boolean MouseIsOver() {
if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
return true;
}
return false;
}
}

最佳答案

我不完全理解你的代码,因为你永远不会对点击使用react,也永远不会绘制第二个按钮。

如果我是你,我会break your problem down into smaller steps并一次对一个人采取这些步骤。例如,您能否运行一个非常基本的示例,该示例显示一个按钮,每当您单击该按钮时,该按钮都会向控制台打印一条消息?

从那里,也许可以添加第二个按钮在不同的位置,当您单击它时,它会打印不同的消息。在继续之前让其完美运行。

当您开始工作时,然后更改您的第一个草图以执行某些操作,例如单击按钮时更改背景颜色。最后,您可以结合上述所有内容,在单击第一个按钮后显示第二个按钮。

提示:您可能希望使用 boolean 变量来表示当前显示的按钮。但就像我说的,最好的选择是从更简单的开始,并以更小的步骤前进。

关于java - 第二个按钮对象覆盖第一个(处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46867478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com