在组件之间传递值

我有一个调用AsbtractActionJDialog ,它会调出一个JFileChooser以便用户可以选择一个目录。 这些都是单独的类。 从JFileChooser传递值的正确方法是什么,所以我可以在JDialog显示目录的路径?

编辑:更新了问题。

这是一个不完整的例子,但我想可以让你知道如何实现你所需要的。 重要的是引用你想要选择的属性,如YourDialog.this.selectedFile=file;

请参阅下文,了解如何将其放入代码中:

  public class YourDialog extends JDialog implements ActionListener { protected File selectedFile=null; //Constructor public YourDialog(JFrame frame, boolean modal, String message) { //... create button and added to the panel someButton.addActionListener(new AbstractAction { public void actionPerformed(ActionEvent e) { final JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(YourDialog.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); // HERE IS THE TRICK I GUESS !! YourDialog.this.selectedFile=file; } } }); } } 

我希望它没有发布一个完整的例子,这有所帮助。

编辑

本质上,我们没有将任何参数传递给AbstractAction。 事实是,AbstractAction可以通过访问类似YourDialog.this.somePropertyOrMethod来访问“调用者”的任何非私有属性。 这是因为AbstractActionYourDialog类的匿名类。

我不是这方面的专业人士,但我相信我有两种常用的方法可以将值从一个对象传递到另一个对象(并且可能有更多),方法是将值从一个推送到另一个并通过将值从一个拉到另一个。 通过推送我的意思是这样做的明显方式,其中第一类引用了第二类,并在类2中调用一个方法传递值。在您的示例中,AbstractAction具有对GUI的引用并在GUI中调用方法设置JTextField,如下所示:

  public void actionPerformed(ActionEvent arg0) { String text = "Text from MyPushAction"; guiPanel.setPushPathFieldText(text); } 

另一种方式更复杂,其中GUI具有注册到操作的侦听器,并且当被通知属性已经改变时从GUI拉取信息。 这可能不是您何时使用它的最佳示例,但它可以是解耦代码的有用方法。 这方面的一个例子有点复杂。 以下是两个例子:

 import java.awt.event.ActionEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import javax.swing.*; public class FuSwing { private static void createAndShowUI() { GuiPanel guiPanel = new GuiPanel(); JFrame frame = new JFrame("FuSwing"); frame.getContentPane().add(guiPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class GuiPanel extends JPanel { private MyPullAction myPullAction = new MyPullAction(); private JTextField pushPathField = new JTextField(20); private JButton pushActionButton = new JButton(new MyPushAction(this)); private JTextField pullPathField = new JTextField(20); private JButton pullActionButton = new JButton(myPullAction); public GuiPanel() { add(pushActionButton); add(pushPathField); add(Box.createHorizontalStrut(15)); add(pullActionButton); add(pullPathField); myPullAction.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals(MyPullAction.TEXT_PROPERTY)) { pullPathField.setText(evt.getNewValue().toString()); } } }); } public void setPushPathFieldText(String text) { pushPathField.setText(text); } } class MyPushAction extends AbstractAction { private GuiPanel guiPanel; public MyPushAction(GuiPanel guiPanel) { super("Push Action"); this.guiPanel = guiPanel; } public void actionPerformed(ActionEvent arg0) { String text = "Text from MyPushAction"; guiPanel.setPushPathFieldText(text); } } class MyPullAction extends AbstractAction { public static final String TEXT_PROPERTY = "text"; private String text = ""; private PropertyChangeSupport pcs = new PropertyChangeSupport(this); public MyPullAction() { super("Pull Action"); } public void addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } public String getText() { return text; } public void setText(String text) { String oldText = this.text; this.text = text; PropertyChangeEvent evt = new PropertyChangeEvent(this, TEXT_PROPERTY, oldText, text); pcs.firePropertyChange(evt); } public void actionPerformed(ActionEvent e) { setText("Text from MyPullAction"); } }