Java 动作事件停止

Java 动作事件停止,java,swing,button,actionlistener,Java,Swing,Button,Actionlistener,如何停止Button2的操作事件以及Button1之后的更多运行。 Button1只需执行Button1动作事件,然后停止 请帮帮我,谢谢 public void actionPerformed(ActionEvent ae) { if (ae.getSource().equals(button1)){ System.out.println("Button 1"); } if (ae.getSource() == button2){ S

如何停止Button2的操作事件以及Button1之后的更多运行。 Button1只需执行Button1动作事件,然后停止

请帮帮我,谢谢

public void actionPerformed(ActionEvent ae) {
    if (ae.getSource().equals(button1)){
        System.out.println("Button 1");

    }

    if (ae.getSource() == button2){
        System.out.println("Button 2!");
    }
编辑:

对不起,打错了

大体上:

Button1.addActionListener(this);
        jPanel1.add(Button1);
        Button2.addActionListener(this);
        jPanel1.add(Button2);
主要不是:

 public void actionPerformed(ActionEvent ae) {
        Object Button1 = null;
        if (!ae.getSource().equals(Button1)){
            System.out.println("Oben");
        }
        Object Button2 = null;
        if (ae.getSource() == (Button2)){
            System.out.println("Links");
        }


             }
如果我按下按钮1,我会得到“奥本”

如果我按下按钮2,我也会得到“奥本”

为什么我得不到“链接”

再看看你的(编辑过的)代码

所以你在这里说的是,在这两种情况下都是正确的

if (ae.getSource() != null)
这就是为什么结果总是奥本

如果要与不同的
按钮1
进行比较,请确保参照正确的对象。看不到代码的其余部分很难说,但您可能想使用(
this.Button1

再看看你的(编辑过的)代码

所以你在这里说的是,在这两种情况下都是正确的

if (ae.getSource() != null)
这就是为什么结果总是奥本


如果要与不同的
按钮1
进行比较,请确保参照正确的对象。看不到代码的其余部分很难说,但您可能想使用(
this.Button1

代码中有两个问题:

  • 您正在将
    Button1
    Button2
    设置为
    null
  • if语句的布局方式使它们中的多个语句可以在一次调用
    actionPerformed
  • 试试这个:


    此代码假定
    Button1
    Button2
    actionPerformed
    方法所属类的成员。

    代码中有两个问题:

    public void actionPerformed(ActionEvent ae) {
        Object Button1 = null;
        if (!ae.getSource().equals(Button1)){
            System.out.println("Oben");
        }
        Object Button2 = null;
        if (ae.getSource() == (Button2)){
            System.out.println("Links");
        }
    
    
             }
    
  • 您正在将
    Button1
    Button2
    设置为
    null
  • if语句的布局方式使它们中的多个语句可以在一次调用
    actionPerformed
  • 试试这个:

    此代码假定
    Button1
    Button2
    actionPerformed
    方法所属类的成员

    public void actionPerformed(ActionEvent ae) {
        Object Button1 = null;
        if (!ae.getSource().equals(Button1)){
            System.out.println("Oben");
        }
        Object Button2 = null;
        if (ae.getSource() == (Button2)){
            System.out.println("Links");
        }
    
    
             }
    
    恐怕上面的代码没有什么意义,也不符合惯例。首先,除特殊情况外,执行不同操作的按钮应有不同的侦听器。这不是那种特殊情况。将代码拆分为:

    public void actionPerformed(ActionEvent e)
    {
         System.out.println("Oben");
         // This is the actionPerformed method for button 1.
    }
    
    public void actionPerformed(ActionEvent e)
    {
         System.out.println("Links");
         // This is for button 2.
    }
    
    然后简单地绑定到相关按钮

    恐怕上面的代码没有什么意义,也不符合惯例。首先,除特殊情况外,执行不同操作的按钮应有不同的侦听器。这不是那种特殊情况。将代码拆分为:

    public void actionPerformed(ActionEvent e)
    {
         System.out.println("Oben");
         // This is the actionPerformed method for button 1.
    }
    
    public void actionPerformed(ActionEvent e)
    {
         System.out.println("Links");
         // This is for button 2.
    }
    

    然后简单地绑定到相关按钮

    动作事件不会循环,除非被告知这样做。所以我真的很困惑,你想在这里得到什么。代码中的问题是什么将单独的侦听器附加到两个按钮上?您希望看到什么输出,以及您实际看到什么?除非被告知这样做,否则操作事件不会循环。所以我真的很困惑,你想在这里得到什么。代码中的问题是什么将单独的侦听器附加到两个按钮上?您希望看到什么样的输出?您实际看到了什么?正如所写的,这个解决方案没有意义。为了有两个具有相同签名的方法,它们必须属于两个不同的类。正确,但我假设OP知道如何处理这些方法。我尽量避免只将代码交给询问者。正如我所写的,这个解决方案没有意义。为了有两个具有相同签名的方法,它们必须属于两个不同的类。正确,但我假设OP知道如何处理这些方法。我尽量避免只把代码交给询问者。