我创建了一个“日期选择器”组件,它只是一个控制器(JComponent
)和一个显示器(JPopupMenu
扩展)。当用户使用箭头控制日期时,我希望显示弹出窗口继续显示。我尝试调用按钮popup.show(...)
以mouseReleased
尝试重置弹出窗口,但之后它并没有像往常一样隐藏。我怎样才能在保持 的通常行为的同时实现这一点JPopupMenu
?
当然我可以使用JWindow
,但是我不得不担心模拟JPopupMenu
使用焦点等的显示/隐藏行为,这是我非常想避免的。
谢谢你。:)
SSCCEE
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class PopupSSCCE {
private JFrame frame = new JFrame();
private JPanel controller = new JPanel();
private JLabel controllerLabel = new JLabel("Controller");
private JLabel display = new JLabel("Display");
private JPopupMenu popup = new JPopupMenu();
private JButton prev = new JButton("<");
private JButton next = new JButton(">");
private PopupSSCCE() {
controllerLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (controllerLabel.contains(e.getPoint()))
popup.show(controllerLabel, -50, controllerLabel.getHeight());
}
});
frame.setPreferredSize(new Dimension(250, 300));
popup.setPreferredSize(new Dimension(200, 200));
controller.setPreferredSize(new Dimension(200, 50));
prev.setPreferredSize(new Dimension(50, 50));
next.setPreferredSize(new Dimension(50, 50));
controllerLabel.setPreferredSize(new Dimension(100, 50));
controller.add(prev);
controller.add(controllerLabel);
controller.add(next);
popup.add(display);
frame.getContentPane().add(controller);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new PopupSSCCE();
}
}