如何将用户输入从 GUI 传递到主类
我刚刚开始再次使用 Java Swing,但遇到了与上次相同的问题。 我想编写一个程序来读取一些用户输入,执行算法并显示结果。该程序必须使用两种不同的用户界面(控制台和带有 Java Swing 的 GUI)。
目前,我有一个带有算法的类包(我可以传入用户输入并获取结果),一个包含主类的类,一个用于控制台界面的类和一个用于 GUI 的类(从 JFrame 扩展) )。 一些代码:
public class Algorithm {
//a lot of code
}
public class MainClass {
public static void main(...) {
Algorithm algorithm = new Algorithm();
//use either console or GUI and read user input
algorithm.execute(user input);
algorithm.getResult();
//display result on console/GUI
}
}
public class GUI extends JFrame implements ActionListener {
}
我的问题是我不知道如何将用户输入(文本、缩放器和单选按钮、按钮)从 GUI 传递到算法以及如何在 GUI 上显示结果。
我是否必须将算法实例传递到 GUI 并从 GUI 调用算法方法?
或者是否可以在 MainClass 中实现 ActionsListener (其中我有一个算法实例)?如果我选择这种实现方式,我如何将算法的结果传递回GUI?
或者我应该改变整个实施? :D
I'm just starting to work with Java Swing again and I have the same problem as last time.
I want to write a program which reads some user input, executes an algorithm and displays the result. The program has to work with two different user interfaces (console and GUI with Java Swing).
Currently I have a packet of classes with the algorithm (i can just pass in the user input and fetch the result), a class which contains the main class, a class for the console interface and a class for the GUI (which extends from JFrame).
Some code:
public class Algorithm {
//a lot of code
}
public class MainClass {
public static void main(...) {
Algorithm algorithm = new Algorithm();
//use either console or GUI and read user input
algorithm.execute(user input);
algorithm.getResult();
//display result on console/GUI
}
}
public class GUI extends JFrame implements ActionListener {
}
My Problem is that I don't know how to pass the user input (text, scalers and radio buttons, button) from the GUI to the algorithm and how to display the result on the GUI.
Do I have to pass an instance of Algorithm to the GUI and call the methods of Algorithm from GUI?
Or is it possible to implement the ActionsListener in MainClass (where I have an instance of Algorithm)? If I choose this way of implementation, how can i pass the result of the Algorithm back to the GUI?
Or should i change the whole implementation? :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短的回答:不要(至少不要在主类中)。
长答案:有一种称为“模型-视图-控制器”(MVC) 的模式,它解释了如何从用户获取数据、对其执行某些操作并再次显示它。此链接(以及整个网站)是一个很好的起点:http://martinfowler.com/ eaaDev/uiArchs.html
应用于您的代码示例:
算法
在这里扮演模型的角色,GUI
是控制器和视图的组合。Short answer: Don't (at least not to the Main class).
Long answer: There is a pattern called Model-View-Controller (MVC) which explains how to get data from the user, do something with it and display it again. This link (and the whole site in general) is a good point to start: http://martinfowler.com/eaaDev/uiArchs.html
Applied to your code sample:
Algorithm
plays the role of the model here andGUI
is a combination of controller and view.由于您将算法很好地封装在自己的类中,因此应该很容易实例化算法类型的对象,以响应 GUI 上的按钮单击并在那里执行算法。 main 方法应该只决定是否需要 GUI 并启动它。
因此,如果您的 GUI 上有一个名为“计算”的按钮,那么:
从 JTextField 等获取输入就像
将一些值写入 JLabel 中进行显示一样简单:
Since you have the Algorithm nicely encapsulated in its own class then it should be easy to instantiate on object of type Algorithm in response to a button click on your GUI and execute the algorithm there. The main method shoul donly decide if the GUI is necessary and start it up.
So if you have a button called calculate on your GUI then:
Getting input from a JTextField and so on is as simple as
and to write some value into a JLabel to display is:
您可以使用观察者模式。在本例中,算法是 java.util.Observer,Gui 是 java.util.Observable。
You can use the Observer Pattern. In this case algorithm is the java.util.Observer and Gui is the java.util.Observable.