Java 如何在JTabbedPane中拖动属于自定义组件的选项卡?
我有一个简单的JPanel,作为JTabbedPane的选项卡组件。它是MainTabComponent类:Java 如何在JTabbedPane中拖动属于自定义组件的选项卡?,java,swing,jtabbedpane,Java,Swing,Jtabbedpane,我有一个简单的JPanel,作为JTabbedPane的选项卡组件。它是MainTabComponent类: public class MainTabComponent extends JPanel { private JLabel label; public MainTabComponent(String title, MainTabbedPane mainTabbedPane) { // unset default FlowLayout gaps
public class MainTabComponent extends JPanel {
private JLabel label;
public MainTabComponent(String title, MainTabbedPane mainTabbedPane) {
// unset default FlowLayout gaps
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
setOpaque(false);
label = new JLabel(title, Images.tabIconSaved, JLabel.LEADING);
add(label);
// add more space between the label and the button
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
// tab button
JButton button = new TabButton();
add(button);
// add more space to the top of the component
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
}
接下来,我需要允许用户拖动选项卡(即MainTabComponent)
问题是TabbedPaneUI的tabForCoordinate方法没有返回正确的选项卡索引。
它总是返回0
所以我需要在鼠标点击时找到标签索引,但是如何使用自定义标签来实现这一点呢
谢谢大家!
问题在于TabbedPaneUI的tabForCoordinate方法没有返回正确的选项卡索引。
此方法是抽象的,其类是抽象的。您可以使用JTabbedPane
的getSelectedIndex()
通过鼠标点击找到选项卡索引。如何在mouseEntered事件中找到选项卡索引?添加MouseDapter
并从mouseEntered
内部调用getSelectedIndex
。