简体   繁体   English

如何暂停具有main()的程序,直到按下GUI中的按钮?

[英]How to pause program having main() until a button from GUI is pressed?

I am new at Java Swing. 我是Java Swing的新手。 I have two Java files. 我有两个Java文件。 One having main() in it and the other is the GUI file. 一个包含main()的文件是GUI文件。

Client 客户

class Client
{
    GUI gui;
    public static void main(String args[])
    {
        //.......... do something.......
        gui = new GUI();
        // at thin point I want to have value of gui.s1 ....
        //but main() actually do not wait for the user input.
    }
}

GUI 图形用户界面

class GUI extends JFrame implements ActionListener
{
    String s1="";

    GUI()
    {
        JTextField t1= new JTextField(20);
        JButton j1= new JButton("submit");
        j1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {         
        s1=t1.getText();
    } 
}

Please guide me, and if it is not appropriate question then please suggest me the article that you think I should read to get the concept. 请指导我,如果不合适的话,请给我建议您认为应该读懂的文章。

Right now I'm at phone so I can't help you with code I will try to let you understand the concept: An user input, a button click is something which vould happen after 5 seconds like could happen after 30 minutes. 现在,我正在打电话,所以我无法为您提供代码方面的帮助,我将尽力让您理解这个概念:用户输入,按钮单击是5秒钟后就会发生的事情,例如30分钟后就会发生。 So yes, you could let sleep the main for sometimes and hope for an input, wait until .s1 get a value and etc. 所以是的,您有时可以让主设备进入休眠状态,并希望有一个输入,等到.s1得到一个值等等。

But, I don't see it like the right thing to do here. 但是,我不认为这是正确的做法。 The best thing which could be used is a callback which is called when the user click the button. 最好使用的方法是在用户单击按钮时调用的回调。 It's done using interfaces. 使用接口完成。

Well, first you declare an interface maybe named OnRequestClick where you implement onRequestClick(String message); 好吧,首先,您声明一个可能名为OnRequestClick的接口,在其中实现onRequestClick(String message); method. 方法。

Message will be the text of s1. 消息将是s1的文本。

Now in the GUI class create a new field of type OnRequestClick named listener and take it in your constructor. 现在,在GUI类中,创建一个名称为listener的OnRequestClick类型的新字段,并将其放入构造函数中。

Now where you create the GUI object the compiler ask to you to provide a code for OnRequestClick so do it and it willbe the code which will be executed when the user press tbe button. 现在,在创建GUI对象的地方,编译器会要求您提供OnRequestClick的代码,这样做就是当用户按下tbe按钮时将执行的代码。

Well, righr now what I said is false: it doesn't get fired since we didn't have done any call to listener.onRequestClick () 好吧,现在我所说的确实是错误的:因为我们没有对listener.onRequestClick()进行任何调用,所以它不会被解雇

So in your actionPerformed add listener.onRequestClick (s1.getText ()); 因此,在您的actionPerformed中添加listener.onRequestClick(s1.getText()); so in your main you will get the ebemt and the text. 因此在您的主要语言中,您将获得ebemt和文本。

Replace GUI with a JOptionPane.showInputDialog(..) and not only will the code be a lot shorter, but the problem will be solved. JOptionPane.showInputDialog(..)替换GUI ,不仅代码会短很多,而且问题也会得到解决。 EG 例如

import javax.swing.*;

class UserInput {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String name = JOptionPane.showInputDialog(null, "Name?");
                if (name==null) {
                    System.out.println("Please input a name!");
                } else {
                    System.out.println("Name: " + name);
                }
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

You can use a Callback Mechanism . 您可以使用回调机制

I have already posted a sample code here JFrame in separate class, what about the ActionListener? 我已经在单独的类中的JFrame此处发布了示例代码,那么ActionListener呢? . Please have a look. 请看一看。

interface Callback {
    void execute(String value);
}

abstract class GUI extends JFrame implements ActionListener, Callback{
     ...
     // do not provide the implementation of `execute` method here

     ...
     public void actionPerformed(ActionEvent e) {
        s1 = t1.getText();
        // now callback method is called
        execute(s1);
     }
}

Your main class will look like: 您的主要课程如下所示:

public static void main(String args[]) {
    gui = new GUI() {

        @Override
        public void execute(String value) {
            System.out.println("Entered value:" + value);
        }
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM