0

我正在创建一个简单的 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);
    }
}
4

2 回答 2

0

您正在向同一个按钮添加多个动作侦听器。结果,当按下按钮时,它们都会触发。在这种情况下,似乎最后一个侦听器首先被触发,这会重置数据,导致其他侦听器抱怨。

只需将您的代码组合成一个动作监听器。

或者,正如mKorbel所建议的,您可能希望调整您的方法并使用 aDocumentListener来验证您JTextField的 s内容。

于 2013-04-30T11:24:23.680 回答
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)

当您通过以下行专门为 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);
}
于 2013-04-30T12:19:56.057 回答