jlist选中在jtextfield显示,jtextfield输入显示在jlist
JTextFieldtextField=newJTextField();JListwordList=newJList();//为wordList列表框添加监听器wordL...
JTextField textField = new JTextField();
JList wordList = new JList();
//为wordList列表框添加监听器
wordList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
textField.setText(wordList.getSelectedValue().toString());//在执行过程中此语句,我选择列表框的选项,会出现空指向异常
}
});
//为textField文本域添加监听器
textField.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
this.doChanged();
}
public void insertUpdate(DocumentEvent e)
{
this.doChanged();
}
public void removeUpdate(DocumentEvent e)
{
this.doChanged();
}
private void doChanged()
{
String ch_text = textField.getText().trim();//文本框里的文本
DefaultListModel model = new DefaultListModel();
model.addElement(ch_text);
wordList.setModel(model);
}
上面的代码在执行过程中注释过的语句出现空指向异常,需要怎么解决
}); 展开
JList wordList = new JList();
//为wordList列表框添加监听器
wordList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
textField.setText(wordList.getSelectedValue().toString());//在执行过程中此语句,我选择列表框的选项,会出现空指向异常
}
});
//为textField文本域添加监听器
textField.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
this.doChanged();
}
public void insertUpdate(DocumentEvent e)
{
this.doChanged();
}
public void removeUpdate(DocumentEvent e)
{
this.doChanged();
}
private void doChanged()
{
String ch_text = textField.getText().trim();//文本框里的文本
DefaultListModel model = new DefaultListModel();
model.addElement(ch_text);
wordList.setModel(model);
}
上面的代码在执行过程中注释过的语句出现空指向异常,需要怎么解决
}); 展开
展开全部
public class Test extends JFrame {
private JTextField textField;
private JList list;
public Test() {
textField = new JTextField();
textField.addActionListener(getActionListener());
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement("测试");
list = new JList(model);
list.getSelectionModel().addListSelectionListener(getListSelectionListener());
setLayout(new BorderLayout());
add(textField, BorderLayout.NORTH);
add(list, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
setVisible(true);
}
public ActionListener getActionListener() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
if (!text.equals("")) {
DefaultComboBoxModel model = (DefaultComboBoxModel) list.getModel();
model.addElement(textField.getText());
}
}
};
}
public ListSelectionListener getListSelectionListener() {
return new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
Object text = list.getSelectedValue();
if (text != null) {
textField.setText(text.toString());
}
}
};
}
public static void main(String args[]) {
new Test();
}
}
private JTextField textField;
private JList list;
public Test() {
textField = new JTextField();
textField.addActionListener(getActionListener());
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement("测试");
list = new JList(model);
list.getSelectionModel().addListSelectionListener(getListSelectionListener());
setLayout(new BorderLayout());
add(textField, BorderLayout.NORTH);
add(list, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
setVisible(true);
}
public ActionListener getActionListener() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
if (!text.equals("")) {
DefaultComboBoxModel model = (DefaultComboBoxModel) list.getModel();
model.addElement(textField.getText());
}
}
};
}
public ListSelectionListener getListSelectionListener() {
return new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
Object text = list.getSelectedValue();
if (text != null) {
textField.setText(text.toString());
}
}
};
}
public static void main(String args[]) {
new Test();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个错误我刚搞懂,最近也在做项卖喊哪目,这样调用肯定是空指中码针异常的,我只告诉你方法,具体的你还要自己调试, 在界面控制器里添加一个界面的更新方法 然后在界面类里实现该更新方法 把你要更新的内容通过参数传过来 加入渗简setText();函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JTextField 有回车
JList有选中
JList有选中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询