繁体   English   中英

检查是否在另一个Java文件中单击了jbutton

[英]checking if a jbutton is clicked in another java file

今天是个好日子。 我在netbeans的项目中有许多Java文件。 一个文件名为mainFile,而另一些文件名为addSale,addAttendance。 在mainFile.java中,我创建了一个actionPerformed方法来检查是否单击了按钮。 但是我要检查是否单击的按钮在其他java文件上。

我已经在mainFile.java中添加了此代码

AddSales addSaleButton;
  Login logButton;

  public void actionPerformed(ActionEvent ae){
    if (ae.getSource() == addSaleButton.getButton()){
      System.out.print("sample add");
    }else if (ae.getSource() == logButton.getButton()){
      System.out.print("sample log");
    }
  } 

  public void setButtonAction(Action action) {
      (addSaleButton.getButton()).setAction(action);
   }

然后我在addSales.java中添加了它

public JButton getButton() {
        return confirmAddSales;
    }

是的,这是可能的并且经常发生,但是细节在于魔鬼。 通常,您会拥有一个Control类来响应与View类(GUI)完全不同的用户交互。 选项包括:

  • 给视图类提供一个公共的addButtonXActionListener(ActionListener l)方法。
  • 提供对视图属性更改侦听器的支持,如果它是Swing组件的子类,它将自动具有此组件,然后在JButton的ActionListener中(通常是一个匿名内部类)将状态更改通知给侦听器。
  • 给Ciew类一个Control实例变量并进行设置。 然后在JButton的ActionListener中,调用适当的控制方法。

编辑
例如,这是一个包含3个文件的小程序,其中1个用于保存JButton的View,1个用于控件,而第三个主类只是为了使事情运行。

请注意,有两个JButton,它们都使用2种不同的方式来通知外部类它们已被按下。

  1. View类具有一个公共方法,Button 1具有一个方法public void setButton1Action(Action action) ,该方法允许外部类设置button1的Action,然后控件执行此操作,并注入一个AbstractAction来通知Control当button1具有被按下。
  2. 该视图具有一个public void addPropertyChangeListener(PropertyChangeListener l)包装器方法,该方法允许外部类在其PropertyChangeListener中添加,然后将其添加到mainPanel对象的属性更改支持中。 然后在button2的匿名内部ActionListener类中,要求mainPanel的PropertyChangeSupport通知所有侦听器BUTTON2属性状态的变化。 然后,View添加一个PropertyChangeListener并侦听此属性的状态并作出响应。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class TestButtonPress {
   private static void createAndShowGui() {
      View view = new View();
      Control control = new Control();
      control.setView(view);

      JFrame frame = new JFrame("TestButtonPress");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(view.getMainPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class View {
   public static final String BUTTON2 = "Button 2";
   private JPanel mainPanel = new JPanel();
   private JButton button1 = new JButton();
   private JButton button2 = new JButton(BUTTON2);

   public View() {
      mainPanel.add(button1);
      mainPanel.add(button2);

      button2.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            mainPanel.firePropertyChange(BUTTON2, false, true);
         }
      });
   }

   public JComponent getMainPanel() {
      return mainPanel;
   }

   public void setButton1Action(Action action) {
      button1.setAction(action);
   }

   public void addPropertyChangeListener(PropertyChangeListener l) {
      mainPanel.addPropertyChangeListener(l);
   }

   public void removePropertyChangeListener(PropertyChangeListener l) {
      mainPanel.removePropertyChangeListener(l);
   }

}

class Control {
   View view;

   public void setView(final View view) {
      this.view = view;
      view.setButton1Action(new ButtonAction());
      view.addPropertyChangeListener(new Button2Listener());
   };

   private class ButtonAction extends AbstractAction {
      public ButtonAction() {
         super("Button 1");
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         System.out.println(evt.getActionCommand() + " has been pressed!");
      }
   }

   private class Button2Listener implements PropertyChangeListener {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
         if (View.BUTTON2.equals(evt.getPropertyName())) {
            System.out.println("Button 2 has been pressed!");
         }
      }
   }
}

据我了解,您希望您的按钮在一个类中,而ActionListener在另一个类中。 我不确定100%是否是您想要的,但是这里有一些代码:

主类Btn:

package btn;

public class Btn
{
    public static void main(String[] args)
    {
        Frame f = new Frame();
        f.setVisible(true);
    }

}

按钮类按钮:

package btn;

import javax.swing.JButton;

public class Button extends JButton
{

    public Button()
    {
        super("Hello world");
        this.addActionListener(new ActionListenerClass());
    }
}

ActionListener类ActionListenerClass:

package btn;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ActionListenerClass implements ActionListener
{
    public static int numberOfClicks = 0;

    @Override
    public void actionPerformed(ActionEvent e)
    {
        numberOfClicks++;
        System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks);
    }

}

车架类车架:

package btn;

import javax.swing.JFrame;

public class Frame extends JFrame
{
    public Frame()
    {
        super("Hello world test");
        this.setBounds(0, 0, 300, 500);
        this.add(new Button());
        this.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
    }
}

和输出:

    run:
HELLO WORLD!!
number of times pressed 1
HELLO WORLD!!
number of times pressed 2
HELLO WORLD!!
number of times pressed 3
HELLO WORLD!!
number of times pressed 4
BUILD SUCCESSFUL (total time: 3 seconds)

您在这里所做的只是将动作侦听器放在单独的类中,然后告诉按钮在该类中寻找动作侦听器

使用以下代码: this.addActionListener(new ActionListenerClass());

希望这会有所帮助,卢克。

编辑:

尝试使用e.paramString():

这将打印如下内容:

HELLO WORLD!!
number of times pressed 1
Button: ACTION_PERFORMED,cmd=Hello world,when=1399588160253,modifiers=Button1
BUILD SUCCESSFUL (total time: 2 seconds)

e.getActionCommand():

这将打印按钮名称:

run:
HELLO WORLD!!
number of times pressed 1
Button: Hello world
BUILD SUCCESSFUL (total time: 4 seconds)

完整的actionListener代码块:

@Override
    public void actionPerformed(ActionEvent e)
    {
        numberOfClicks++;
        System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks + 
                "\ne.getActionCommand();: " + e.getActionCommand()
        + "\ne.paramString();: " + e.paramString());
    }

输出:

run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399588455144,modifiers=Button1
BUILD SUCCESSFUL (total time: 6 seconds)

第二编辑:

好的,使用getActionCommand(); 方法然后使用开关约束检查女巫按钮是否被按下。

我添加了另一个按钮,一个叫做“ LOL”,另一个叫做“ Hello world”,都使用相同的动作监听器。

同时按下两个的输出:

run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590104631,modifiers=Button1
HELLO WORLD!!
number of times pressed 2
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590106679,modifiers=Button1
HELLO WORLD!!
number of times pressed 3
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590107665,modifiers=Button1
HELLO WORLD!!
number of times pressed 4
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590107780,modifiers=Button1
BUILD SUCCESSFUL (total time: 5 seconds)

现在使用开关约束来区分按钮之间的区别:

@Override
    public void actionPerformed(ActionEvent e)
    {
        numberOfClicks++;
        System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks + 
                "\ne.getActionCommand();: " + e.getActionCommand()
        + "\ne.paramString();: " + e.paramString());

        switch(e.getActionCommand())
        {
            case "LOL":
                System.out.println("Button \"LOL\" was clicked");
                break;
            case "Hello world":
                System.out.println("Button \"Hello world\" was clicked");
                break;
        }
    }

带开关输出:

run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590324792,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 2
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590324943,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 3
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590325089,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 4
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590330897,modifiers=Button1
Button "LOL" was clicked
HELLO WORLD!!
number of times pressed 5
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590331048,modifiers=Button1
Button "LOL" was clicked
BUILD SUCCESSFUL (total time: 11 seconds)

我希望这回答了你的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM