日期:2014-05-20  浏览次数:20660 次

【新手提问】请问怎么给托盘图标右击就出来个JPopupMenu那样的东西呢?
因为昨天上不了网,翻了好久的API还是翻不出来。。

JPopupMenu这东西要怎么用?我加到一个JFrame里面,右键都点烂,就是不出来。。郁闷。。

有没有简单点的例子给晚辈看看。。

谢谢高手帮忙。。

------解决方案--------------------
Java code

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;

public class JPopupMenuTest {

    private JFrame frame;

    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
        try {
            JPopupMenuTest window = new JPopupMenuTest();
            window.frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the application
     */
    public JPopupMenuTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 290, 234);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPopupMenu popupMenu = new JPopupMenu();
        addPopup(frame.getContentPane(), popupMenu);

        final JMenuItem newItemMenuItem = new JMenuItem();
        newItemMenuItem.setText("New Item");
        popupMenu.add(newItemMenuItem);

        popupMenu.addSeparator();

        final JCheckBoxMenuItem newCheckboxCheckBoxMenuItem = new JCheckBoxMenuItem();
        newCheckboxCheckBoxMenuItem.setText("New CheckBox");
        popupMenu.add(newCheckboxCheckBoxMenuItem);

        final JMenu menu = new JMenu();
        menu.setText("New SubMenu");
        popupMenu.add(menu);

        final JMenuItem newItemMenuItem_1 = new JMenuItem();
        newItemMenuItem_1.setText("New Item");
        menu.add(newItemMenuItem_1);

        final JCheckBoxMenuItem newCheckboxCheckBoxMenuItem_1 = new JCheckBoxMenuItem();
        newCheckboxCheckBoxMenuItem_1.setText("New CheckBox");
        menu.add(newCheckboxCheckBoxMenuItem_1);

        final JRadioButtonMenuItem newRadiobuttonRadioButtonMenuItem_1 = new JRadioButtonMenuItem();
        newRadiobuttonRadioButtonMenuItem_1.setText("New RadioButton");
        menu.add(newRadiobuttonRadioButtonMenuItem_1);

        final JRadioButtonMenuItem newRadiobuttonRadioButtonMenuItem = new JRadioButtonMenuItem();
        newRadiobuttonRadioButtonMenuItem.setText("New RadioButton");
        popupMenu.add(newRadiobuttonRadioButtonMenuItem);
    }

    private static void addPopup(Component component, final JPopupMenu popup) {
        component.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger())
                    showMenu(e);
            }
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger())
                    showMenu(e);
            }
            private void showMenu(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }
}

------解决方案--------------------
托盘?我用jni实现过,纯java应该不行吧。
最近研究swt,不知道有没有托盘功能。
------解决方案--------------------
探讨
托盘?我用jni实现过,纯java应该不行吧。
最近研究swt,不知道有没有托盘功能。