-1

我基本上做了这个东西,用户突出显示食物列表并按下“>>”按钮,将食物从左侧传输到右侧,反之亦然,使用“<<”按钮。我真正遇到的唯一麻烦就是这样做。我走得足够远,使它出现在指定的列表上,但它只将其中一种食物转移到列表中。例如,如果您选择了“Tuna”和“Mayo”,那么只有“Tuna”会被转移到适当的列表中。继承人的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.*;
import java.util.List;

import javax.swing.JTextField;

public class MAIN extends JFrame {

    Button ltor, rtol;
    JList homelist, shoppinglist;
    DefaultListModel homefoodlist = new DefaultListModel();
    DefaultListModel shoppingfoodlist = new DefaultListModel();
    JTextField foodlog;

    String[] hfood = { "Tuna", "Mayo", "Ketchup", "Sun Flower Oil", "Buscuits",
            "Cookies", "Turkey" };
    String[] sfood = { "Chocolate", "bread", "Milk", "Toast", "Beef", "Chicken" };

    public static void main(String[] args) {

        new MAIN();

    }

    private MAIN() {
        JPanel thepanel = new JPanel();
        thehandler handler = new thehandler();

        this.setLocationRelativeTo(null);
        this.setSize(400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle("Shopping List");
        this.add(thepanel);

        // Creating the Home List(left list)
        for (String homefood : hfood) {
            homefoodlist.addElement(homefood);
        }

        homelist = new JList(homefoodlist);
        homelist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        thepanel.add(homelist);

        // Buttons for moving lists from left to right
        ltor = new Button(">>");
        thepanel.add(ltor);
        ltor.addActionListener(handler);

        rtol = new Button("<<");
        rtol.addActionListener(handler);
        thepanel.add(rtol);

        // Creating the Shopping list(right list)
        for (String shoppingfood : sfood) {
            shoppingfoodlist.addElement(shoppingfood);
        }
        shoppinglist = new JList(shoppingfoodlist);
        shoppinglist
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        thepanel.add(shoppinglist);

    }

    // ActionListener

    private class thehandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // The HomeList to the ShoppingList
            if (e.getSource() == ltor) {
                if (homelist.isSelectionEmpty() == false) {
                    for(String x : hfood) {
                        if (x == homelist.getSelectedValue()) {
                            shoppingfoodlist.addElement(x);
                        }

                    }
                }

            }
}
}
}

编辑 == 刚刚修复它但由于某种原因我不能做相反的事情(例如从购物清单到家庭清单。这是我的动作监听器代码

if (e.getSource() == ltor) {
                if (homelist.isSelectionEmpty() == false) {
                    for (String x : hfood) {
                        for (String y : homelist.getSelectedValuesList()) {
                            if (x.equals(y)) {
                                shoppingfoodlist.addElement(x);
                                homefoodlist.removeElement(x);
                                continue;
                            }
                        }
                    }

                    if (e.getSource() == rtol) {
                        if (shoppinglist.isSelectionEmpty() == false) {

                            for (String x : sfood) {
                                for (String y : shoppinglist
                                        .getSelectedValuesList()) {
                                    if (x.equals(y)) {
                                        homefoodlist.addElement(x);
                                        shoppingfoodlist.removeElement(x);
                                    }
                                }
                            }

                        }
                    }
4

4 回答 4

1

你在正确的轨道上,但是:

for(String x : hfood) {
  if (x == homelist.getSelectedValue()) {
    shoppingfoodlist.addElement(x);
  }
}

如果您从第一个列表中选择两个项目并移动它们,这仅适用于第一个选定的项目。您需要确保跟踪所有选定的项目,并适当地抓取它们。

另一件事,看起来您正在将Strings 与==.. 进行比较,不要在 Java 中这样做。利用.equals()

for(String x : hfood) {
  for (String y : homelist.getSelectedValuesList()) {
    if (x.equals(y)))
      shoppingfoodlist.addElement(x);
  }
}
于 2013-10-25T17:46:37.067 回答
1

我将使用该getSelectedListValues()方法返回所有选定对象的列表。

然后我将遍历这个列表,a) 从第一个 JList 的 DefaultListModel 中删除每个项目,然后 b) 将项目添加到第二个 JList 的模型中。

DefaultListModel model1 = (DefaultListModel)list1.getModel();
DefaultListModel model2 = (DefaultListModel)list2.getModel();

for (Object item: list1.getSelectedListValues())
{
    model1.removeElement(item);
    model2.addElement(item);
}
于 2013-10-25T17:50:16.600 回答
0

所以这就是你弄错的地方

for(String x : hfood) {
      if (x == homelist.getSelectedValue()) {
        shoppingfoodlist.addElement(x);
      }
    }

你应该尝试这样的事情

ArrayList(或任何适用于此的数据结构) selectedValues = hfood.getSelectedValues();

for(String x : selectedValues) {

        shoppingfoodlist.addElement(x);

    }
于 2013-10-25T17:55:28.437 回答
0

您需要 getSelectedValues(),它返回一个数组,而不是 getSelectedValue(),它只返回第一个选定的值。

于 2013-10-25T17:47:47.357 回答