我创建了一个日期选择器'组件,它只是一个控制器(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();
}
}