-1

我对 Java 很陌生。

我创建了两个JLists,您可以在其中添加和删除“购物车”项目。

一旦用户添加了他们的所有项目,他们可以单击一个submit按钮在新窗口中查看他们选择的项目。

我的第一个列表是itemList(填充了数组中的项目),第二个列表是shoppinglist填充了用户使用JButton.

创建额外的数组来处理将项目移入和移出的按钮的操作JLists。我已经尝试了一些事情,但未能成功显示被选中的项目并在被击中shopinglist后显示在新窗口submit中。

任何帮助深表感谢。

//Create itemList
itemList = new JList(shopping);
contentPane.add(itemList);
itemList.setVisibleRowCount(10);
    itemList.setFixedCellHeight(20);
    itemList.setFixedCellWidth(140);
    itemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);


    //Add JScrollPane to maintain size
    JScrollPane list1 = new JScrollPane(itemList);
    //contentPane.add(list1);

    //Create shoppingList
    shoppingList = new JList(items);
    contentPane.add(shoppingList);
    shoppingList.setVisibleRowCount(10);
    shoppingList.setFixedCellHeight(20);
    shoppingList.setFixedCellWidth(140);
    shoppingList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    //Add JScrollPane to maintain size
    JScrollPane list2 = new JScrollPane(shoppingList);

    JPanel buttonPanel = new JPanel();

    //contentPane.add(list2);

    Buttonin = new JButton(">>");
    //Buttonin.setBounds(144, 46, 60, 23);
    Buttonin.addActionListener(this);
    buttonPanel.add(Buttonin);

    ButtonOut = new JButton("<<");
    //ButtonOut.setBounds(144, 80, 60, 23);
    ButtonOut.addActionListener(this);
    buttonPanel.add(ButtonOut);

    JPanel submitPanel = new JPanel();

    submitButton = new JButton("Submit");
    submitPanel.add(submitButton);

    submitButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent az) {
            JFrame frame = new JFrame ("Go Shopping!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MyPanel2());
            frame.pack();
            frame.setVisible(true);

        }
    });


    contentPane.add(list1);
    contentPane.add(buttonPanel);
    contentPane.add(list2);
    contentPane.setOpaque(true);
    contentPane.add(submitPanel);
    return contentPane;


}




public void actionPerformed(ActionEvent e) 
{
    int i = 0;

    //When in buttonin is pressed index and value of selected item is output to array

    if (e.getSource() == Buttonin)
    {

        int[] fromindex = itemList.getSelectedIndices();
        Object[] from = itemList.getSelectedValues();

        //add items to the shoppingList
        for (i = 0; i < from.length; i++)
        {
            items.addElement(from[i]);

        }
        //Must remove items that are selected from the itemList
        for (i = (fromindex.length-1); i >= 0; i--)
        {
            shopping.remove(fromindex[i]);
        }

    }   


    //When out button is pressed index and value of selected item is output to new array

    else if (e.getSource() == ButtonOut)
    {
        Object[] to = shoppingList.getSelectedValues();
        int [] toindex = shoppingList.getSelectedIndices();

        //add items to previous list
           for(i = 0; i < to.length; i++)
            {
                shopping.addElement(to[i]);
            }
         //Must remove what's deselected
           for (i = (toindex.length-1); i >= 0; i--)
            {
                items.remove(toindex[i]);
            }
    }

好的,请耐心等待(对java来说非常新)这是我将如何设置构造函数来引用 ProfileFrame 对象吗?如果是这样,我该如何更改我的 main 以反映新的构造函数?

public class GoShopping extends JPanel {

private JList shopList;

public GoShopping(ProfileFrame slist) {
//construct components


shopList = new JList(slist.getListModel());
shopList.setBounds(6, 6, 123, 166);//don't worry I'm changing the layout
add(shopList);
}


public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

//I need new GoShopping to reflect the new constructor, but not sure how to make it    work
frame.getContentPane().add (new GoShopping());
frame.pack();
frame.setVisible (true);
}
}
4

1 回答 1

0

您正在 MyPanel2 构造函数中创建一个新的 ProfileFrame 对象,并且由于这与可视化的 ProfileFrame 对象不同,因此其 JList 的模型将为空。一个不好的解决方案是使用静态变量——请不要这样做。更好的解决方案是将对真实可视化 ProfileFrame 对象的引用传递到您的 MyPanel2 构造函数中,并从该实例中调用公共方法。

再一次,不要使用空布局和setBounds(...),而是使用布局管理器,除非你喜欢让事情变得比他们需要的更困难。

于 2012-12-17T12:52:37.530 回答