我有一个问题,当我将鼠标侦听器添加到用作选项卡的组件时,我无法切换选项卡。
这说明了问题:
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class JTabBug {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Red", new JLabel("Roses"));
jTabbedPane.addTab("Blue", new JLabel("Skies"));
jTabbedPane.addTab("Green", new JLabel("Grass"));
for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));
tabComponent.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("dragging");
}
});
jTabbedPane.setTabComponentAt(i, tabComponent);
}
JFrame jFrame = new JFrame("Testing");
jFrame.add(jTabbedPane);
jFrame.setSize(400, 500);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
拖动按预期打印,但您无法更改选项卡。