正如标题所暗示的那样,从JTabbedPane中删除选项卡的最佳方法是什么?与您在Chrome中拖动标签的方式类似,它会打开一个新窗口,然后将其拖回会再次放回来?
答案 0 :(得分:2)
这不是一个简单的问题。
首先,您需要一个触发器,您可以识别该选项卡是否已取消停靠。请记住,标签可以重新排序,因此仅仅监控拖动事件是不够的
其次,如果窗口关闭(或者即使你想要),你还需要某种方法来确定如何“重新停靠”标签。
之后,它变得非常简单,只需从标签中取出组件并放在框架上即可。反之亦然。
答案 1 :(得分: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);
}
}