如何在点击时使用一个JButton实现背景颜色在红色和绿色之间来回切换。

huangapple go评论72阅读模式
英文:

how to change background color back and forth eg red and green when clicked using only 1 jbutton

问题

我试图做的是创建一个按钮,当点击它时,将背景颜色设置为绿色。再次点击时,将其变为红色。基本上是重复这个过程。因此,我正在寻找一些帮助,以指导我如何实现这一点。

以下是我目前所做的:

class ColorText implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Go")) {
            panel.setBackground(Color.GREEN);
        }
    }
}

我尝试过上述方法,但是我遇到了错误,并且想知道如何解决这个问题,或者是否有一种新的方法来实现这个功能。

if (e.getActionCommand().equals(Color.GREEN)) {
    panel.setBackground(Color.RED);
}
英文:

so what I'm trying to do is creating a button that when clicks, turns the background color to be green. once its clicked on again, it turns red. And repeat basically. So im looking for some help that show me how I can do this.

this is what I've done so far

class colorText implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Go")) {
			panel.setBackground(Color.GREEN);
		}
	}
}		

I tried doing it by doing this but I get an error and was wondering either how to get around it or a new a way of doing it

			if (e.getActionCommand() == Color.GREEN) {
			panel.setBackground(Color.RED);
		}

答案1

得分: 1

你尝试的解决方案不起作用,因为(正如我们从第一个示例中看到的),e.getActionCommand() 返回一个字符串 - 并且在点击按钮时很可能始终返回"Go"

你需要的是一种确定状态的方法;区分“我现在应该将背景变为红色”和“我现在应该将背景变为绿色”的方法。有两种方法可以实现这个目标:

1)检查当前的背景颜色

如果 API 允许你查询背景颜色并与一个常量进行比较,你可以这样检查:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Go")) {
        if (panel.getBackground() == Color.RED) {
            panel.setBackground(Color.GREEN);
        } else { // 可以在这里检查是否为绿色背景
            panel.setBackground(Color.RED);
        }
    }
}

这种方法的可能缺点:

  • 你可能无法(可靠地)读取你设置的颜色。可能它在 panel 的 API 中没有暴露出来,或者由于某种原因,你得到的值与 Color 常量不相等(例如,读取的值具有 alpha 通道,而常量没有)
  • 这仅在该方法是修改背景的唯一方法时才有效。如果任何其他代码修改背景,你就无法准确记录你先前的按钮按下所做的操作。这可能是一个很大的问题,因为这意味着这段代码与其他功能不兼容。

2)存储一个标志并在每次更改时翻转它

这种方法涉及存储一个显式字段,让你知道下一步要设置的背景是什么。这可以是一个布尔值,如下所示:

private boolean greenIsNext = true;
       
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Go")) {
        Color nextColour = greenIsNext ? Color.GREEN : Color.RED;
        panel.setBackground(nextColour);
        // 翻转标志
        greenIsNext = !greenIsNext;
    }
}

这可能是更清晰的处理这种行为的方式,因为按下按钮将在设置背景为绿色和红色之间交替,而不管页面上发生了什么其他事情。

英文:

Your attempted solution doesn't work because (as we can see from the first example), e.getActionCommand() returns a String - and likely will always return "Go" when they click on your button.

What you need is some way of determining the state; telling the difference between "I should make the background red now" and "I should make the background green now". There are two ways of doing this that come to mind:

1) Check the current background colour

If the API lets you query the background colour and compare against a constant, you could check it like so:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Go")) {
        if (panel.getBackground() == Color.RED) {
            panel.setBackground(Color.GREEN);
        } else { // could check for GREEN background here
            panel.setBackground(Color.RED)
        }
    }
}

Possible downsides to this approach:

  • You might not be able to (reliably) read back the colour that you set. Either it's not exposed in the panel's API, or the value you get back does not equate to the Color constants for some reason (e.g. the read value has an alpha channel while the constants do not)
  • This only works if this method is the only way that the background gets modified. If any other code were to modify the background, you'd not have an accurate record of what your previous button press did. This is potentially a big issue as it means this code doesn't play nicely with other functionality.

2) Store a flag and flip it each time

This approach involves storing an explicit field that lets you know what to set the background to next. This could just be a boolean, like so:

private boolean greenIsNext = true
   
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Go")) {
        Color nextColour = greenIsNext ? Color.GREEN : Color.RED
        panel.setBackground(nextColour);
        // Flip the flag
        greenIsNext = !greenIsNext
    }
}

This is likely the cleaner way to handle this behaviour, as pressing the button will alternate between setting the background green and red regardless of what else is going on with the page.

huangapple
  • 本文由 发表于 2020年10月14日 23:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64356420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定