Bri*_*ian 8 java swing jdialog jbutton jpopupmenu
我原来问的并没有明确说明我的问题/问题,所以我会更好地解释.我有一个JButton
,设置一个JDialog
可见.JDialog将WindowListener
其设置为在windowDeactivated()
事件中不可见,该事件在用户在对话框外单击时触发.按钮ActionListener
检查对话框是否为可见,如果为true则隐藏它,如果为false则显示该对话框.
windowDeactivated()
只要用户在对话框外单击,就会始终触发是否单击按钮.我遇到的问题是当用户单击按钮关闭对话框时.该对话框由关闭WindowListener
,然后ActionListener
尝试显示它.
如果windowDeactivated()
没有setVisible(false)
,则对话框仍然打开,但在父窗口后面.我要求的是如何访问里面的点击位置windowDeactivated()
.如果我知道用户点击了按钮并且windowDeactivated()可以跳过隐藏对话框,那么按钮ActionListener
会看到它仍然可见并隐藏它.
public PropertiesButton extends JButton { private JDialog theWindow; public PropertiesButton() { theWindow = new JDialog(); theWindow.setUndecorated(true); theWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); theWindow.add(new JMenuCheckBoxItem("Something")); theWindow.addWindowListener(new WindowListener() { // just an example, need to implement other methods public void windowDeactivated(WindowEvent e) { theWindow.setVisible(false); } }); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (theWindow.isVisible()) { theWindow.setVisible(false); } else { JButton btn = (JButton)e.getSource(); theWindow.setLocation(btn.getLocationOnScreen.x,btn.getLocationOnScreen.x-50); theWindow.setVisible(true); } } }); theWindow.setVisible(false); } }
小智 1
您可以尝试使用 JPanel 而不是 JDialog 作为下拉属性列表。像这样的东西:
public class PropertiesButton extends JButton {
private JPanel theWindow;
public PropertiesButton() {
theWindow = new JPanel();
theWindow.add(new JMenuCheckBoxItem("Something"));
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (theWindow.isVisible()) {
theWindow.setVisible(false);
getParent().remove(theWindow);
} else {
JButton btn = (JButton)e.getSource();
getParent().add(theWindow);
theWindow.setBounds(
btn.getX(),
btn.getY() + btn.getHeight(), 100, 100);
theWindow.setVisible(true);
}
}
});
theWindow.setVisible(false);
}
}
Run Code Online (Sandbox Code Playgroud)
在 Swing 中使用轻量级组件而不是像 JDialog 这样的重量级组件总是更可取的,并且不会产生像您报告的那样的不良影响。这种方法的唯一问题是面板位置和大小可能会受到父级中活动的布局管理器的影响。
归档时间: |
|
查看次数: |
1423 次 |
最近记录: |