从 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);
}
}
}
});
}
我正在尝试创建一个放射性尘埃式的用户界面,在打开 UI 之前,我需要让用户输入以输入 'password',但我做不到弄清楚如何从 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;
}
您可以通过调用 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");
}
}
});
}
}
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 之前,我需要让用户输入以输入 'password',但我做不到弄清楚如何从 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;
}
您可以通过调用 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");
}
}
});
}
}