验证电话号码保存
之前,我这里有一个代码,以验证电话号码验证电话号码保存
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) {...}
,以防止用户保存电话号码格式无效
THANK YOU!
假如你是一类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
...
}
});
...
}
}
你也可以使用匿名类。 – Avi 2012-02-24 04:48:37
你好@FSp谢谢你回答我的问题,但我想如果这 类ButtonListener实现的ActionListener澄清{ 公共无效的actionPerformed(ActionEvent的AE){// 您在这里的代码.... }} 应 公共类库存里面放延伸javax.swing.JFrame中 内外 私人无效saveButton3ActionPerformed(EVT java.awt.event.ActionEvent中){...} 对不起,我在Java :( – mix 2012-02-24 05:34:08
是新@混合简短的回答你的问题是:是的,我提供了一个更详细的例子,希望它澄清 – FSp 2012-02-24 06:39:30
这是执行此操作的简单方法。
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);
}
}
你好@john谢谢你给我一个答案。我只是想澄清,似乎我是新的Java,我不明白。你上面显示的代码,我应该把它们放在里面 private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt){...} – mix 2012-02-24 05:32:08
根据您提供的信息,很难回答您的问题。但是,您需要一些方法从您的操作中检索电话号码,然后以某种方式批准或拒绝电话号码,以便应用程序可以保存该电话号码或在出现问题时提醒用户。 – Bill 2012-02-24 04:44:32
对不起。我是新来的Java。我想要做的就是防止用户保存无效的电话号码格式。您有任何建议 – mix 2012-02-24 05:35:31
您好@mix,万一您发现我的答案有用,您能接受吗?它已经3年了...... :) – FSp 2015-10-13 10:24:13