防止 JPopupMenu 在点击时关闭

如何解决防止 JPopupMenu 在点击时关闭

我有一个弹出式菜单,但当我点击某些东西时它会关闭

大多数时候这没问题,但有时需要它不会在点击时关闭

这是我正在处理的一段代码,可以帮助您重现我所拥有的:

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

public class Try{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Trial");
        jpopupmenu menu = new jpopupmenu();
        JLabel label = new JLabel("This is a popup menu!");
        menu.add(label);
        frame.setSize(900,500);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeto(null);
        frame.setVisible(true);
        menu.show(frame,450,250);
    }
}

我想要的是,如果我点击框架,菜单不会关闭

解决方法

适合工作的合适工具。
考虑一个非模态、未修饰的 dialog(而不是 JPopupMenu)。

这是您需要的那种东西吗?

import java.awt.Color;

import javax.swing.*;

public class Try{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Trial");
        JLabel label = new JLabel("This is a popup menu!");
        label.setBorder(BorderFactory.createLineBorder(Color.red,2));
        frame.setSize(900,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        JDialog dlg = new JDialog(frame);
        dlg.add(label);
        dlg.setUndecorated(true);
        dlg.pack();
        dlg.setLocationRelativeTo(frame);
        dlg.setVisible(true);
    }
}

这是我电脑上的样子。

gui

如果您点击 JFrame,对话框仍然可见。

,

所以这可以通过 JPopupMenu 本身实现,不需要 JDialog。

我必须重写 setVisible 方法并允许仅在调用 close 方法后关闭弹出窗口!

这是弹出菜单只会在 5000 毫秒后关闭而不是在点击时关闭的代码!

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

public class Try{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Trial");
        MyPopupMenu menu = new MyPopupMenu();
        JLabel label = new JLabel("This is a popup menu!");
        menu.add(label);
        frame.setSize(900,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        menu.show(frame,450,250);
        Timer timer = new Timer(5000,new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 menu.closePopup();         
                 ((Timer)evt.getSource()).stop();
             }
         });
         timer.start();
    }
}

class MyPopupMenu extends JPopupMenu{
    private boolean isHideAllowed;

    public MyPopupMenu(){
        super();
        this.isHideAllowed = false;
    }

    @Override
    public void setVisible(boolean visibility){
        if(isHideAllowed && !visibility)
            super.setVisible(false);
        else if(!isHideAllowed && visibility)
            super.setVisible(true);
    }

    public void closePopup(){
        this.isHideAllowed = true;
        this.setVisible(false);
    }
}
,

您需要设置正确的defaultCloseOperation-

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

public class Try{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Trial");
        JPopupMenu menu = new JPopupMenu();
        JLabel label = new JLabel("This is a popup menu!");
        menu.add(label);
        frame.setSize(900,500);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.addWindowsListener(new WindowAdapter(){
           //Determine whether you should call frame.dispose or hide.
         });
        menu.show(frame,250);
    }
}

另见How to programmatically close a JFrame

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?