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

时间:2014-05-08 20:54:54

标签: java swing project jbutton actionevent

大家好日子。我在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;
    }

2 个答案:

答案 0 :(得分:2)

是的,这是可能的,并且经常完成,但魔鬼在细节中。通常,您将拥有一个Control类,它响应与View类(GUI)完全独立的用户交互。选项包括:

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

修改
例如,这是一个包含3个文件的小程序,1个用于保存JButton的View,1个用于Control,第3个主要类用于运行。

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

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

  3. 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!");
             }
          }
       }
    }
    

答案 1 :(得分:0)

根据我的理解,你想要一个类中的按钮和另一个类中的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)

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