你知道如何以一种好的方式同时显示多个弹出式菜单吗?(对于JPopupMenu)我尝试@Override show(Component invoker, int x, int y),并设法通过移除setInvoker(invoker);同时显示多个弹出式菜单。这样做的问题是我无法以任何方式删除弹出窗口。
问:你知道如何让JPopupMenu在显示更多JPopupMenu时仍然可见,但在其他情况下照常工作(关闭/隐藏其他操作)?
public class MultiPopupMenu {
public static void main(String[] args){
// Create popup
JPopupMenu menu1 = createPopupMenu("First label");
JPopupMenu menu2 = createPopupMenu("Second label");
// Create labels
JLabel label1 = new JLabel("abcde");
JLabel label2 = new JLabel("1234");
JPanel panel = new JPanel();
panel.add(label1);
panel.add(label2);
// Add labels
JFrame frame = new JFrame();
frame.add(panel);
frame.setPreferredSize(new Dimension(200,100));
frame.pack();
frame.setVisible(true);
// Show popups
menu1.show(label1,-40,20); // Not showing
menu2.show(label2, 0,20);
}
private static JPopupMenu createPopupMenu(String label){
JPopupMenu popup = new JPopupMenu();
JLabel lblTest = new JLabel(label);
popup.add(lblTest);
popup.setBackground(Color.YELLOW);
return popup;
}
}发布于 2013-06-05 18:25:23
当前Swing中的
1. setVisible for escape key (add KeyBindings) and focusLost / (better) WindowFocusListener
2. add there JPanel with JButtons (firing setVisible as 1st code line, rest is wrapped into invokeLater, delayed by invokeLater)
3. then you can to put JComboBox as JMenuItem (not possible see description in my 1st. sentence)
发布于 2013-06-05 19:33:37
public class MultiPopupMenu {
public static void main(String[] args){
// Create popup
JWindow popup1 = createPopup("First label");
JWindow popup2 = createPopup("Second label");
// Create labels
JLabel label1 = new JLabel("abcde");
JLabel label2 = new JLabel("1234");
JPanel panel = new JPanel();
panel.add(label1);
panel.add(label2);
// Add labels
JFrame frame = new JFrame();
frame.add(panel);
frame.setPreferredSize(new Dimension(200,100));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Show popups
popup1.pack();
popup2.pack();
Point floc = frame.getLocation();
Point loc = label1.getLocation();
System.out.println(floc);
popup1.setLocation((int)(floc.getX()+loc.getX())-20, (int)(floc.getY()+loc.getY())+40);
loc = label2.getLocation();
popup2.setLocation((int)(floc.getX()+loc.getX())+20, (int)(floc.getY()+loc.getY())+40);
popup1.setBackground(Color.YELLOW);
popup1.setVisible(true);
popup2.setVisible(true);
}
private static JWindow createPopup(String label){
JWindow popup = new JWindow();
JLabel lblTest = new JLabel(label);
popup.add(lblTest);
popup.getContentPane().setBackground(Color.YELLOW);
return popup;
}
}https://stackoverflow.com/questions/16937283
复制相似问题