1
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を使用することになりましたが、完全に機能しました!

4

2 に答える 2

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;
}
于 2016-01-06T04:54:51.357 に答える
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");
                }
            }
        });
    }
}
于 2016-01-06T05:13:26.043 に答える