gpt4 book ai didi

java - 从 JTextArea 获取输入

转载 作者:行者123 更新时间:2023-11-30 07:41:36 24 4
gpt4 key购买 nike

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

});
}

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

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

编辑:感谢大家的帮助,我最终选择了 keyListener,并且效果非常好!

最佳答案

由于您使用 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;
}

关于java - 从 JTextArea 获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34625087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com