如何修复数组的输出方式

时间:2013-10-22 19:34:46

标签: java user-interface

好的伙计们。这是一个新手。刚刚创建了这个东西,用户从“主页列表”中选择食物并点击“>>”按钮将其添加到左侧列表中的“购物清单”,反之亦然。虽然当用户在选择按钮后单击按钮时会开始变得有点狡猾,但效果很好。它再次打印出整个列表,也显示为数组。我只想将选定的值添加到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");
                        }
                }
            }
        }

}

2 个答案:

答案 0 :(得分:0)

就像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等以及变量低级的camel-cased:shoppingFoodList,homeFoodList等。你可以阅读更多here
  • 这是debatable,因为每个人都有自己的方式。但我相信要使代码更具可读性,你应该只在必要时否定条件(如果你不需要else分支),否则只需将最简单的条件放在if中并继续使用流程并描述像我上面用消息对话框
  • 那样的逻辑
  • 一旦你让代码工作,你可以开始重构一些东西,比如if blocks和[{1}}执行的>><<的{​​{1}}常见和重复,但我想这可能是现在太多信息

祝你好运从newb升级为职业

答案 1 :(得分:0)

您需要在代码中替换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))。