Java addActionListener, multiple instance is occurring

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

Java addActionListener, multiple instance is occurring

问题

当我尝试执行comboBox_0的操作时,它会打印“red”并将comboBox_1的选定项目更改为“apple”,但它还会生成comboBox_1的操作事件,并打印“blue”。

如何执行此操作,以使程序不将更改comboBox_1项目视为生成操作事件。我不需要它打印“blue”,只需要打印“red”并更改为“apple”。

Java是否有办法区分用户进行的操作与程序本身?

抱歉,我对这个领域很陌生,无法查看讨论ActionEvents的源代码。

这是一个示例:

public void actionPerformed(ActionEvent e) 
{
    if(e.getSource()==comboBox_0)
    {
        System.out.println("red");
        comboBox_1.setSelectedItem("apple");
    }
    else if(e.getSource()==comboBox_1)
    {   
        System.out.println("blue");
    }
    else if(e.getSource()==comboBox_2)
    {
        System.out.println("green");
    }
}
英文:

When i try to perform action for comboBox_0, it wil print "red" and change selected item on comboBox_1 as "apple", however it will also generate action event for comboBox_1 and will also print "blue".

How can i perform this so the program will not see changing comboBox_1 item as generating action event. i dont need it to print "blue", only print "red" and change to "apple".

Is there a way for java to differentiate between user conducted action vs the program itself?

Sorry, i'm new to this field and i cant check source where they discuss about ActionEvents.

here's the example:

public void actionPerformed(ActionEvent e) 
{
	if(e.getSource()==comboBox_0)
	{
		System.out.println("red");
        comboBox_1.setSelectedItem("apple");
	}
	else if(e.getSource()==comboBox_1)
	{   
		System.out.println("blue");
	}
	else if(e.getSource()==comboBox_2)
	{
		System.out.println("green");
	}
}

答案1

得分: 1

以下是翻译好的部分:

"你正在触发一个事件,但同时你不想使用它。将标志输入到你的程序中。使用以下代码:

private boolean flag = false;
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==comboBox_0)
    {
        System.out.println("red");
        comboBox_1.setSelectedItem("apple");
        flag = true;
    }
    else if(e.getSource()==comboBox_1)
    {
        if(flag){
            flag = false;
            return;
        }
        System.out.println("blue");
    }
    else if(e.getSource()==comboBox_2)
    {
        System.out.println("green");
    }
}

不幸的是,无法区分事件来源(软件或用户定义的)。但这并不是必要的。如果有这样的实现,它最终也会被减少为一个“标志”。您可以使用布尔值或枚举作为标志。"

英文:

You are raising an event, but at the same time you don't want to use it. Enter the flag into your program. Use this:

private boolean flag = false;
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==comboBox_0)
    {
        System.out.println("red");
        comboBox_1.setSelectedItem("apple");
        flag = true;
    }
    else if(e.getSource()==comboBox_1)
    {
        if(flag){
            flag = false;
            return;
        }
        System.out.println("blue");
    }
    else if(e.getSource()==comboBox_2)
    {
        System.out.println("green");
    }
}

Unfortunately, it is not possible to distinguish the event source (software or user-defined). But it is not necessary. If there were such an implementation, it would in any case be reduced to a"flag". You can use boolean or Emun as the flag.

huangapple
  • 本文由 发表于 2020年8月13日 14:58:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63389720.html
匿名

发表评论

匿名网友

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

确定