【问题标题】:Getting Input from JTextArea从 JTextArea 获取输入
【发布时间】:2023-03-24 03:46:01
【问题描述】:
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);                        
                } 
                }
    }

    });
}

我正在尝试创建一个类似后果的用户界面,我需要在打开 UI 之前让用户输入输入“密码”,但我不知道如何从JTextArea,请帮忙!

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

编辑:感谢大家的帮助,我最终选择了 keyListener,它运行良好!

【问题讨论】:

  • 嗨,也许这可以帮助你:stackoverflow.com/questions/19755259/…
  • 使用JPasswordField。否则,您将需要使用 DocumentListenerDocumentFilter 并可能覆盖 insert-break 键绑定
  • JPasswordField 会在 JTextArea 中工作还是需要弹出窗口?
  • @jstuder a JPasswordField 就像 JTextArea 一样工作。您真的应该阅读文档,它们为您提供了您需要知道的几乎所有内容。

标签: java input jtextarea keylistener jwindow


【解决方案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;
}

【讨论】:

    【解决方案2】:

    你可以通过调用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");
                    }
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 2017-02-12
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      相关资源
      最近更新 更多