gpt4 book ai didi

java - 改变 nimbus JPopupmenu 行为

转载 作者:行者123 更新时间:2023-11-30 08:42:07 27 4
gpt4 key购买 nike

我需要有关 JTree 和 JPopupMenu 的灵气行为的帮助。我正在为 JTree 设置右键单击菜单。如果我在打开另一个节点的右键单击菜单后左键单击一个节点,则单击的节点将被选中。但是在 nimbus 的外观和感觉中,需要第二次单击才能选择另一个节点。我的代码在下面,您可以尝试使用默认外观并在 nimbus 部分添加注释。

public class JTreeDemo {

public static void main(String[] args) {

try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception ex) {
}
}

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
rootNode.add(new DefaultMutableTreeNode("Child1"));
rootNode.add(new DefaultMutableTreeNode("Child2"));
rootNode.add(new DefaultMutableTreeNode("Child3"));

DefaultTreeModel model = new DefaultTreeModel(rootNode);

JTree tree = new JTree(model);
tree.addMouseListener(new TreeMouseListener());

JFrame jf = new JFrame();
jf.getContentPane().add(new JScrollPane(tree));

jf.setSize(new Dimension(300, 300));

jf.setVisible(true);
}
}

class TreeMouseListener extends MouseAdapter {

@Override
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)) {
JTree tree = (JTree) e.getSource();

TreePath jClickedPath = tree.getPathForLocation(e.getX(), e.getY());
tree.setSelectionPath(jClickedPath);

JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("menu1"));
menu.show(tree, e.getX(), e.getY());
}
}
}

最佳答案

如果您打印出按下、释放和单击鼠标事件,您将看到使用默认的 L&F

// right click
tree: pressed
tree: released
tree: clicked
// click on node
tree: pressed
tree: released
tree: clicked

而有了 Nimbus L&F,您会得到

// right click
tree: pressed
tree: released
tree: clicked
// first click on node, the pressed event is not passed to the listener
tree: released
tree: clicked
// second click on node
tree: pressed
tree: released
tree: clicked

这是 Nimbus 弹出窗口在关闭时使用事件的理想行为。 (参见错误报告中的解释 #JDK-6770445 )

您可以在设置 L&F 后更改此行为。

UIManager.setLookAndFeel(info.getClassName());
UIManager.put("PopupMenu.consumeEventOnClose", false);

编辑 片段以仅更改特定JTree

的默认行为
// instruct the JTree not to close the popup
tree.putClientProperty("doNotCancelPopup",
new JComboBox().getClientProperty("doNotCancelPopup"));

// create the popup menu not inside the listener
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("menu1"));

// add the listener to the JTree
MouseListener popupListener = new PopupListener(popup);
tree.addMouseListener(popupListener);

以编程方式显示和隐藏弹出窗口

static class PopupListener extends MouseAdapter {

JPopupMenu popup;

PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}

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

@Override
public void mouseReleased(MouseEvent e) {
togglePopup(e);
}

private void togglePopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
} else if (popup.isVisible()) {
popup.setVisible(false);
}
}
}

关于java - 改变 nimbus JPopupmenu 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679216/

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