我怎样才能改变这个用于joptionpane?

时间:2016-03-30 11:10:13

标签: java

我不知道从哪里开始......有哪些提示?

 public static void search(String searchString, String type, int numberOfContacts, Person[] contacts) {

int found = 0;
int[] results = new int[numberOfContacts];

if (type.equals("name")) {
    // name
    for (int x = 0; x < numberOfContacts; x++) {
        if (contacts[x].getName().contains(searchString)) {
            results[found] = x;
            found++;
        }
    }
} else {
    // number
    for (int x = 0; x < numberOfContacts; x++) {
        if (contacts[x].getPhone().contains(searchString)) {
            results[found] = x;
            found++;
        }
    }
}

System.out.println("\n\t**************");
System.out.println("\tSearch Results");
System.out.println("\t**************");
System.out.println("Found " + found + " results containing \"" + searchString + "\":");
System.out.println();

if (found > 0) {
    for (int x = 0; x < found; x++) {
        System.out.println(contacts[results[x]].getName() + "\t" + contacts[results[x]].getPhone());
    }
}

System.out.println("\n\n\n");

我被困了

1 个答案:

答案 0 :(得分:0)

试试这个,它会显示一个String项列表。用户可以选择或取消。

if (found > 0) {
        //prepare items
        String[] persons = new String[found];
        int defaultSelection = 0;

        for (int x = 0; x < found; x++) {
            persons[x] = contacts[results[x]].getName() + "\t" + 
                    contacts[results[x]].getPhone();
        }

        //show JOptionPane
        String selection = (String) JOptionPane.showInputDialog(
                null, "Please choose", "Title", JOptionPane.QUESTION_MESSAGE, 
                null, persons, persons[defaultSelection]);

        if (selection == null) {
            System.out.println("No item selected");
        } else {
            System.out.println("User selected : " + selection);
        }
    }

您可能希望实现用于搜索联系人的文本字段,并在列表中显示搜索结果。在这个cas中,我会使用Window而不是JOptionPan。

public class Window extends JFrame implements ActionListener{

private JList<String> listOfResult;
private JTextField searchInput;
private JButton submit;

public Window() {
    //prepar window
    setTitle("Title");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 600);

    //prepar search field
    searchInput = new JTextField("search persion here");
    submit = new JButton("Search");
    submit.addActionListener(this);

    //init list
    listOfResult = new JList<String>();

    //organise components with panels and layouts and add them to the window
    this.getContentPane().setLayout(new BorderLayout());

    JPanel northPanel = new JPanel();
    northPanel.add(searchInput);
    northPanel.add(submit);

    this.add(northPanel, BorderLayout.NORTH);
    this.add(listOfResult, BorderLayout.CENTER);

    //show window
    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    //this method is called on 'submit' button click
    Person[] searchResult = doSearch(searchInput.getText());
    PersonListModel personList = new PersonListModel(searchResult);
    listOfResult.setModel(personList);
}

public Person[] doSearch(String searchedString) {//implement your code here}

public class PersonListModel implements ListModel<String>{

    private Person[] contacts;

    public PersonListModel(Person[] contacts) {
        this.contacts = contacts;
    }

    @Override
    public int getSize() {
        return contacts.length;
    }

    @Override
    public String getElementAt(int index) {
        return contacts[index].getName() + "\t" + contacts[index].getPhone();
    }

    @Override
    public void addListDataListener(ListDataListener l) {/*not implemented*/}
    @Override
    public void removeListDataListener(ListDataListener l) {/*not implemented*/}
}
}