【问题标题】:Opening contents of a JList in new window在新窗口中打开 JList 的内容
【发布时间】:2012-12-17 02:27:26
【问题描述】:

我是 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);
}
}

【问题讨论】:

  • 如果您想在新窗口中显示相同的列表,只需从原始 JList 中获取 ListModel 并将其填充到新窗口中即可。像Math.PI一样简单。
  • 好的,我尝试按照您的建议进行操作,首先将 ListModel 的 getter 添加到 ProfileFrame 类: public ListModel getListModel(){ return shopping;并试图在第二个类 MyPanel2 中检索它,我得到一个“dataModel 必须为非空”错误。我仍然不确定我做错了什么:/ public MyPanel2() { //construct components ProfileFrame pframe = new ProfileFrame(); shopList = new JList(pframe.getListModel()); shopList.setBounds(6, 6, 123, 166);添加(商店清单); }
  • 您使用的是空模型——不要那样做。如果您需要向我们展示代码,请不要在评论中这样做,因为这显然是不可读的。编辑您的原始帖子并将最新代码添加到底部。另外,不要设置边界或使用空布局。使用布局管理器,除非你喜欢让事情变得比他们需要的更困难。见答案。

标签: java swing jlist


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    相关资源
    最近更新 更多