Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-setSelectedIcon()和setPressedIcon()似乎未注册_Java_Swing_Icons_Contextmenu_Jpopupmenu - Fatal编程技术网

Java-setSelectedIcon()和setPressedIcon()似乎未注册

Java-setSelectedIcon()和setPressedIcon()似乎未注册,java,swing,icons,contextmenu,jpopupmenu,Java,Swing,Icons,Contextmenu,Jpopupmenu,我在JTree上有一个JCheckboxMenuItem,我想在其中使用自定义图标(Windows L&F)。我能够在JCheckboxMenuItem上成功地使用setIcon(),但由于某些原因,setSelectedIcon()和setPressedIcon()似乎不起作用 下面的代码是完整的,编译时不会出错。然而,它根本不起作用。我通过setIcon()设置图像并在那里查看它们,从而确认图像已正确加载 在做一些研究时,我注意到,所以我尝试使用它实现一个变通方法,但使用JPopupMenu

我在JTree上有一个JCheckboxMenuItem,我想在其中使用自定义图标(Windows L&F)。我能够在JCheckboxMenuItem上成功地使用setIcon(),但由于某些原因,setSelectedIcon()和setPressedIcon()似乎不起作用

下面的代码是完整的,编译时不会出错。然而,它根本不起作用。我通过setIcon()设置图像并在那里查看它们,从而确认图像已正确加载

在做一些研究时,我注意到,所以我尝试使用它实现一个变通方法,但使用JPopupMenu似乎不起作用。我正试图找出一个可能的解决办法与JPOppMenu,但我有麻烦。我正在考虑添加一个鼠标侦听器,但我希望它也能响应键盘事件。。。不幸的是,Menulistener在JPOpupMenus上不起作用

以下是可编译、无错误的代码:

package mypackage;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class test extends JFrame {

    public static final ImageIcon icon_properties   = new ImageIcon(mypackage.class.getResource("options.png"));
    public static final ImageIcon icon_checked      = new ImageIcon(mypackage.class.getResource("checkbox_checked.png"));
    public static final ImageIcon icon_unchecked    = new ImageIcon(mypackage.class.getResource("checkbox_unchecked.png"));
    public static final ImageIcon icon_pressed      = new ImageIcon(mypackage.class.getResource("checkbox_pressed.png"));

    String SINGLE_LEFT      = "single left click";
    String SINGLE_MIDDLE    = "single middle click";
    String SINGLE_RIGHT     = "single right click";
    String DOUBLE_LEFT      = "double left click";
    String DOUBLE_MIDDLE    = "double middle click";
    String DOUBLE_RIGHT     = "double right click";

    String MENU_VISIBLE     = "Visible";
    String MENU_SELECTABLE  = "Selectable";
    String MENU_DRAGGABLE   = "Draggable";
    String MENU_PROPERTIES  = "Properties";

    protected JTree  theTree;
    protected DefaultMutableTreeNode theModel;

    ContextMenu menu = new ContextMenu();

    public test() {
        setSize(400, 300);
        theModel = new DefaultMutableTreeNode("LayerList") {
            {
                String NODE_NAME = " this is an item to select ";
                DefaultMutableTreeNode node_icon = new DefaultMutableTreeNode(new DefaultMutableTreeNode(NODE_NAME));
                add(node_icon);
                node_icon = new DefaultMutableTreeNode(new DefaultMutableTreeNode(NODE_NAME));
                add(node_icon);
                node_icon = new DefaultMutableTreeNode(new DefaultMutableTreeNode(NODE_NAME));
                add(node_icon);
                node_icon = new DefaultMutableTreeNode(new DefaultMutableTreeNode(NODE_NAME));
                add(node_icon);
                node_icon = new DefaultMutableTreeNode(new DefaultMutableTreeNode(NODE_NAME));
                add(node_icon);
                node_icon = new DefaultMutableTreeNode(new DefaultMutableTreeNode(NODE_NAME));
                add(node_icon);
                node_icon = new DefaultMutableTreeNode(new DefaultMutableTreeNode(NODE_NAME));
                add(node_icon);
            }
        };

        theTree = new JTree(theModel);
        theTree.putClientProperty("JTree.lineStyle", "Angled");
        theTree.setRootVisible(false);
        theTree.setShowsRootHandles(true);
        theTree.setEditable(false);

        MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                int selRow = theTree.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = theTree.getPathForLocation(e.getX(), e.getY());
                if(selRow != -1) {
                    if( SwingUtilities.isLeftMouseButton(e) ) {
                        if(e.getClickCount() == 1) {
                            ContextMenuClickHander(SINGLE_LEFT, selRow, selPath, e);
                        }
                        else if(e.getClickCount() >= 2) {
                            ContextMenuClickHander(DOUBLE_LEFT, selRow, selPath, e);
                        }
                    }
                    else if( SwingUtilities.isMiddleMouseButton(e) ) {
                        if(e.getClickCount() == 1) {
                            ContextMenuClickHander(SINGLE_MIDDLE, selRow, selPath, e);
                        }
                        else if(e.getClickCount() >= 2) {
                            ContextMenuClickHander(DOUBLE_MIDDLE, selRow, selPath, e);
                        }
                    }
                    else if( SwingUtilities.isRightMouseButton(e) ) {
                        if(e.getClickCount() == 1) {
                            ContextMenuClickHander(SINGLE_RIGHT, selRow, selPath, e);
                        }
                        else if(e.getClickCount() >= 2) {
                            ContextMenuClickHander(DOUBLE_RIGHT, selRow, selPath, e);
                        }
                    }
                }
            }
        };
        theTree.addMouseListener(ml);
        JScrollPane s = new JScrollPane();
        s.getViewport().add(theTree);
        getContentPane().add(s, BorderLayout.CENTER);
        addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
        setVisible(true);
    }

    class ContextMenu extends JPopupMenu implements ActionListener {
        JCheckBoxMenuItem item;
        public ContextMenu(){
            item = new JCheckBoxMenuItem(MENU_VISIBLE);
            item.setIcon(icon_unchecked);
            item.setSelectedIcon(icon_checked);
            item.setPressedIcon(icon_pressed);
            //item.setRolloverIcon(icon_checked);
            //item.setRolloverSelectedIcon(icon_checked);
            //item.setDisabledIcon(icon_checked);
            //item.setDisabledSelectedIcon(icon_checked);
            item.setActionCommand(MENU_VISIBLE);
            item.addActionListener(this);
            add(item);

            item = new JCheckBoxMenuItem(MENU_SELECTABLE);
            item.setIcon(icon_unchecked);
            item.setSelectedIcon(icon_checked);
            item.setPressedIcon(icon_pressed);
            item.setActionCommand(MENU_SELECTABLE);
            item.addActionListener(this);
            add(item);

            item = new JCheckBoxMenuItem(MENU_DRAGGABLE);
            item.setIcon(icon_unchecked);
            item.setSelectedIcon(icon_checked);
            item.setPressedIcon(icon_pressed);
            item.setActionCommand(MENU_DRAGGABLE);
            item.addActionListener(this);
            add(item);

            JMenuItem item = new JMenuItem(MENU_PROPERTIES+"...");
            item.setIcon(icon_properties);
            item.setActionCommand(MENU_PROPERTIES);
            item.addActionListener(this);
            add(item);
        }

        public void actionPerformed(ActionEvent e) {
            if (MENU_VISIBLE.equals(e.getActionCommand())) {
                System.out.println("Clicked "+MENU_VISIBLE);
                ChangeIcon(e);
            }
            else if (MENU_SELECTABLE.equals(e.getActionCommand())) {
                System.out.println("Clicked "+MENU_SELECTABLE);
                ChangeIcon(e);
            }
            else if (MENU_DRAGGABLE.equals(e.getActionCommand())) {
                System.out.println("Clicked "+MENU_DRAGGABLE);
                ChangeIcon(e);
            }
            else if (MENU_PROPERTIES.equals(e.getActionCommand())) {
                System.out.println("Clicked "+MENU_PROPERTIES);
            }
        }
    }

    public void ChangeIcon(ActionEvent e) {
        if (e.getSource() instanceof AbstractButton) {
            boolean selected = ((AbstractButton) e.getSource()).isSelected();
            if( selected ) {
                ((AbstractButton) e.getSource()).setIcon(icon_checked);
            }
            else {
                ((AbstractButton) e.getSource()).setIcon(icon_unchecked);
            }
        }
    }

    void ContextMenuClickHander(String clickType, int row, TreePath tpath, MouseEvent e) {
        theTree.setSelectionPath(tpath);
        System.out.println(clickType+": row="+row+", treepath="+tpath);
        if( clickType.equals(SINGLE_RIGHT) ) {
            menu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    public static void main(String argv[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception evt) {}
        new test().setLocationRelativeTo(null);
    }
}
以下是我尝试使用的图像,如果有帮助的话:
-选中:
-未选中:
-按:

更新: 到目前为止,我开发的一个不完全完美的解决方法是在每次检测到单击时手动设置图标。在每个println语句后的“actionPerformed(ActionEvent e)”函数中,添加一行带有“ChangeIcon(e);”的新行,完成后,只需添加此函数:

然而,Windows L&F似乎仍然添加了自己的悬停/选定边框(见图)。我希望有人仍然有真正的解决方案…

更新2: 用第一个更新代码更新了完整的代码,稍微收紧了帖子。

希望这能在我的第三段(“在做一些研究……”)中对你有所帮助。我指出,我已经看过这个问题。不幸的是,您不能向JPopupMenu中添加menuListener,但我使用的解决方法(如上面的代码所示)在功能上做了相同的事情(只要用户单击菜单)。不过,还没有修复选中菜单项时显示的蓝色边框。