[英]How can I get JList item to be displayed in a JTextField
I have made a simple JList with 4 options, and I have a JTextField beside the JList. 我创建了一个带有4个选项的简单JList,并且在JList旁边有一个JTextField。 How can I get the user's choice from the JList to be displayed in the JTextField?
如何让JList中的用户选择显示在JTextField中? (Code has been edited to include Listener class)
(代码已编辑为包含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();
}
}
}
You're referencing the class name, not the variable: 您引用的是类名,而不是变量:
JTextField.setText();
What you want is: 你想要的是:
jtfChoice.setText();
Also, you're importing the awt
events, when you should be importing the swing
events: 此外,当您导入
swing
事件时,您正在导入awt
事件:
import javax.swing.event.*;
Also, you never declare listener
: 此外,您永远不会声明
listener
:
ListenerClass ListSelectionListener = new ListenerClass();
jList.addListSelection(listener); //listener doesn't exist
Here's what you should be doing: 这是你应该做的:
ListenerClass listener = new ListenerClass();
jList.addListSelectionListener(listener);
All told, the final, functional class looks like this: 总而言之,最终的功能类看起来像这样:
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());
}
}
}
On a completely unrelated note: your comments don't really add anything. 在一个完全不相关的注释:你的评论并没有真正添加任何东西。 When you say something like:
当你说的话:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ends program on frame closing
All you're doing is duplicating what the code already says. 您所做的只是重复代码已经说明的内容。 Good code should be self-documenting, and comments should explain the why and the how , not the what .
好的代码应该是自我记录的,评论应该解释原因和方法 ,而不是什么 。
您只需添加一个ListSelectionListener ,您可以在其中更新JTextField。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.