Java JList问题 [英] Java JList problems

查看:126
本文介绍了Java JList问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我想做的是允许用户自己创建一个列表,无论他们在TextField中键入什么内容,其输出都将显示在Jlist中,但是我的问题是,如果我键入另一个单词到TextField的输出是添加或替换已经存在的另一个单词,它应该位于另一个单词的下方并保存在那里,任何人都可以帮助我吗?

Ok what am trying to do is allow the user to make a list for themselves, what ever they type in in the TextField the output of that will be shown in the Jlist but my problem here is that if i type in another word to the TextField the output of that is either appending or replacing the other word that was already there it suppose to go beneath the other word and save there can anyone help me please??

    public lala(){

    b2 = new JButton("ADD");
    b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        model.removeAllElements();
        list1.setModel(model);

       }
    });

    b3 = new JButton("MOVE");
    b3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
          model = new DefaultListModel<String>(); 
          model.addElement(field.getText());
          list.setModel(model);
          field.setText("");

        }
    });

    list = new JList<String>();
    list.setFixedCellHeight(10);
    list.setFixedCellWidth(10);
    list.setVisibleRowCount(10);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    scroll = new JScrollPane(list);
    scroll.setPreferredSize(new Dimension(100,100));

    field = new JTextField(19);
    field.setToolTipText("Input Text Area Here");
    field.setFont(new Font("Corier",Font.BOLD,20));
    field.setBackground(Color.BLACK);
    field.setForeground(Color.RED);
    field.setDragEnabled(true);

    panel = new JPanel();
    panel.setBackground(Color.BLACK);

    panel.add(b3);
    //panel.add(b2);
    panel.add(field);
    panel.add(scroll);
    add(panel);

      } 
    }


推荐答案

您的问题是,您正在事件侦听器中的每个操作上创建一个全新的 DefaultListModel

Your issue is that you are creating a whole new DefaultListModel on each Action in the event listener.

您需要在用户按下按钮时为其声明一个全局DefaultListModel并为其添加 addElement()

You need to declare a global DefaultListModel and addElement() to it as your user presses the button.

能够帮助您指出正确的方向:

This might be able to help point you in the right direction:

public class Examples {

    private DefaultListModel modelList;
    private JList list;
    private JButton button;
    private JTextField field;

    public Examples() {
        modelList = new DefaultListModel();
        list = new JList(modelList);
        button = new JButton("Add To List");
        field = new JTextField();
        init();
    }

    private void init() {
        button.addActionListener((ActionEvent e) -> {
            modelList.addElement(field.getText());
            // !! list.setModel(modelList);
            field.setText("");
        });
    }

}

在这里,我们已经注册了DefaultListModel作为我们 Examples 类中的实例字段。

Here, we have registered our DefaultListModel as a instance field in our Examples class.

然后,我们使用lambda表达式注册一个新的侦听器,并具有使用字段文本更新的modelList,并将modelList设置为JList的模型。

Then we register a new listener using the lambda expression, and have the modelList updated with the field's text, and set the modelList as the model for the JList.

这篇关于Java JList问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆