检查 JTextField 是否不为空
Checking if JTextField not empty
我正在尝试为 GUI 创建示例,它从 JTextFiled 中的用户那里获取名称,并为用户显示在 JtextField 中输入的名称的消息,现在我想检查用户是否在按钮上输入而不输入任何事情,我试图在 ActionListener 中使用此方法,但我在编辑器中看到错误,而当我在 ActionListener 之外使用它时,我发现它有效! ,请看附件图片
public class Example01 extends JFrame {
public JTextField text;
public Example01() {
setTitle("Example 01");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
JLabel label = new JLabel("Enter Your Name : ");
text = new JTextField();
text.setSize(30, 10);
JButton btn1 = new JButton("Enter");
btn1.addActionListener(new ActionListener() {
if (text.getText().equals("")) {
JOptionPane.showMessageDialog(rootPane, "Please enter anything");
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(rootPane, "Hello : " + text.getText());
}
});
panel.add(label);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(text);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(btn1);
add(panel);
setVisible(true);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
error message
编辑
问题是因为我将代码放在可执行上下文之外 "a method"
拿...
if (text.getText().equals("")) {
JOptionPane.showMessageDialog(rootPane, "Please enter anything");
}
并将其放入您的 actionPerformed
方法中...
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (text.getText().equals("")) {
JOptionPane.showMessageDialog(rootPane, "Please enter anything");
}
JOptionPane.showMessageDialog(rootPane, "Hello : " + text.getText());
}
});
我正在尝试为 GUI 创建示例,它从 JTextFiled 中的用户那里获取名称,并为用户显示在 JtextField 中输入的名称的消息,现在我想检查用户是否在按钮上输入而不输入任何事情,我试图在 ActionListener 中使用此方法,但我在编辑器中看到错误,而当我在 ActionListener 之外使用它时,我发现它有效! ,请看附件图片
public class Example01 extends JFrame {
public JTextField text;
public Example01() {
setTitle("Example 01");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
JLabel label = new JLabel("Enter Your Name : ");
text = new JTextField();
text.setSize(30, 10);
JButton btn1 = new JButton("Enter");
btn1.addActionListener(new ActionListener() {
if (text.getText().equals("")) {
JOptionPane.showMessageDialog(rootPane, "Please enter anything");
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(rootPane, "Hello : " + text.getText());
}
});
panel.add(label);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(text);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(btn1);
add(panel);
setVisible(true);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
error message
编辑
问题是因为我将代码放在可执行上下文之外 "a method"
拿...
if (text.getText().equals("")) {
JOptionPane.showMessageDialog(rootPane, "Please enter anything");
}
并将其放入您的 actionPerformed
方法中...
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (text.getText().equals("")) {
JOptionPane.showMessageDialog(rootPane, "Please enter anything");
}
JOptionPane.showMessageDialog(rootPane, "Hello : " + text.getText());
}
});