日期:2014-05-20 浏览次数:20660 次
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,不知道有没有托盘功能。
------解决方案--------------------