如何让JList项目显示在JTextField中

时间:2013-04-04 14:16:02

标签: java swing jtextfield jlist listselectionlistener

我创建了一个包含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();
    }
}
}

2 个答案:

答案 0 :(得分:2)

您正在引用类名,而不是变量:

JTextField.setText();

你想要的是:

jtfChoice.setText();

此外,当您要导入awt事件时,您正在导入swing个事件:

import javax.swing.event.*;

此外,您永远不会声明listener

ListenerClass ListSelectionListener = new ListenerClass();
jList.addListSelection(listener);  //listener doesn't exist

这是你应该做的:

ListenerClass listener = new ListenerClass();
jList.addListSelectionListener(listener);

总而言之,最终的功能类看起来像这样:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class JListExample extends JFrame {

    private JPanel p1, p2;
    private JList jList;
    private JScrollPane scrollPane;
    private JTextField jtfChoice;

    public JListExample() // constructor
    {
        String[] itemList = {"alpla", "beta", "gamma", "delta", "omega"};
        jList = new JList(itemList);
        jList.setSelectedIndex(1);
        jList.setVisibleRowCount(3);
        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 listener = new ListenerClass();
        jList.addListSelectionListener(listener);
    }

    public static void main(String[] args) {
        JListExample frame = new JListExample();
        frame.setTitle("JList Example");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private class ListenerClass implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            jtfChoice.setText(jList.getSelectedValue().toString());
        }
    }
}

完全不相关的说明:你的评论并没有真正添加任何内容。当你说出类似的话时:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ends program on frame closing

您所做的只是重复代码已经说明的内容。好的代码应该是自我记录的,评论应该解释为什么如何,而不是是什么

答案 1 :(得分:1)

您只需添加ListSelectionListener即可在其中更新JTextField。