所以我想验证用户在游戏进行前输入的用户名(长度应该在6到30个字符之间,不允许使用数字(,我在互联网上搜索了一下regex表达式。我不习惯,所以我发现了一些问题。代码应该执行以下操作:如果用户没有输入用户名,或者在单击"时包含不允许的字符;开始游戏";按钮,我想弹出一个对话框,告诉他再试一次。如果一切都是正确的,游戏应该照常进行,但当我运行此代码时,用户名没有被选中,它会进入游戏,尽管在文本框中输入了什么,下面是我到目前为止所拥有的。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.util.regex.*;
public class StartScreen extends JFrame {
private JPanel contentPane;
private JTextField textbox_name;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StartScreen frame = new StartScreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public static boolean isValidUsername(String name)
{
String regex = "^[a-zA-Z]{5,29}$"; //Removed ^[aA-zZ]\\w{5,29}$
Pattern p = Pattern.compile(regex);
if (name == null) {
return false;
}
Matcher m = p.matcher(name);
return m.matches();
}
public StartScreen() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 455, 191);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblEnterName = new JLabel("Enter Name:");
lblEnterName.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
lblEnterName.setBounds(91, 48, 102, 29);
contentPane.add(lblEnterName);
textbox_name = new JTextField();
textbox_name.setBounds(205, 49, 130, 28);
contentPane.add(textbox_name);
textbox_name.setColumns(10);
String field;
field = textbox_name.getText();
JButton btnStartGame = new JButton("Start Game");
btnStartGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(isValidUsername(field)) {
MainScreen window = new MainScreen();
window.setVisible(true);
dispose();
}
else {
JOptionPane.showMessageDialog(null, "Please enter your username correctly");
}
}
});
btnStartGame.setBounds(150, 102, 117, 29);
contentPane.add(btnStartGame);
}
}
这两行来自您在问题中发布的代码:
String field;
field = textbox_name.getText();
需要在方法actionPerformed()
内,因为您想在用户点击按钮btnStartGame
后获得textbox_name
的内容。
这是我对你发布的代码的改写。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.util.regex.*;
public class StartScreen extends JFrame {
private JPanel contentPane;
private JTextField textbox_name;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StartScreen frame = new StartScreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public static boolean isValidUsername(String name)
{
String regex = "^[a-zA-Z]{5,29}$"; //Removed ^[aA-zZ]\\w{5,29}$
Pattern p = Pattern.compile(regex);
if (name == null) {
return false;
}
Matcher m = p.matcher(name);
return m.matches();
}
public StartScreen() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 455, 191);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblEnterName = new JLabel("Enter Name:");
lblEnterName.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
lblEnterName.setBounds(91, 48, 102, 29);
contentPane.add(lblEnterName);
textbox_name = new JTextField();
textbox_name.setBounds(205, 49, 130, 28);
contentPane.add(textbox_name);
textbox_name.setColumns(10);
JButton btnStartGame = new JButton("Start Game");
btnStartGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String field; // added this line
field = textbox_name.getText(); // added this line
if(isValidUsername(field)) {
MainScreen window = new MainScreen();
window.setVisible(true);
dispose();
}
else {
JOptionPane.showMessageDialog(null, "Please enter your username correctly");
}
}
});
btnStartGame.setBounds(150, 102, 117, 29);
contentPane.add(btnStartGame);
}
顺便说一下,您应该尝试使用布局管理器。
我尝试将isValidUsername作为一个独立的方法,但它没有给出有效的结果。即使是有效的名字,它也会给出错误的名字。我把它改成了下面的,效果很好:
public static boolean isValidUsername(String name)
{
String regex = "^[a-zA-Z]{5,29}$"; //Removed ^[aA-zZ]\\w{5,29}$
Pattern p = Pattern.compile(regex);
if (name == null) {
return false;
}
Matcher m = p.matcher(name);
return m.matches();
}
此外,由于代码是在点击按钮btnStartGame后执行的,因此该按钮不可能被禁用(禁用的按钮不会触发事件(。因此,我认为检查if(btnStartGame.isEnabled(((是多余的(我可能错了(。因此,您只需检查:
if(sValidUsername(field)){
//start game here
} else {
//display prompt here
}