简体   繁体   English

从JTextArea获取输入

[英]Getting Input from JTextArea

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);                        
                } 
                }
    }

    });
}

I'm trying to create a fallout-esque user interface, and I need to get user input to type in a 'password' before I open up the UI, but I can't figure out how to read input from the JTextArea, please help! 我正在尝试创建一个类似辐射的用户界面,在打开UI之前,我需要获取用户输入以键入“密码”,但是我不知道如何从JTextArea中读取输入。救命!

NOTE: The main goal here is to keep the feel of using an old school DOS program, so i can't use JOptionPane or anything like that. 注意:这里的主要目标是保持使用老式DOS程序的感觉,因此我不能使用JOptionPane或类似的东西。

EDIT: Thanks for all your help everybody, I ended up going with the keyListener, and it worked perfectly! 编辑:谢谢大家的所有帮助,我最终选择了keyListener,它运行良好!

Since you use JTextArea instead of JPasswordField , you have to filter out the password from the content of the text from the your JTextArea . 由于使用JTextArea而不是JPasswordField ,因此必须从JTextArea的文本内容中过滤出密码。 The idea that I have so far is to make a condition to capture the password after the Type the password > sentences. 到目前为止,我的想法是在Type the password >句子之后,为捕获密码创造条件。

Then, save the original password somewhere in ArrayList and masking the original password to something else like *** and replace the original password content on the JTextArea to the masked password. 然后,将原始密码保存在ArrayList某个位置,并将原始密码屏蔽为***内容,然后将JTextArea上的原始密码内容替换为屏蔽的密码。 Maybe this is not a perfect solution for your question, but I believe this answer can help you at least. 也许这不是您问题的完美解决方案,但我相信这个答案至少可以为您提供帮助。

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;
}

you can get text from input by calling getText() method 您可以通过调用getText()方法从输入中获取文本

JTextArea text = new JTextArea();
String content = text.getText();

If login is successful then show success msg in your JTextArea Code should be like 如果登录成功,则在您的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");
                }
            }
        });
    }
}

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

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