如何重新排列通过拖动在JPanel中放置JPanels

时间:2013-06-14 08:16:18

标签: java swing resize jpanel mouse-listeners

我有一个非常简单的例子。在尝试将其应用于我计划的更复杂的程序之前,我正努力工作。但我正试图拖放那些流动或垂直布局的JPanel。

我不确定从哪里开始,因为这不是我之前处理的任何事情。但是从我研究过的内容来看,我肯定需要在每个JPanel上都有一个监听器来监听点击。从这里它将从mousePressed收集初始数据,并运行一个被覆盖的repaint(),它将更新容器中面板的坐标。然后根据坐标的位置,它将重新打包()并希望调整面板的大小。

我是否走上正轨?

import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;


public class DraggablePanels extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Border raisedbevel = BorderFactory.createRaisedBevelBorder();
    int px, py;
    JPanel main = new JPanel();

    public DraggablePanels(){
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);

        JPanel p1 = new JPanel();
        p1.addMouseListener(new MouseListener(){
            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                px = e.getX();
                py = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        p1.setBorder(raisedbevel);
        JPanel p2 = new JPanel();

        p2.addMouseListener(new MouseListener(){
            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                px = e.getX();
                py = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        p2.setBorder(raisedbevel);
        p1.add(new JLabel("Thing 1"));
        p2.add(new JLabel("Thing 2"));

        main.add(p1);
        main.add(p2);

        add(main);
        pack();
        setVisible(true);
    }

    public static void main (String args[]){
        DraggablePanels d = new DraggablePanels();
    }

}

1 个答案:

答案 0 :(得分:0)

选中此示例应用程序以重新排列jpanel:

http://www.bryanesmith.com/docs/drag-and-drop-java-5/