我基本上做了这件事,用户突出显示食物清单并按下'>>'按钮将食物从左手侧转移到右手侧,反之亦然,用'<<<按钮。 我遇到的唯一麻烦就是这样做。我走得足够远,使它出现在指定的清单上,但它只将其中一种食物转移到清单中。例如。如果你选择了'金枪鱼'和'梅奥',那么只有'金枪鱼'会被转移到适当的名单。 下面是代码:
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);
}
}
}
}
}
答案 0 :(得分:1)
你走在正确的轨道上,但是:
for(String x : hfood) {
if (x == homelist.getSelectedValue()) {
shoppingfoodlist.addElement(x);
}
}
如果您从第一个列表中选择两个项目并移动它们,则这仅适用于第一个选定项目。您需要确保跟踪所有选定的项目,并适当地抓住它们。
另一件事,看起来你正在将String
与==
进行比较..不要在Java中这样做。使用.equals()
for(String x : hfood) {
for (String y : homelist.getSelectedValuesList()) {
if (x.equals(y)))
shoppingfoodlist.addElement(x);
}
}
答案 1 :(得分:1)
我会使用getSelectedListValues()
方法返回所有选定对象的列表。
然后我将遍历此List并a)从第一个JList的DefaultListModel中删除每个Item,并b)将Item添加到第二个JList的模型中。
DefaultListModel model1 = (DefaultListModel)list1.getModel();
DefaultListModel model2 = (DefaultListModel)list2.getModel();
for (Object item: list1.getSelectedListValues())
{
model1.removeElement(item);
model2.addElement(item);
}
答案 2 :(得分:0)
你想要getSelectedValues(),它返回一个数组,而不是getSelectedValue(),它只返回第一个选定的值。
答案 3 :(得分:0)
所以这就是你弄错了
for(String x : hfood) {
if (x == homelist.getSelectedValue()) {
shoppingfoodlist.addElement(x);
}
}
你应该尝试这样的事情
ArrayList(或适用于此的任何数据结构)selectedValues = hfood.getSelectedValues();
for(String x : selectedValues) {
shoppingfoodlist.addElement(x);
}