我这里有一个验证电话号码的代码
public class ValidatePhoneNumber {
public static void main(String[] argv) {
String phoneNumber = "1-(80..2)-321-0361";
System.out.println(phoneNumber.length());
String regex = "^\\+?[0-9. ()-]{10,25}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
System.out.println("Phone Number Valid");
} else {
System.out.println("Phone Number must be in the form XXX-XXXXXXX");
}
}
}
如何将此代码放在SAVEBUTTON ACTION中?
private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt) {...}
阻止用户保存无效的电话号码格式
谢谢!
答案 0 :(得分:4)
假设您在类public class Inventory extends javax.swing.JFrame
中编写代码(正如您在评论中指定的那样),我会写一个ActionListener
来处理按钮点击事件
public class Inventory extends javax.swing.JFrame
private String currPhoneNumber; // it may contains something like "1-(80..2)-321-0361"
...
// this code can be *within* your class, but *outside* any method declaration
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
System.out.println(currPhoneNumber.length());
String regex = "^\\+?[0-9. ()-]{10,25}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(currPhoneNumber);
}
if (matcher.matches()) {
System.out.println("Phone Number Valid");
} else {
System.out.println("Phone Number must be in the form XXX-XXXXXXX");
// throw an exception or do something that will prevent the data to be
// saved (what to do here really depends on the application you are writing)
}
}
...
private void createMyFancyInterface(...) {
...
JButton source = new JButton("Do something");
source.addActionListener(new ButtonListener());
...
}
}
或者(作为带下划线的人),您可以使用匿名类,从而将其减少到以下内容:
public class Inventory extends javax.swing.JFrame
private String currPhoneNumber; // it may contains something like "1-(80..2)-321-0361"
...
// this code can be *within* your class, but *outside* any method declaration
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
System.out.println(currPhoneNumber.length());
String regex = "^\\+?[0-9. ()-]{10,25}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(currPhoneNumber);
}
if (matcher.matches()) {
System.out.println("Phone Number Valid");
} else {
System.out.println("Phone Number must be in the form XXX-XXXXXXX");
// throw an exception or do something that will prevent the data to be
// saved (what to do here really depends on the application you are writing)
}
}
...
private void createMyFancyInterface(...) {
...
JButton source = new JButton("Do something");
source.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// the same code as ButtonListener.actionPerformed above
...
}
});
...
}
}
答案 1 :(得分:0)
这是执行此操作的简单方法。
class JavaApplication extends JFrame implements ActionListener {
JTextArea area;
JButton button;
JTextField box;
JPanel panel;
public JavaApplication19() {
panel = new JPanel();
panel = (JPanel) getContentPane();
panel.setLayout(new FlowLayout());
area = new JTextArea(26, 30);
box = new JTextField(30);
button = new JButton("Submit");
button.addActionListener(this);
panel.add(area);
panel.add(button);
panel.add(box);
}
@Override
public void actionPerformed(ActionEvent e) {
String phoneNumber = area.getText();
System.out.println(phoneNumber);
String regex = "^\\+?[0-9. ()-]{10,25}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
box.setText("Phone Number Valid");
} else {
box.setText("Phone Number must be in the form XXX-XXXXXXX");
}
}
public static void main(String[] args) {
JavaApplication pad = new JavaApplication();
pad.setSize(500, 500);
pad.setVisible(true);
}
}