0

正如标题所暗示的那样,从 JTabbedPane 中记录/撤消选项卡的最佳方法是什么?类似于您如何在 Chrome 中拖动选项卡并打开一个新窗口,然后将其拖回将其重新放回原处?

4

2 回答 2

2

这不是一个简单的问题。

首先,您需要一个触发器,您可以确定该选项卡将被取消停靠。请记住,选项卡可以重新排序,因此仅监视拖动事件是不够的

其次,如果窗口关闭(或者即使您想要),您需要通过某种方式确定如何“重新停靠”选项卡。

之后,它变得像从标签中移除组件并将反之亦然放置到框架上一样简单。

于 2012-11-28T00:54:08.457 回答
0

我使用MouseMotionListener 和MouseListener 做到了这一点。当您将选项卡标题拖动很长一段距离时,这只会取消停靠,并在您关闭取消停靠的框架时重新停靠它。

    import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTree;


public class DockableTabbedPane extends JTabbedPane {

    private DockableTabbedPane  DraggableTabbedPane;
    public DockableTabbedPane() {
        super();
        DraggableTabbedPane = this;
        TabDragListener tabDragger = new TabDragListener();
        this.addMouseListener(tabDragger);
        this.addMouseMotionListener(tabDragger);        
    }

    private class TabDragListener implements MouseListener, MouseMotionListener {

        Point p0,p0screen;

        Component current;
        String title;

        public void mousePressed(MouseEvent e) {
            p0 = e.getPoint();


            for (int i = 0; i < getTabCount(); i++) {
                Rectangle bounds = getBoundsAt(i);
                if (bounds.contains(p0)) {
                    current = DockableTabbedPane.this.getComponentAt(i);   
                    title =DockableTabbedPane.this.getTitleAt(i);
                    p0screen = getLocationOnScreen();
                }
            }
        };

        public void mouseDragged(MouseEvent e) {
            Point p = e.getPoint();
            JFrame frame;

            if (current != null) {
                // check for a significant  drag
                if( p.distance(p0)>20){

                    frame = undock(current,title);
                    current = null; 


                }
            }
        }



        public void mouseMoved(MouseEvent arg0) {}

        public void mouseClicked(MouseEvent arg0) {}

        public void mouseEntered(MouseEvent arg0) {}

        public void mouseExited(MouseEvent arg0) {}

        public void mouseReleased(MouseEvent arg0) {
            current=null;
            title =null;
        }
    }

    private UndockedFrame undock(Component current,String title) {


        Point p = current.getLocationOnScreen();
        remove(current);
        UndockedFrame frame = new UndockedFrame(current,title);

        p.translate(20, 20);
        frame.setLocation(p);
        frame.setVisible(true);
        fireStateChanged();
        return frame;    



    }

    private class UndockedFrame extends JFrame {
        Component current;
        String title;
        public UndockedFrame(Component current,String title) {
            this.current = current;

            this.setTitle(title);

            Container content = this.getContentPane();
            content.setLayout(new BorderLayout());
            content.add(current, BorderLayout.CENTER);
            this.setBounds(current.getBounds());
            this.addWindowListener(new UndockedFrameListener());
        }

        public void redock() {

            this.dispose();
            add(title, current);

        }
    }

    // Redock on close
    private class UndockedFrameListener extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            Window w = e.getWindow();
            if (w instanceof UndockedFrame) {
                UndockedFrame frame = (UndockedFrame)w;
                frame.redock();
            }
        }
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        DockableTabbedPane pane = new DockableTabbedPane();
        pane.add(new JTree(), "Tree 0");
        pane.add(new JTextArea(" Hello"), "Tree 1");
        pane.add(new JFileChooser(), "Tree 2");
        pane.add(new JSpinner(), "Tree 3");
        pane.add(new JSlider(),"Tree 4");
        frame.getContentPane().add(pane);
        frame.setBounds(100, 100, 400, 400);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }
}
于 2015-03-26T22:18:39.453 回答