如何将用户输入从 GUI 传递到主类

发布于 2024-11-07 04:01:30 字数 874 浏览 3 评论 0原文

我刚刚开始再次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扶醉桌前 2024-11-14 04:01:30

简短的回答:不要(至少不要在主类中)。

长答案:有一种称为“模型-视图-控制器”(MVC) 的模式,它解释了如何从用户获取数据、对其执行某些操作并再次显示它。此链接(以及整个网站)是一个很好的起点:http://martinfowler.com/ eaaDev/uiArchs.html

应用于您的代码示例:

public class Algorithm {
//a lot of code
}
public class MainClass {
    public static void main(...) {
        Algorithm algorithm = new Algorithm();
        GUI g = new GUI(algorithm );
    }
}
public class GUI extends JFrame implements ActionListener {
    private Algorithm algo;
    public GUI(Algorithm a) { this.algo = a; }
}

算法在这里扮演模型的角色,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:

public class Algorithm {
//a lot of code
}
public class MainClass {
    public static void main(...) {
        Algorithm algorithm = new Algorithm();
        GUI g = new GUI(algorithm );
    }
}
public class GUI extends JFrame implements ActionListener {
    private Algorithm algo;
    public GUI(Algorithm a) { this.algo = a; }
}

Algorithm plays the role of the model here and GUI is a combination of controller and view.

枫林﹌晚霞¤ 2024-11-14 04:01:30

由于您将算法很好地封装在自己的类中,因此应该很容易实例化算法类型的对象,以响应 GUI 上的按钮单击并在那里执行算法。 main 方法应该只决定是否需要 GUI 并启动它。

因此,如果您的 GUI 上有一个名为“计算”的按钮,那么:

  calculate.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
               //get the user input from the JFrame
               Algorithm algorithm = new Algorithm();
               algorithm.execute(user input);
               algorithm.getResult();
               //display results on the JFrame
          }
  });

从 JTextField 等获取输入就像

  mytextfield.getText();

将一些值写入 JLabel 中进行显示一样简单:

  mylabel.setText("Some Text");

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:

  calculate.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
               //get the user input from the JFrame
               Algorithm algorithm = new Algorithm();
               algorithm.execute(user input);
               algorithm.getResult();
               //display results on the JFrame
          }
  });

Getting input from a JTextField and so on is as simple as

  mytextfield.getText();

and to write some value into a JLabel to display is:

  mylabel.setText("Some Text");
醉城メ夜风 2024-11-14 04:01:30

您可以使用观察者模式。在本例中,算法是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文