我正在创建一个简单的GUI表单,其中我有两个文本,一个用于名称,另一个用于电话号码,我正在尝试验证它们并在点击按钮时将它们设置为空。 但是再次单击按钮,我不想要的JOptionPane打开,请帮助我这个方法。
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
public class Form {
public static void main(String args[]) {
final JFrame frame = new JFrame("Hello");
final JButton btnOne = new JButton("save");
final JTextField jname = new JTextField(8);
final JTextField jphno = new JTextField(8);
final JLabel lname = new JLabel("Name");
final JLabel lphno = new JLabel("Phone no");
frame.getContentPane().setBackground(new Color(58, 192, 126));
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
btnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jname.getText().length() <= 2) {
JOptionPane.showMessageDialog(null,
"please enter at least Three digits");
}
}
});
btnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jphno.getText().trim().length() != 10) {
JOptionPane.showMessageDialog(null,
"Please enter Valid Phone no");
}
}
});
btnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int j = jphno.getText().length();
int h = jname.getText().length();
if (e.getSource() == btnOne && j == 10 && h >= 3) {
jname.setText("");
jphno.setText("");
}
}
});
jname.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent fe) {
if (fe.getSource() == jname) {
jname.setText("");
}
}
});
jphno.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent fe) {
if (fe.getSource() == jphno) {
jphno.setText("");
}
}
});
// adding components
frame.setLayout(gbl);
gbc.anchor = gbc.WEST;
gbc.insets = new Insets(0, 30, 20, 30);
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(lname, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
frame.add(jname, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
frame.add(lphno, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
frame.add(jphno, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
frame.add(btnOne, gbc);
btnOne.setBackground(Color.CYAN);
btnOne.setBackground(Color.GRAY);
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
答案 0 :(得分:0)
您正在向同一按钮添加多个动作侦听器。结果,当按下按钮时,所有这些都将触发。在这种情况下,似乎最后一个侦听器被触发,这会重置导致其他侦听器抱怨的数据。
只需将您的代码合并到一个动作侦听器中即可。
或者,根据mKorbel的建议,您可能希望调整您的方法,并使用DocumentListener
来验证JTextField
on the fly的内容。
答案 1 :(得分:0)
以下代码应该适合您 - 将所有三个动作侦听器合并为一个。
btnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//get length of the input fields
int phoneInputLength = jphno.getText().length();
int nameInputLength = jname.getText().length();
//Check the name and phone number are the correct length first
if (phoneInputLength != 10 && nameInputLength <= 2) {
JOptionPane.showMessageDialog(null, "Please enter a name that is at least three characters" +
"\nand enter a valid phone no (10 digits long)");
} else if (phoneInputLength != 10) {
JOptionPane.showMessageDialog(null, "Please enter Valid Phone no");
} else if (nameInputLength <= 2) {
JOptionPane.showMessageDialog(null,
"please enter at least Three digits");
} else {
jname.setText("");
jphno.setText("");
}
}
});
此外,您不需要该行:
if (e.getSource() == btnOne)
当你通过line:
专门为btnOne组件添加一个动作监听器时 btnOne.addActionListener
我还会做一些验证,以确保输入框中给出的电话号码只包含数字。
之类的东西try {
int number = Integer.parseInt(jphno.getText());
//success - then you can make the text field empty
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "Phone numbers can only contain digits. " +
"Please remove any characters that are not digits!",
"Invalid Phone Number", JOptionPane.ERROR_MESSAGE);
}