gpt4 book ai didi

java - 添加鼠标监听器后无法单击选项卡

转载 作者:搜寻专家 更新时间:2023-11-01 02:15:03 25 4
gpt4 key购买 nike

我一直在尝试将弹出菜单添加到 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());
}
}

});

但是,如果您右键单击,您将获得上下文菜单,但如果您左键单击,则没有任何反应。有谁知道我如何才能在选项卡上恢复原始功能,以便它在左键单击时变为 Activity 状态,但在右键单击时也有上下文菜单。

一个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 但不能右键单击。

最佳答案

更新:巧合的是,这个答案使用与@Neifen 的 answer 相同的方法。 ,但该示例可能值得保留。

设置选项卡式 Pane 模型的选定索引似乎有效:getModel().setSelectedIndex(3)。我已经更新了您的示例以使用事件分派(dispatch)线程并成为 sscce通过使其独立

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

/** @see https://stackoverflow.com/questions/7818752 */
public class JTabbedPaneDemo extends JPanel {

private JPopupMenu menu = new JPopupMenu();

public JTabbedPaneDemo() {
Icon icon = UIManager.getIcon("html.pendingImage");
final JTabbedPane jtb = new JTabbedPane();

JPanel jplInnerPanel1 = createInnerPanel("Tab 1: Tooltip and Icon");
jtb.addTab("One", icon, jplInnerPanel1, "Tab 1");
jtb.setSelectedIndex(0);

JPanel jplInnerPanel2 = createInnerPanel("Tab 2: Icon only");
jtb.addTab("Two", icon, jplInnerPanel2);

JPanel jplInnerPanel3 = createInnerPanel("Tab 3: Tooltip and Icon");
jtb.addTab("Three", icon, jplInnerPanel3, "Tab 3");

JPanel jplInnerPanel4 = createInnerPanel("Tab 4: Text only");
jtb.addTab("Four", jplInnerPanel4);

menu.add(new JMenuItem("Item 1"));
menu.add(new JMenuItem("Item 2"));

JLabel tab4Label = new JLabel();
tab4Label.setText("Four");
jtb.setTabComponentAt(3, tab4Label);
tab4Label.addMouseListener(new MouseAdapter() {

@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);

}

@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}

private void maybeShowPopup(MouseEvent e) {
jtb.getModel().setSelectedIndex(3);
if (e.isPopupTrigger()) {
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
});

setLayout(new GridLayout());
add(jtb);
}

private 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) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
JFrame frame = new JFrame("TabbedPane Source Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTabbedPaneDemo());
frame.pack();
frame.setSize(400, 125);
frame.setVisible(true);
}
});
}
}

关于java - 添加鼠标监听器后无法单击选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7818752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com