添加鼠标侦听器后无法单击选项卡
我一直在尝试向 JTabbedPane 的选项卡标题添加一个弹出菜单,我已设法执行以下操作:
tabbedPane.setTabComponentAt(a+2, x);
x.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popUpMenuTab.show(e.getComponent(),
e.getX(), e.getY());
}
}
});
但是,如果您右键单击,您将获得上下文菜单,但如果您左键单击,则不会发生任何事情。有谁知道我如何才能在选项卡上恢复原始功能,以便它在左键单击时变为活动状态,但在右键单击时也有上下文菜单。
SSCCE 如下所示
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
public class JTabbedPaneDemoextends JPanel {
private JPopupMenu menu = new JPopupMenu();
public JTabbedPaneDemo() {
ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
JTabbedPane jtbExample = new JTabbedPane();
JPanel jplInnerPanel1 = createInnerPanel("Tab 1 Contains Tooltip and Icon");
jtbExample.addTab("One", icon, jplInnerPanel1, "Tab 1");
jtbExample.setSelectedIndex(0);
JPanel jplInnerPanel2 = createInnerPanel("Tab 2 Contains Icon only");
jtbExample.addTab("Two", icon, jplInnerPanel2);
JPanel jplInnerPanel3 = createInnerPanel("Tab 3 Contains Tooltip and Icon");
jtbExample.addTab("Three", icon, jplInnerPanel3, "Tab 3");
JPanel jplInnerPanel4 = createInnerPanel("Tab 4 Contains Text only");
jtbExample.addTab("Four", jplInnerPanel4);
menu.add(new JMenuItem("Item 1"));
menu.add(new JMenuItem("Item 2"));
JLabel tab4Label = new JLabel();
tab4Label.setText("Four");
jtbExample.setTabComponentAt(3, tab4Label);
tab4Label.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.show(e.getComponent(),
e.getX(), e.getY());
}
}
});
//Add the tabbed pane to this panel.
setLayout(new GridLayout(1, 1));
add(jtbExample);
}
protected JPanel createInnerPanel(String text) {
JPanel jplPanel = new JPanel();
JLabel jlbDisplay = new JLabel(text);
jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
jplPanel.setLayout(new GridLayout(1, 1));
jplPanel.add(jlbDisplay);
return jplPanel;
}
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPane Source Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}
});
frame.getContentPane().add(new JTabbedPaneDemo(),
BorderLayout.CENTER);
frame.setSize(400, 125);
frame.setVisible(true);
}
}
,在这里您可以左键单击选项卡 4,但不能右键单击。
I have been trying to add a pop up menu to the tab title of a JTabbedPane, which I have managed to do as follows:
tabbedPane.setTabComponentAt(a+2, x);
x.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popUpMenuTab.show(e.getComponent(),
e.getX(), e.getY());
}
}
});
However, with this if you right click you get the context menu, but if you left click nothing happnes. Does anyone know how I can get the orginal functionality back on the tab so that it changes to being active on a left click but also has the context menu on a right click.
An SSCCE is as follows
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
public class JTabbedPaneDemoextends JPanel {
private JPopupMenu menu = new JPopupMenu();
public JTabbedPaneDemo() {
ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
JTabbedPane jtbExample = new JTabbedPane();
JPanel jplInnerPanel1 = createInnerPanel("Tab 1 Contains Tooltip and Icon");
jtbExample.addTab("One", icon, jplInnerPanel1, "Tab 1");
jtbExample.setSelectedIndex(0);
JPanel jplInnerPanel2 = createInnerPanel("Tab 2 Contains Icon only");
jtbExample.addTab("Two", icon, jplInnerPanel2);
JPanel jplInnerPanel3 = createInnerPanel("Tab 3 Contains Tooltip and Icon");
jtbExample.addTab("Three", icon, jplInnerPanel3, "Tab 3");
JPanel jplInnerPanel4 = createInnerPanel("Tab 4 Contains Text only");
jtbExample.addTab("Four", jplInnerPanel4);
menu.add(new JMenuItem("Item 1"));
menu.add(new JMenuItem("Item 2"));
JLabel tab4Label = new JLabel();
tab4Label.setText("Four");
jtbExample.setTabComponentAt(3, tab4Label);
tab4Label.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.show(e.getComponent(),
e.getX(), e.getY());
}
}
});
//Add the tabbed pane to this panel.
setLayout(new GridLayout(1, 1));
add(jtbExample);
}
protected JPanel createInnerPanel(String text) {
JPanel jplPanel = new JPanel();
JLabel jlbDisplay = new JLabel(text);
jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
jplPanel.setLayout(new GridLayout(1, 1));
jplPanel.add(jlbDisplay);
return jplPanel;
}
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPane Source Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}
});
frame.getContentPane().add(new JTabbedPaneDemo(),
BorderLayout.CENTER);
frame.setSize(400, 125);
frame.setVisible(true);
}
}
Here you can left click on tab 4 but you can't right click.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:巧合的是,这个答案使用了与@Neifen相同的方法 答案,但该示例可能值得保留。
设置选项卡式窗格模型的选定索引似乎有效:
getModel().setSelectedIndex(3)
。我已经更新了您的示例,以使用事件调度线程,并通过使其成为一个 sscce self-包含。。Update: Coincidentally, this answer uses the same approach as @Neifen's answer, but the example may be worth preserving.
Setting the tabbed pane model's selected index seems to work:
getModel().setSelectedIndex(3)
. I've updated your example to use the event dispatch thread and to be an sscce by making it self-contained.这是我的解决方案:
我已经将侦听器作为一个类。当您声明侦听器时,您可以将选项卡的索引作为参数。
我还将 tabbedPane 设为全局
This is my Solution:
I have made a the listener as a class. When you declare the listener you can put the index of the tab as parameter.
I also made the tabbedPane global