簡體   English   中英

使程序“暫停”,直到用戶在JTextField中輸入內容為止

[英]Make program “pause” until the user enters something in a JTextField

我想制作一個行為類似於Windows命令提示符或終端的程序。 它基本上只是一個JFrame,其output為JTextArea,而JTextField為input 看起來像這樣:

在此處輸入圖片說明

我希望每當我的程序調用返回String的方法時都可以從JTextField獲取輸入,例如:

public static String getInput() {
    //Wait for user to enter something, then press the enter key
}

我添加了AbstractAction,以便可以在按下回車鍵時做一些事情,但是我仍然可以弄清楚無論何時調用該方法,如何以String形式返回輸入。

Action action = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e) {

        //Clears the JTextField
        input.setText("");

    }
};

我可以將諸如userInput = input.getText()放入public void actionPerformed() ,以便將變量設置為每次輸入的內容,並在需要時使用userInput ,但我希望用戶有時間讀取屏幕上的內容,然后讓程序等待響應,而不僅僅是使用他們立即輸入的最后內容。

我試圖使用一個userInput變量和一個布爾變量,如下所示:

private static String userInput = "";
private static boolean enterPressed = false;

Action action = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e) {

        userInput = input.getText()
        enterPressed = true;
        input.setText("");

    }
};

...

public static String getInput() {

    enterPressed = false;
    while(!enterPressed){
       //Do Nothing
    }
    enterPressed = false;
    return userInput;

}

當我調用output.setText(getInput()); ,它的工作方式與我想要的一樣,只是while(!enterPressed){}使我的處理器工作起來比應做的要難得多。 我很確定這樣做可能有更好的方法。

現在是我的整個代碼:

public class ConsoleFrame {

//Objects for Console Frame
JFrame frame = new JFrame();
JTextArea output = new JTextArea();
JTextField input = new JTextField();
BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
JScrollPane scrollPane = new JScrollPane(output);
DefaultCaret caret = (DefaultCaret)output.getCaret();
Action action = new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e) {

        input.setText("");

    }
};
ConsoleFrame(){
    input.addActionListener(action);
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    frame.setLayout(boxLayout);
    frame.add(scrollPane);
    frame.add(input);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 250);
    frame.setLocationRelativeTo(null);
    input.setMaximumSize(new Dimension(Integer.MAX_VALUE, 10));
    output.setEditable(false);
    output.setAutoscrolls(true);
}
public static String getInput() {
    return null;
}

}

因此,每次調用getInput();時,如何在用戶按下Enter之前停止程序getInput();

編輯:上半年刪除。 我注意到這是錯誤的。

最好的選擇是在用戶在actionPerformed方法內輸入文本后,將對您需要執行的計算機的調用括起來。 因此,當用戶輸入文本並按Enter鍵時,程序將自動從那里繼續。

Action action = new AbstractAction(){
@Override
    public void actionPerformed(ActionEvent e) {

    userInput = input.getText()
    enterPressed = true;
    input.setText("");
    //call next method;

    }
};

這需要您進行更多格式化工作,但是根據您的項目可能會有所幫助。 此鏈接包含有關此策略的更多信息。

希望這會有所幫助。

我希望每當我的程序調用返回String的方法時都可以從JTextField獲取輸入,例如:

我添加了AbstractAction,以便可以在按下回車鍵時做一些事情,但是我仍然可以弄清楚無論何時調用該方法,如何以String形式返回輸入。

public String getInput() {
    return input.getText();
}

那么,每次調用getInput();時,如何停止程序,直到用戶按下Enter為止?

你不知道 像大多數UI框架一樣,擺動是事件驅動的,也就是說,某些事情發生了,您對此做出了響應。

因此,考慮到這一點,您應該考慮使用某種Observer Pattern ,在其中使用回叫通知您感興趣的某種更改,例如ActionListener

取而代之的是,您可以提供某種偵聽器,有興趣的各方可以向其注冊,並在字段更改時通知您,例如...

import java.util.EventListener;
import java.util.EventObject;

public class InputEvent extends EventObject {

    private final String text;

    public InputEvent(Object source, String text) {
        super(source);
        this.text = text;
    }

    public String getText() {
        return text;
    }

}

public interface InputObsever extends EventListener {

    public void inputChanged(InputEvent evt);

}

因此,我們現在有了一個觀察者,只要更改/更新輸入,便會得到通知。

然后我們只需要一種取消注冊/觸發事件的方法...

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;
import javax.swing.text.DefaultCaret;

public class ConsoleFrame {

//Objects for Console Frame
    JFrame frame = new JFrame();
    JTextArea output = new JTextArea();
    JTextField input = new JTextField();
    BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    JScrollPane scrollPane = new JScrollPane(output);
    DefaultCaret caret = (DefaultCaret) output.getCaret();
    Action action = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {

            fireInputChanged(input.getText());
            input.selectAll();

        }
    };

    private EventListenerList listenerList = new EventListenerList();

    ConsoleFrame() {
        input.addActionListener(action);
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        frame.setLayout(boxLayout);
        frame.add(scrollPane);
        frame.add(input);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 250);
        frame.setLocationRelativeTo(null);
        input.setMaximumSize(new Dimension(Integer.MAX_VALUE, 10));
        output.setEditable(false);
        output.setAutoscrolls(true);
    }

    public String getInput() {
        return input.getText();
    }

    public void addInputObsever(InputObsever obsever) {
        listenerList.add(InputObsever.class, obsever);
    }

    public void removeInputObsever(InputObsever obsever) {
        listenerList.remove(InputObsever.class, obsever);
    }

    protected void fireInputChanged(String text) {

        InputObsever[] listeners = listenerList.getListeners(InputObsever.class);
        if (listeners.length > 0) {

            InputEvent evt = new InputEvent(this, text);
            for (InputObsever obsever : listeners) {
                obsever.inputChanged(evt);
            }

        }

    }

}

現在,這里的重點是,當您想知道何時輸入/更改了文本時,可以將觀察者注冊到ConsoleFrame的實例

console.addInputObsever(new InputObsever() {
    @Override
    public void inputChanged(InputEvent evt) {
        // Do what ever you need to do...
    }
});
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
                yourFunction(jTextField1.getText());
                jTextField1.setText("");
        }
    });

順便說一句,您可以append JTextArea以獲得console window的感覺

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM