如何修复数组的输出方式



好的伙计们。 这里的新手。刚刚创建了这个东西,用户从"家庭列表"中选择一种食物,然后单击">>"按钮将其添加到左侧的列表中,即"购物清单",反之亦然。它运行良好,尽管当用户在选择按钮后单击按钮时,它开始变得有点狡猾。它再次打印出整个列表,并且还显示为一个数组。我只想将选定的值添加到 JList 中。这是代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
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){
                    shoppingfoodlist.addElement(homefoodlist);
                    homefoodlist.remove(homelist.getSelectedIndex());
                    }else{
                        JOptionPane.showMessageDialog(null, "Select a food from either list");
                    }

                }
                if(e.getSource() == rtol){
                    if(shoppinglist.isSelectionEmpty() == false){
                        homefoodlist.addElement(shoppingfoodlist);
                        shoppingfoodlist.remove(shoppinglist.getSelectedIndex());
                        }else{
                            JOptionPane.showMessageDialog(null, "Select a food from either list");
                        }
                }
            }
        }
}

您需要替换 ActionListener 中的以下代码:

shoppingfoodlist.addElement(homefoodlist);
homefoodlist.remove(homelist.getSelectedIndex());

到:

List selectedValues = homelist.getSelectedValuesList();
for (Object object : selectedValues) {
    shoppingfoodlist.add(0,object);
    homefoodlist.remove(homelist.getSelectedIndex());
}

还有第二部分:

homefoodlist.addElement(shoppingfoodlist);
shoppingfoodlist.remove(shoppinglist.getSelectedIndex());

到:

List selectedValues = shoppinglist.getSelectedValuesList();
for (Object object : selectedValues) {
    homefoodlist.add(0,object);
    shoppingfoodlist.remove(shoppinglist.getSelectedIndex());
}

如您所见,您必须在两个地方进行更改。我建议使用这样的方法重构您的侦听器代码,以摆脱条件逻辑和重复:

class CustomActionListener implements ActionListener{
    JList source;
    JList sink;
    CustomActionListener(JList source, JList sink){
        this.source = source;
        this.sink = sink;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if( !source.isSelectionEmpty() ){
            List selectedValues = source.getSelectedValuesList();
            for (Object object : selectedValues) {
                DefaultListModel sinkModel =  (DefaultListModel) this.sink.getModel();
                sinkModel.add(0, object);
                DefaultListModel sourceModel = (DefaultListModel) this.source.getModel();
                sourceModel.remove(source.getSelectedIndex());
            }       
        }
    }
}

然后,您可以按如下方式使用它:

ltor.addActionListener(new CustomActionListener(homelist,shoppinglist));
rtol.addActionListener(new CustomActionListener(shoppinglist,homelist));

代码更简洁,您不必在ActionListener中维护两个位置(一个if(ltor)和第二个if(rtol))。

正如 iberbeu 指出的那样,我认为您只想将所选项目从一侧移动到另一侧,并且由于您允许多项选择,因此您应该迭代并添加所有选定项目:

//ActionListener
private class TheHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //The HomeList to the ShoppingList
        if(e.getSource() == ltor){
            if(homelist.isSelectionEmpty()){
                JOptionPane.showMessageDialog(null, "Select a food from either list");
            }else{
                for(int i : homelist.getSelectedIndices()){
                    shoppingFoodList.addElement(homeFoodList.get(i));
                    homeFoodList.remove(i);
                }
            }

        }
        if(e.getSource() == rtol){
            if(shoppingList.isSelectionEmpty()){
                JOptionPane.showMessageDialog(null, "Select a food from either list");
            }else{
                for(int i: shoppingList.getSelectedIndices()){
                    homeFoodList.addElement(shoppingFoodList.get(i));
                    shoppingFoodList.remove(i);
                }
            }
        }
    }
}

在它的时候,如果可以的话,我想给你几个建议:

  • 根据Java的命名约定,类应该是驼峰大小写:Main,TheHandler等,变量小写:shoppingFoodList,homeFoodList等。您可以在此处阅读更多内容
  • 这是值得商榷的,因为每个人都有自己的方式。但我相信为了使代码更具可读性,您应该仅在绝对必要时否定条件(例如,如果您不需要 else 分支),否则只需将最简单的条件放在if中并顺其自然并描述您的逻辑,就像我上面在消息对话框中所做的那样
  • 一旦你让代码工作,你就可以开始重构东西,比如>><<执行的操作if blocks常见和重复的,但我想这可能是现在太多的信息

祝你好运从新手升级为专业人士

最新更新