当我尝试使用 if 语句将两个单独的单词打印到控制台时,无论输入什么,它始终只返回一个单词。
我的完整代码:
public class Frame{
//Declaring needed text variables
//Text Pane
static JFormattedTextField formattedTextField = new JFormattedTextField();
static String username = formattedTextField.getText();
//Frame
static JFrame UsernameFrame = new JFrame("Welcome");
/**
* @wbp.parser.entryPoint
*/
public static void frame(){
/**
* Configuring the Frame
*/
UsernameFrame.setSize(400,150);
UsernameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UsernameFrame.getContentPane().setLayout(null);
/**
* Configuring Submit button
*/
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(0, 89, 384, 23);
btnSubmit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(username.length() < 5){
System.out.println("no");
}
if(username.length() > 5){
System.out.println("continue");
}
//Execute when the button is pressed
System.out.println("The button has been pressed");
}
});
UsernameFrame.getContentPane().add(btnSubmit);
/**
* Configuring user-name text pane
*/
JLabel lblEnterYourSelected = new JLabel("Enter your selected username below");
lblEnterYourSelected.setHorizontalAlignment(SwingConstants.CENTER);
lblEnterYourSelected.setBounds(81, 11, 214, 14);
UsernameFrame.getContentPane().add(lblEnterYourSelected);
/**
* Configuring content pane
*/
formattedTextField.setBounds(129, 36, 120, 20);
UsernameFrame.getContentPane().add(formattedTextField);
UsernameFrame.setVisible(true);
}
}
这是我正在使用的特定线路
if(username.length() < 5){
System.out.println("no");
}
if(username.length() > 5){
System.out.println("continue");
}
我一直在寻找解决方案,但我找不到。