我尝试过搜索 Stack Overflow 和 Code Ranch,但查询结果没有成功。我会尽力使其尽可能简单和直接。我想做的是有一个登录页面,当单击它时,它会根据我编码的内容检查用户名,如果正确,则处理该 JFrame 并转到另一个 JFrame。谁能指出我正确的方向?
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (userTextField.equals(userString)){
frame.dispose();
new SecondFrame();
}
}
});
其中按钮是 JButton,userTextField 是 JTextField,userString 是存储用户名的字符串,SecondFrame 是我希望在选择 JButton 时将其移动到的 JFrame
编辑
public SecondFrame() {
centerPiece.setLayout(new net.miginfocom.swing.MigLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500, 525);
f.setVisible(true);
f.setLocationRelativeTo(null);
如果你想了解更多,请告诉我
最佳答案
您需要使用 userTextField 的 getText
方法,因为 userTextField.equals(userString)
实际上是比较对象 userTextField
和 用户字符串
查看 javadocs 了解更多信息:https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html
关于java - 从 JTextField 输入并将其与输入的字符串进行比较,然后移至下一个 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38603515/