繁体   English   中英

动态上/下移动项目

[英]Move items dynamically up/down

我有一个窗格,并将项目添加到窗格中。 目前,我已经在程序中实现了删除功能,但是我也想通过向上或向下移动元素来移动元素。 但是,我正在为算法而苦苦挣扎。 这是一个工作示例:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class minimumExample extends JFrame {

    private JButton addItem;

    private JComboBox itemBox;

    private String[] itemSelect = { "test1", "test2" };

    private JPanel addUpperPane;

    private JPanel addLowerPane;

    private List<CheckItem> displayedItems = new ArrayList<CheckItem>();

    private JButton upButton;

    private JButton downButton;

    private JButton deleteButton;

    public void createControlPane() {

        addUpperPane = new JPanel();
        addLowerPane = new JPanel(new GridLayout(0, 1));

        addItem = new JButton("Add item");
        upButton = new JButton("Up");
        downButton = new JButton("Down");
        deleteButton = new JButton("Delete");

        itemBox = new JComboBox(itemSelect);

        addItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if(itemBox.getSelectedItem().toString().equals("test1")) {
                    ButtonComp butt = new ButtonComp();
                    butt.init();
                    displayedItems.add(butt);
                    validate();
                    repaint();
                }

                if(itemBox.getSelectedItem().toString().equals("test2")) {
                    LabelComp label = new LabelComp();
                    label.label();
                    displayedItems.add(label);
                    validate();
                    repaint();
                }


                for (int i = 0; i < displayedItems.size(); i++) {
                    addLowerPane.add(displayedItems.get(i).getComponent());
                    validate();
                    repaint();
                }
            }
        });


        deleteButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                System.out.println("delete Item");

                Iterator<CheckItem> it = displayedItems.iterator();
                while (it.hasNext()) {
                    CheckItem next = it.next();
                    if (next.isSelected()) {
                        addLowerPane.remove(next.getComponent());
                        it.remove();
                        continue;
                    }
                }

                validate();
                repaint();

            }
        });

        upButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                System.out.println("move item up");

                //how to move items up

                validate();
                repaint();

            }
        });

        addUpperPane.add(itemBox, BorderLayout.EAST);
        addUpperPane.add(addItem, BorderLayout.WEST);
        addUpperPane.add(new JLabel(" | "), BorderLayout.WEST);
        addUpperPane.add(upButton, BorderLayout.WEST);
        addUpperPane.add(downButton, BorderLayout.WEST);
        addUpperPane.add(deleteButton, BorderLayout.WEST);
        addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));

        //put everything together

        add(addUpperPane, BorderLayout.NORTH);
        add(addLowerPane, BorderLayout.SOUTH);

        repaint();

    }

    private void makeLayout() {

        setTitle("Test App");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));

        createControlPane();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }


    /**
     * starts the GUI
     */
    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) throws IOException {
        minimumExample ex = new minimumExample();
        ex.start();
    }

    public abstract class CheckItem {

        protected JCheckBox check;

        public boolean isSelected() {
            return check.isSelected();
        }

        public abstract Component getComponent();

    }

    public class ButtonComp extends CheckItem {
        JPanel panel = new JPanel();

        public void init() {
            JButton button = new JButton("Test1");
            check = new JCheckBox();
            panel.add(button);
            panel.add(check);
        }

        @Override
        public Component getComponent() {
            return panel;
        }
    }

    public class LabelComp extends CheckItem {

        JPanel panel = new JPanel();

        public void label() {
            JLabel label = new JLabel("Test2");
            check = new JCheckBox();
            panel.add(label);
            panel.add(check);
        }

        @Override
        public Component getComponent() {
            return panel;
        }
    }
}

我研究了一种可能的解决方案,可能是使用Collections.swap 关于如何实现这一目标的任何建议,我真的很困惑……

感谢您的回答! 任何建议如何

您需要做的第一件事是获得对要向上或向下移动的组件的引用的方法。 您可能需要保留对当前选定组件的引用,该引用在每次更改每次上移/下移可抓住的选择时都会更新。

在那之后,我觉得你快到了。

浏览您的displayItems,然后在列表中找到该组件的位置。 如果您要向上移动它(并且上面有组件),请使用Collections.swap来切换它在组件之前的位置。

然后,您将从addLowerPane中删除所有组件,然后再次运行此部分代码。 (由于重用了它,所以您可能想创建它自己的方法)

for (int i = 0; i < displayedItems.size(); i++) {
    addLowerPane.add(displayedItems.get(i).getComponent());
    validate();
    repaint();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM