2017-08-09 42 views
0

我對Java很新穎,正在創建一個電話簿應用程序,允許用戶通過名稱或電話進行搜索。您可以按名稱搜索並獲取用戶電話號碼,反之亦然。我通過使用二維數組硬編碼用戶,我知道這並不是非常有效。我想創建一個按鈕,彈出打開一個JDialog,讓用戶輸入名稱和電話號碼,然後自動將用戶名和數字放入數組中。我知道爲了做到這一點,我可能需要使用一個Collection,比如arrayList或其他東西,但我想看看是否有人能夠以正確的最有效的方向引導我。這是到目前爲止我的代碼如何有效地添加Java成員按鈕

public class PhoneBookGUI implements ActionListener{ 
JLabel name; 
JLabel number; 
JLabel searchOptions; 
JButton addButton; 
JTextField nameField; 
JTextField numberField; 
JCheckBox ignoreCase; 
JRadioButton exactMatch; 
JRadioButton startsWithButton; 
JRadioButton endsWithButton; 
String[][] phonelist = { 
    {"Connor Littleton", "707-799-0194"}, 
    {"Justin Littleton", "707-799-0182"}, 
    {"Zia Thach", "707-123-4567"}, 
    {"Mike Hawk", "707-123-1234"}, 
    {"Sal Hal", "707-111-1111"} 
}; 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
      new PhoneBookGUI(); 
     } 
    }); 
} 

PhoneBookGUI(){ 
    JFrame frame = new JFrame("Phone-Book GUI"); 
    frame.setSize(300, 500); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    frame.setLayout(new GridLayout(0,1)); 
    frame.setLocationRelativeTo(null); 

    name = new JLabel("Name:"); 
    number = new JLabel("Number:"); 
    searchOptions = new JLabel("Search Options:"); 
    nameField = new JTextField(10); 
    numberField = new JTextField(10); 
    ignoreCase = new JCheckBox("Ignore Case"); 
    exactMatch = new JRadioButton("Exact Match"); 
    startsWithButton = new JRadioButton("Starts With"); 
    endsWithButton = new JRadioButton("Ends With"); 
    JPanel buttonPane = new JPanel(); 
    addButton = new JButton("Add Contact"); 
    buttonPane.add(addButton); 
    ButtonGroup bg = new ButtonGroup(); 

    nameField.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent ae){ 
      numberField.setText(lookUpName(nameField.getText())); 
     } 
    }); 
    numberField.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent ae){ 
      nameField.setText(lookUpNumber(numberField.getText())); 
     } 
    }); 

    addButton.addActionListener(new java.awt.event.ActionListener() { 
      @Override 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      addButtonactionPerformed(evt); 
     } 
    }); 

    bg.add(exactMatch); 
    bg.add(startsWithButton); 
    bg.add(endsWithButton); 

    frame.add(name); 
    frame.add(nameField); 
    frame.add(number); 
    frame.add(numberField); 
    frame.add(new JLabel()); 
    frame.add(buttonPane); 
    frame.add(searchOptions); 
    frame.add(ignoreCase); 
    frame.add(new JLabel()); 
    frame.add(exactMatch); 
    frame.add(startsWithButton); 
    frame.add(endsWithButton); 

} 



public void addButtonactionPerformed(java.awt.event.ActionEvent evt){ 
    addButtonMenu menu = new addButtonMenu(new javax.swing.JFrame(), true); 
    menu.setVisible(true); 
} 

public String lookUpNumber(String n){ 
    for (int i = 0; i < phonelist.length; i++) { 
     if(phonelist[i][1].equals(n)){ 
      return phonelist[i][0]; 
     } 
    } 
    return "Not Found"; 
} 

public String lookUpName(String n){ 
    for (int i = 0; i < phonelist.length; i++) { 
     if(startsWithButton.isSelected()){ 
      if(ignoreCase.isSelected()){ 
       if(phonelist[i][0].toLowerCase().startsWith(n.toLowerCase())) 
        return phonelist [i][1]; 
        }else{ 
       if(phonelist[i][0].startsWith(n)) 
        return phonelist[i][1]; 
      } 
     } 
     else if(endsWithButton.isSelected()){ 
      if(ignoreCase.isSelected()){ 
       if(phonelist[i][0].toLowerCase().endsWith(n.toLowerCase())) 
        return phonelist[i][1]; 
      } else { 
       if(phonelist[i][0].endsWith(n)) 
        return phonelist[i][1]; 
      } 
     } 
     else { 
      if(ignoreCase.isSelected()) { 
       if(phonelist[i][0].toLowerCase().equals(n.toLowerCase())) 
        return phonelist[i][1]; 
       } else { 
        if(phonelist[i][0].equals(n)) 
         return phonelist[i][1]; 
      } 
     } 
    } 

    return "not found"; 
} 

@Override 
public void actionPerformed(ActionEvent ae){ 

} 

}

,這裏是我的其他頁面的Hashtable的所有JDialog

public class addButtonMenu extends javax.swing.JDialog { 

/** 
* Creates new form addButtonMenu 
*/ 
public addButtonMenu(java.awt.Frame parent, boolean modal) { 
    super(parent, modal); 
    setLocationRelativeTo(parent); 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    addFirstName = new javax.swing.JLabel(); 
    firstNameField = new javax.swing.JTextField(); 
    addLastName = new javax.swing.JLabel(); 
    lastNameField = new javax.swing.JTextField(); 
    addMemberButton = new javax.swing.JButton(); 
    cancelMember = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
    setTitle("Add Contact"); 
    setSize(new java.awt.Dimension(500, 200)); 
    getContentPane().setLayout(new java.awt.GridLayout(3, 1, 5, 5)); 

    addFirstName.setText("Full Name:"); 
    getContentPane().add(addFirstName); 
    getContentPane().add(firstNameField); 

    addLastName.setText("Phone Number:"); 
    getContentPane().add(addLastName); 

    lastNameField.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      lastNameFieldActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(lastNameField); 

    addMemberButton.setText("Submit"); 
    getContentPane().add(addMemberButton); 

    cancelMember.setText("Cancel"); 
    cancelMember.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      cancelMemberActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(cancelMember); 

    pack(); 
}// </editor-fold>       

private void lastNameFieldActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
}            

private void cancelMemberActionPerformed(java.awt.event.ActionEvent evt) {            
    this.dispose(); 
}            

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(addButtonMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(addButtonMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(addButtonMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(addButtonMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the dialog */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      addButtonMenu dialog = new addButtonMenu(new javax.swing.JFrame(), true); 
      dialog.addWindowListener(new java.awt.event.WindowAdapter() { 
       @Override 
       public void windowClosing(java.awt.event.WindowEvent e) { 
        System.exit(0); 
       } 
      }); 
      dialog.setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JLabel addFirstName; 
private javax.swing.JLabel addLastName; 
private javax.swing.JButton addMemberButton; 
private javax.swing.JButton cancelMember; 
private javax.swing.JTextField firstNameField; 
private javax.swing.JTextField lastNameField; 
// End of variables declaration     

}

回答

1

使用的概念,它存儲鍵/值在哈希表中配對。使用散列表時,可以指定一個用作要鏈接到該鍵的鍵(電話號碼)值(名稱)的對象。然後對密鑰進行散列,並將結果散列代碼用作索引,該值存儲在該表中。 通過以下鏈接使用收藏的想法,所以它很容易進行排序和搜索值的你,

JOptionPane - input dialog box program

或者

您可以使用數組,但它會是一個痛苦處理它,因爲在相同的數據類型的二維數組存儲值而電話號碼INT(驗證通過正則表達式)解析用作字符串。無論如何,請使用給定的鏈接來了解2D數組如何在JDialog中工作。

http://forums.devshed.com/java-help/958183-form-data-2d-array-output-text-post2924712.html