2012-02-24 182 views
2

之前,我這裏有一個代碼,以驗證電話號碼驗證電話號碼保存

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!

+2

根據您提供的信息,很難回答您的問題。但是,您需要一些方法從您的操作中檢索電話號碼,然後以某種方式批准或拒絕電話號碼,以便應用程序可以保存該電話號碼或在出現問題時提醒用戶。 – Bill 2012-02-24 04:44:32

+0

對不起。我是新來的Java。我想要做的就是防止用戶保存無效的電話號碼格式。您有任何建議 – mix 2012-02-24 05:35:31

+0

您好@mix,萬一您發現我的答案有用,您能接受嗎?它已經3年了...... :) – FSp 2015-10-13 10:24:13

回答

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 
      ... 
      } 
     }); 
     ... 
    } 
} 
+0

你也可以使用匿名類。 – Avi 2012-02-24 04:48:37

+0

你好@FSp謝謝你回答我的問題,但我想如果這 類ButtonListener實現的ActionListener澄清{ 公共無效的actionPerformed(ActionEvent的AE){// 您在這裏的代碼.... }} 應 公共類庫存裏面放延伸javax.swing.JFrame中 內外 私人無效saveButton3ActionPerformed(EVT java.awt.event.ActionEvent中){...} 對不起,我在Java :( – mix 2012-02-24 05:34:08

+0

是新@混合簡短的回答你的問題是:是的,我提供了一個更詳細的例子,希望它澄清 – FSp 2012-02-24 06:39:30

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); 
    } 
} 
+0

你好@john謝謝你給我一個答案。我只是想澄清,似乎我是新的Java,我不明白。你上面顯示的代碼,我應該把它們放在裏面 private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt){...} – mix 2012-02-24 05:32:08