我制作了一个带有 4 个选项的简单 JList,并且在 JList 旁边有一个 JTextField。如何从 JList 中获取用户的选择以显示在 JTextField 中?(代码已被编辑以包含 Listener 类)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JListExample extends JFrame
{
private JPanel p1, p2;
private JList jList; // instance variables
private JScrollPane scrollPane;
private JTextField jtfChoice;
public JListExample() // constructor
{
String[] itemList = {"alpla", "beta", "gamma", "delta", "omega"}; // array of Strings for list of items
jList = new JList(itemList);
jList.setSelectedIndex(1); // default item selected
jList.setVisibleRowCount(3); // no. of visible rows
jList.setSize(220, 200);
p1 = new JPanel();
p1.add(jtfChoice = new JTextField(8), BorderLayout.CENTER);
p2 = new JPanel();
p2.add(scrollPane = new JScrollPane(jList), BorderLayout.WEST);
p2.add(p1);
add(p2, BorderLayout.EAST);
ListenerClass ListSelectionListener = new ListenerClass();
jList.addListSelection(listener);
}
public static void main(String[] args)
{
JListExample frame = new JListExample(); // new frame object
frame.setTitle("JList Example"); // set frame title
frame.pack(); // sizes the frame so components fit frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ends program on frame closing
frame.setLocationRelativeTo(null); // centre frame
frame.setVisible(true); // make frame visible
}
private class ListenerClass implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
JTextField.setText();
}
}
}