JFrame在单独的类中,ActionListener怎么样?
我正在尝试将我的Swing GUI与我的实际代码分开。 简而言之,我希望用户启动一个过程(根据用户的选择); 在这种情况下,将不再需要JFrame。
我无法弄清楚的是如何使用Main.class与GUI.class共享用户的选择。
你有什么建议吗?
这是我的代码:
public class Main { public static void main(String[] args) { // Show GUI java.awt.EventQueue.invokeLater(new Runnable() { public void run() { GUI gui = new GUI(templates); gui.setVisible(true); } }); // Kick off a process based on the user's selection } } public class GUI extends JFrame { private static final long serialVersionUID = 1L; public GUI(Object[] objects) { setTitle("GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 350, 100); setLocationRelativeTo(null); JPanel cp = new JPanel(); cp.setBorder(new EmptyBorder(10, 10, 10, 10)); setContentPane(cp); JLabel lbl = new JLabel("Selection:"); cp.add(lbl); final JComboBox comboBox = new JComboBox(new String[] { "One", "Two", "Three" }); comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); dispose(); // Share the selected item with Main.class } }); cp.add(comboBox); } }
这样做的好方法是使用Callback
机制 。
要遵循的步骤:
-
创建一个回调接口
interface Callback { void execute(Object result); }
-
GUI
类将实现Callback
接口,但不提供任何实现 -
使
GUI
类abstract
abstract class GUI extends JFrame implements Callback
-
现在创建一个
GUI
类对象,提供Callback
接口的实际实现 -
在这里你可以使用Anonymous类
GUI gui = new GUI() { @Override public void execute(Object result) { System.out.println("You have selected " + result); } };
-
你可以在
Callback
execute()
方法中传递任何东西。comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); dispose(); // Share the selected item with Main.class // Callback execute(comboBox.getSelectedItem()); } });
这里Main
类负责捕获由GUI
类定向的Callback
的响应。
这是代码:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class Main { public static void main(String[] args) { // Show GUI java.awt.EventQueue.invokeLater(new Runnable() { public void run() { GUI gui = new GUI() { @Override public void execute(Object result) { System.out.println("You have selected " + result); } }; gui.setVisible(true); } }); // Kick off a process based on the user's selection } } interface Callback { void execute(Object result); } abstract class GUI extends JFrame implements Callback { private static final long serialVersionUID = 1L; public GUI() { setTitle("GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 350, 100); setLocationRelativeTo(null); JPanel cp = new JPanel(); cp.setBorder(new EmptyBorder(10, 10, 10, 10)); setContentPane(cp); JLabel lbl = new JLabel("Selection:"); cp.add(lbl); final JComboBox comboBox = new JComboBox(new String[] { "One", "Two", "Three" }); comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); dispose(); // Share the selected item with Main.class execute(comboBox.getSelectedItem()); } }); cp.add(comboBox); } }
您可以创建一个对象来存储选择结果,并将其传递给GUI类的构造函数。 在关闭UI之前在该对象中设置选择结果,然后您的Main类可以访问该值:
public class SelectionResult { private String selectionResult; public void setSelectionResult(final String selectionResult) { this.selectionResult = selectionResult; } public String getSelectionResult() { return this.selectionResult; } }
然后,您可以像这样修改GUI构造函数:
private final SelectionResult selectionResult; public GUI(Object[] objects, SelectionResult selectionResult) { this.selectionResult = selectionResult; ...
在Main类中创建一个SelectionResult对象,并将其传递给GUI类的构造函数。 在GUI类ActionListener中,您可以使用所选值调用setSelectionResult()方法,并且该值可从Main类获得。
在等待在UI中设置值然后根据选择继续执行逻辑时,您需要添加代码以使主方法等待。