如何从 JComboBox 中获得与多个数组选择相关的选择?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
class ContactsReader extends JFrame
{
public JPanel mainPanel;
public JPanel buttonPanel;
public JPanel displayPanel;
public JLabel titleLabel;
public JLabel nameLabel;
public JLabel ageLabel;
public JLabel emailLabel;
public JLabel cellPhoneLabel;
public JLabel comboBoxLabel;
public JButton exitButton;
public JTextField nameTextField;
public JTextField ageTextField;
public JTextField emailTextField;
public JTextField cellPhoneTextField;
public JComboBox<String> contactBox;
public String[] getContactNames;
public String[] displayContactNames;
public File contactFile;
public Scanner inputFile;
public String selection;
public ContactsReader()
{
super("Contacts Reader");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildPanel();
add(mainPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void buildPanel()
{
titleLabel = new JLabel("Please enter contact information");
mainPanel = new JPanel(new BorderLayout());
buttonPanel();
displayPanel();
mainPanel.add(titleLabel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(displayPanel, BorderLayout.CENTER);
}
public void buttonPanel()
{
//create submit and exit buttons
exitButton = new JButton("Exit");
exitButton.addActionListener(new exitButtonListener());
buttonPanel = new JPanel(); //create button panel
buttonPanel.add(exitButton); //add exit button to panel
}
public void displayPanel()
{
String nameHolder;
int count = 0;
nameLabel = new JLabel("Name");
ageLabel = new JLabel("Age)");
emailLabel = new JLabel("Email");
cellPhoneLabel = new JLabel("Cell Phone #");
comboBoxLabel = new JLabel("Select a Conact");
nameTextField = new JTextField(10);
nameTextField.setEditable(false);
ageTextField = new JTextField(10);
ageTextField.setEditable(false);
emailTextField = new JTextField(10);
emailTextField.setEditable(false);
cellPhoneTextField = new JTextField(10);
cellPhoneTextField.setEditable(false);
try{
contactFile = new File("ContactData.txt");
inputFile = new Scanner(contactFile);
}
catch (Exception event){}
while (inputFile.hasNext())
{
nameHolder = inputFile.nextLine();
count++;
}
inputFile.close();
String getContactNames[] = new String[count];
String displayContactNames[] = new String[count/4];
try{
contactFile = new File("ContactData.txt");
inputFile = new Scanner(contactFile);
}
catch (Exception event){}
for (int i = 0; i < count; i++)
{
if (inputFile.hasNext())
{
nameHolder = inputFile.nextLine();
getContactNames[i] = nameHolder;
if (i % 4 == 0)
{
displayContactNames[i/4] = getContactNames[i];
}
}
}
inputFile.close();
contactBox = new JComboBox<String>(displayContactNames);
contactBox.setEditable(false);
contactBox.addActionListener(new contactBoxListener());
displayPanel = new JPanel(new GridLayout(10,1));
displayPanel.add(comboBoxLabel);
displayPanel.add(contactBox);
displayPanel.add(nameLabel);
displayPanel.add(nameTextField);
displayPanel.add(ageLabel);
displayPanel.add(ageTextField);
displayPanel.add(emailLabel);
displayPanel.add(emailTextField);
displayPanel.add(cellPhoneLabel);
displayPanel.add(cellPhoneTextField);
}
private class exitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0); //set exit button to exit even when pressed
}
}
private class contactBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//get selection from dropdown menu
selection = (String) contactBox.getSelectedItem();
}
}
public static void main(String[] args)
{
new ContactsReader(); //create instance of Contact Reader
}
}
我希望选择将姓名、年龄、电子邮件和手机号码发送到相应的文本字段。我可以找到一个选择,但不知道如何让它选择正确的数组选择并将其发送到文本字段。