问题描述
大家好。我在netbeans的项目中有许多Java文件。一个文件名为mainFile,而另一些文件名为addSale,addAttendance。在mainFile.java中,我创建了一个actionPerformed方法来检查是否单击了按钮。但是我要检查是否单击的按钮在其他java文件上。
good day everyone. I have many java files in a project in netbeans. One file is named mainFile while some are addSale, addAttendance. In my mainFile.java, I created an actionPerformed method to check if a button is clicked. But the buttons that I want to checked if clicked is on the other java files.
我已经在mainFile.java中添加了这段代码
I've added this code in my 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
then I added this in my addSales.java
public JButton getButton() {
return confirmAddSales;
}
推荐答案
是的,这是可能的,并且是经常做,但是细节决定成败。通常,您会拥有一个Control类来响应用户交互,而该类与View类(GUI)完全不同。选项包括:
Yes this is possible and is often done, but the devil is in the details. Often you'll have a Control class that responds to user interaction that is completely separate from the View class, the GUI. Options include:
- 为视图类提供公共的
addButtonXActionListener(ActionListener l)
方法 - 赋予view属性更改侦听器支持,并且如果它子类化了一个Swing组件,它将自动拥有它,然后在您的JButton的ActionListener中(通常是一个匿名内部类)将侦听器通知给侦听器。
- 为Ciew类提供Control实例变量并进行设置。然后在JButton的ActionListener中,调用适当的控制方法。
- Give the view class a public
addButtonXActionListener(ActionListener l)
method. - Give the view property change listener support, and if it subclasses a Swing component, it automatically has this, and then in your JButton's ActionListener, often an anonymous inner class, notify the listeners of a state change.
- Give the Ciew class a Control instance variable and set it. Then in the JButton's ActionListener, call the appropriate control method.
编辑
例如,这是一个包含3个文件的小程序,其中1个用于保存JButton的View,1个用于Control以及一个用于使程序运行的第三个主类。
Edit
For example, here is a small program with 3 files, 1 for the View that holds the JButton, 1 for the Control, and a 3rd main class just to get things running.
请注意,有两个JButton,它们都使用两种不同的方式通知外部类它们已被按下。
Note that there are two JButtons and they both use 2 different ways of notifying outside classes that they've been pressed.
- View类具有公共方法,Button 1具有方法
public void setButton1Action(Action action)
,该方法允许外部类设置button1的操作,然后控件执行此操作,并注入AbstractAction来通知控件何时按下button1。 - 视图具有
公共无效addPropertyChangeListener(PropertyChangeListener l)
包装器方法,该方法允许外部类在其PropertyChangeListener中添加,然后将其添加到mainPanel对象的属性更改支持中。然后在button2的匿名内部ActionListener类中,要求mainPanel的PropertyChangeSupport通知所有侦听器BUTTON2属性状态的变化。然后,View添加一个PropertyChangeListener并侦听此属性的状态并作出响应。
- The View class has a public method, Button 1 has a method,
public void setButton1Action(Action action)
, that allows outside classes to set the Action of button1, The Control then does this, injecting an AbstractAction that notifies the Control of when button1 has been pressed. - The View has an
public void addPropertyChangeListener(PropertyChangeListener l)
wrapper method that allows outside classes to add in their PropertyChangeListener which then is added to the property change support of the mainPanel object. Then in button2's anonymous inner ActionListener class, the mainPanel's PropertyChangeSupport is asked to notify all listeners of a change in the state of the BUTTON2 property. View then adds a PropertyChangeListener and listens for changes the state of this property and responds.
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!");
}
}
}
}
这篇关于检查是否在另一个Java文件中单击了jbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!