Using swing, trying to make the text "Mouse is Clicked" appear on the screen for 2 seconds whenever I click the mouse.
This is what I have so far.
String s = "";
int timeDelay = 30;
public void paintFrame(Graphics g) {
g.drawString(s, 100, 100);
if (timeDelay>0) {
timeDelay--;
}
if(isMouseClicked()) {
String s = "Mouse is clicked";
timeDelay = 30;
return;
}
String s = "";
}
Nothing is showing up when I click, can't seem to figure out why.
You need to call g.drawString() inside if condition for isMouseClicked(). Also inside the if condition for isMouseClicked() you are creating a new local parameter String s(not sure if you want to do that). You cna try the following piece of code.
if(isMouseClicked()) {
s = "Mouse is clicked";
timeDelay = 30;
g.drawString(s, 100, 100);
}
else{
s = "";
}
g.drawString(s, 100, 100);
If it's not disappearing for whatever reason, you could use the tertiary operator for a quick fix. Set's the string depending on the isMouseClicked() boolean
s = isMouseClicked() ? "Mouse is clicked" : "";
g.drawString(s,100,100);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.