从JTextArea获取输入

时间:2016-01-06 03:21:49

标签: java input jtextarea keylistener jwindow

public static void main(String[] args) throws PrinterException {

    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    int ySize = ((int) tk.getScreenSize().getHeight());
    final String password = "Alphabet";

    JFrame screen = new JFrame("INSERT TITLE HERE");

    screen.setSize(xSize, ySize);
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setResizable(false);

    screen.setVisible(true);

    final JWindow window = new JWindow(screen);
    window.setSize(xSize, ySize);
    window.setName("INSERT TITLE HERE");

    final JTextArea text = new JTextArea();
    text.setText("Type the password > ");
    text.setBackground(Color.BLACK);
    text.setForeground(Color.green);



    window.add(text);
    window.setVisible(true);

    text.addKeyListener(new java.awt.event.KeyAdapter(){
        public void keyReleased(java.awt.event.KeyEvent evt) {
            System.out.println(evt.getKeyCode());

            if(evt.getKeyCode() == 51){
                System.out.println(text.getText());
                String passAttempt = text.getText();
                int start = passAttempt.indexOf('>') + 2 ;
                int end = passAttempt.indexOf('#');
                passAttempt = passAttempt.substring(start, end);
                if(passAttempt.equals(password)) {
                    System.out.println("SUCCESSFUL");
                    text.setText("Login Successful");
                    window.add(text);
                    window.setVisible(true);
                    }
                if(!passAttempt.equals(password)) {
                    System.out.println(passAttempt);
                    text.setText("Incorrect");
                    window.add(text);
                    window.setVisible(true);                        
                } 
                }
    }

    });
}

我正在尝试创建一个Fallout-esque用户界面,我需要在打开UI之前让用户输入键入'password',但我无法弄清楚如何从中读取输入JTextArea,请帮忙!

注意:这里的主要目标是保持使用旧学校DOS程序的感觉,所以我不能使用JOptionPane或类似的东西。

编辑:感谢大家的帮助,我最终选择了keyListener,它完美无缺!

2 个答案:

答案 0 :(得分:1)

由于您使用的是JTextArea而不是JPasswordField,因此您必须从JTextArea中的文字内容中过滤掉密码。到目前为止,我的想法是在Type the password >句之后创建一个捕获密码的条件。

然后,将原始密码保存在ArrayList的某处,并将原始密码屏蔽到***之类的其他内容,并将JTextArea上的原始密码内容替换为屏蔽密码。也许这不是你问题的完美解决方案,但我相信这个答案至少可以帮到你。

public class Test {
private static String password;
private static List<String> passwordList;

public static void main(String[] args) throws PrinterException {

    keyEvents ke = new keyEvents();
    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    int ySize = ((int) tk.getScreenSize().getHeight());

    JFrame screen = new JFrame("INSERT TITLE HERE");

    screen.setSize(xSize, ySize);
    screen.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    screen.setResizable(false);

    screen.setVisible(true);

    JWindow window = new JWindow(screen);
    window.setSize(xSize, ySize);
    window.setName("INSERT TITLE HERE");

    final JTextArea text = new JTextArea();
    text.setText("Type the password > ");
    text.setBackground(Color.BLACK);
    text.setForeground(Color.green);

    passwordList = new ArrayList<String>();

    text.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            String[] content = text.getText().split("\n");
            String newContent = "";

            for (int i = 0; i < content.length; i++) {
                if (content[i].contains("Type the password > ")) {
                    password = content[i].replace("Type the password > ", "");

                    if(password.length() > 0){
                        passwordList.add(password.substring(password.length() - 1));
                    }

                    content[i] = "Type the password > " + passwordMasked(password);


                }

                newContent += content[i];
            }

            if (evt.getKeyCode() == 10) {
                newContent += "\nYour password is " + Arrays.toString(passwordList.toArray());
            }

            text.setText(newContent);
        }
    });

    window.add(text);
    window.setVisible(true);

}

public static String passwordMasked(String password) {
    String value = password;
    password = "";
    for (char c : value.toCharArray()) {
        password += "*";
    }

    return password;
}

答案 1 :(得分:0)

您可以通过调用getText()方法

从输入中获取文本
JTextArea text = new JTextArea();
String content = text.getText();

如果登录成功,则在JTextArea代码中显示成功消息应该像

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.print.PrinterException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JWindow;


public class Test {
    private static String password = "abc";
    public static void main(String[] args) throws PrinterException {
        Toolkit tk = Toolkit.getDefaultToolkit();
        int xSize = ((int) tk.getScreenSize().getWidth());
        int ySize = ((int) tk.getScreenSize().getHeight());

        JFrame screen = new JFrame("INSERT TITLE HERE");

        screen.setSize(xSize, ySize);
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        screen.setResizable(false);

        screen.setVisible(true);

        JWindow window = new JWindow(screen);
        window.setSize(xSize, ySize);
        window.setName("INSERT TITLE HERE");

        final JTextArea text = new JTextArea();
        text.setText("Type the password > ");
        text.setBackground(Color.BLACK);
        text.setForeground(Color.green);


        window.add(text);
        window.setVisible(true);

        text.addKeyListener(new java.awt.event.KeyAdapter(){
            public void keyReleased(java.awt.event.KeyEvent evt) {
                System.out.println(evt.getKeyCode());
                if(evt.getKeyCode() == 10){
                    if(text.getText().equalsIgnoreCase(password))
                        text.setText("Login Successfull");
                }
            }
        });
    }
}