当前位置: 首页 > 知识库问答 >
问题:

JPopupMenu模式

云承天
2023-03-14

我不完全确定“模态”是否是我需要描述问题的正确术语,但我有一个带有JDialog的独立应用程序。JDialog 设置为阻止应用程序的所有其他部分,直到对话框关闭。我最近开始使用JPopupMenu添加一种自动建议功能。这在应用程序的其余部分工作正常,但是当尝试将其作为对话框的一部分时,我似乎无法单击弹出窗口,我假设这是因为对话框的模式排除类型。有谁知道一个黑客来解决这个问题?

用于启动JDialog的代码:

NewPlayerDialog dialog = new NewPlayerDialog();
dialog.setLocation(Main.getWindow().getLocation().x + Main.getWindow().getWidth()/2 - dialog.getWidth()/2, Main.getWindow().getLocation().y + Main.getWindow().getHeight()/2 - dialog.getHeight()/2);
dialog.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
dialog.setResizable(false);
dialog.setVisible(true);

JDialog中的代码:

package com.etan.bracketrunner2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import com.etan.dbhs.DBHSDatabaseIntermediary;
import com.etan.dbhs.DBHSPlayerInitializer;
import com.etan.widgets.UnderlinedLabel;

@SuppressWarnings("serial")
public class NewPlayerDialog extends JDialog implements ActionListener, MouseListener {

    private JTextField fnTextField;
    private JPopupMenu fnPopup;
    private ActionListener fnActionListener;
    private JTextField lnTextField;
    private JPopupMenu lnPopup;
    private ActionListener lnActionListener;
    private JTextField idTextField;
    private JTextField handicapTextField;

    private JButton okButton;
    private JButton cancelButton;

    /**
     * Constructor
     */
    public NewPlayerDialog() {

        super(Main.getWindow(), "New Player", Dialog.ModalityType.TOOLKIT_MODAL);

        Font font = new Font("Arial Black", Font.PLAIN, 14);

        JLabel fnLabel = new UnderlinedLabel("First Name");
        fnLabel.setForeground(Color.BLACK);
        fnLabel.setFont(font);
        JPanel fnLabelPanel = new JPanel();
        fnLabelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        fnLabelPanel.add(fnLabel);

        JLabel lnLabel = new UnderlinedLabel("Last Name");
        lnLabel.setForeground(Color.BLACK);
        lnLabel.setFont(font);
        JPanel lnLabelPanel = new JPanel();
        lnLabelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        lnLabelPanel.add(lnLabel);

        JLabel idLabel = new UnderlinedLabel("ID#");
        idLabel.setForeground(Color.BLACK);
        idLabel.setFont(font);
        JPanel idLabelPanel = new JPanel();
        idLabelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        idLabelPanel.add(idLabel);

        JLabel handicapLabel = new UnderlinedLabel("Handicap");
        handicapLabel.setForeground(Color.BLACK);
        handicapLabel.setFont(font);
        JPanel handicapLabelPanel = new JPanel();
        handicapLabelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        handicapLabelPanel.add(handicapLabel);

        JPanel labelPanel = new JPanel();
        labelPanel.setLayout(new GridLayout(1, 4));
        labelPanel.add(fnLabelPanel);
        labelPanel.add(lnLabelPanel);
        labelPanel.add(idLabelPanel);
        labelPanel.add(handicapLabelPanel);

        fnTextField = new JTextField(7);
        fnTextField.setForeground(Color.BLACK);
        fnTextField.setFont(font);

        fnPopup = new JPopupMenu();

        PopupMenuListener fnListener = new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {

                fnPopup.setLocation(fnTextField.getLocationOnScreen().x,
                        fnTextField.getLocationOnScreen().y + fnTextField.getHeight());
                fnPopup.setPopupSize(fnTextField.getWidth(), fnPopup.getComponentCount() * 20);

            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                ;
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                ;
            }

        };

        fnPopup.addPopupMenuListener(fnListener);

        fnTextField.setComponentPopupMenu(fnPopup);

        fnActionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                fnTextField.setText(e.getActionCommand());
                fnPopup.setVisible(false);

            }// ends actionPerformed()

        };

        FocusListener fnFocusListener = new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {

                resetFirstNamePopup();

            }

            @Override
            public void focusLost(FocusEvent e) {

                fnPopup.setVisible(false);

            }

        };

        KeyListener fnKeyListener = new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {

                ;

            }

            @Override
            public void keyPressed(KeyEvent e) {

                ;

            }

            @Override
            public void keyReleased(KeyEvent e) {

                resetFirstNamePopup();

            }

        };

        fnTextField.addActionListener(fnActionListener);
        fnTextField.addFocusListener(fnFocusListener);
        fnTextField.addKeyListener(fnKeyListener);

        lnTextField = new JTextField(7);
        lnTextField.setForeground(Color.BLACK);
        lnTextField.setFont(font);

        lnPopup = new JPopupMenu();

        PopupMenuListener lnListener = new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {

                lnPopup.setLocation(lnTextField.getLocationOnScreen().x,
                        lnTextField.getLocationOnScreen().y + lnTextField.getHeight());
                lnPopup.setPopupSize(lnTextField.getWidth(), lnPopup.getComponentCount() * 20);

            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                ;
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                ;
            }

        };

        lnPopup.addPopupMenuListener(lnListener);

        lnTextField.setComponentPopupMenu(lnPopup);

        lnActionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                lnTextField.setText(e.getActionCommand());
                lnPopup.setVisible(false);

            }// ends actionPerformed()

        };

        FocusListener lnFocusListener = new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {

                resetLastNamePopup();

            }

            @Override
            public void focusLost(FocusEvent e) {

                lnPopup.setVisible(false);

            }

        };

        KeyListener lnKeyListener = new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {

                ;

            }

            @Override
            public void keyPressed(KeyEvent e) {

                ;

            }

            @Override
            public void keyReleased(KeyEvent e) {

                resetLastNamePopup();

            }

        };

        lnTextField.addActionListener(lnActionListener);
        lnTextField.addFocusListener(lnFocusListener);
        lnTextField.addKeyListener(lnKeyListener);

        idTextField = new JTextField(3);
        idTextField.setForeground(Color.BLACK);
        idTextField.setFont(font);

        handicapTextField = new JTextField(3);
        handicapTextField.setForeground(Color.BLACK);
        handicapTextField.setFont(font);

        JPanel fnTextFieldPanel = new JPanel();
        fnTextFieldPanel.add(fnTextField);

        JPanel lnTextFieldPanel = new JPanel();
        lnTextFieldPanel.add(lnTextField);

        JPanel idTextFieldPanel = new JPanel();
        idTextFieldPanel.add(idTextField);

        JPanel handicapTextFieldPanel = new JPanel();
        handicapTextFieldPanel.add(handicapTextField);

        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(1, 4));
        infoPanel.add(fnTextFieldPanel);
        infoPanel.add(lnTextFieldPanel);
        infoPanel.add(idTextFieldPanel);
        infoPanel.add(handicapTextFieldPanel);

        okButton = new JButton("OK");
        okButton.setForeground(Color.BLACK);
        okButton.setFont(font);
        okButton.addActionListener(this);

        cancelButton = new JButton("Cancel");
        cancelButton.setForeground(Color.BLACK);
        cancelButton.setFont(font);
        cancelButton.addActionListener(this);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(BorderFactory.createMatteBorder(4, 0, 0, 0, new Color(160, 0, 0)));
        buttonPanel.setLayout(new GridLayout(1, 2));
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);

        JPanel content = new JPanel();
        content.setBorder(BorderFactory.createLineBorder(new Color(160, 0, 0), 4));
        content.setLayout(new BorderLayout());
        content.add(labelPanel, BorderLayout.NORTH);
        content.add(infoPanel, BorderLayout.CENTER);
        content.add(buttonPanel, BorderLayout.SOUTH);

        setPreferredSize(new Dimension(500, 200));
        setContentPane(content);
        pack();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }// ends NewPlayerDialog()

    @Override
    public void actionPerformed(ActionEvent evt) {

        if(evt.getSource().equals(okButton)) {

            if(checkInfo()) {
                DBHSPlayerInitializer init = new DBHSPlayerInitializer();
                int id = 0;
                float handicap = 0;
                try {
                    if(!idTextField.getText().equals("")) {
                        id = Integer.parseInt(idTextField.getText());
                    }
                }
                catch(NumberFormatException e) {
                    Main.getControlPanel().showErrorMessage(
                            "Please make sure that all inputs under ID# are integer numbers.");
                }

                try {
                    if(!handicapTextField.getText().equals("")) {
                        handicap = Float.parseFloat(handicapTextField.getText());
                    }
                }
                catch(NumberFormatException e) {
                    Main.getControlPanel().showErrorMessage(
                            "Please make sure that all inputs under handicap are number values.");
                }
                Player player;
                try {
                    player = init.initialize(fnTextField.getText(),
                            lnTextField.getText(), id, handicap, 0);
                    Main.getControlPanel().getScreen().getPlayers().addPlayer(player);
                    Main.getControlPanel().showPrizesDialog();
                } catch (Exception e) {
                    Main.getControlPanel().showErrorMessage("There was an error while trying to add the new player.");
                    e.printStackTrace();
                }
            }

            dispose();
            Main.getControlPanel().updateInfo();

        } else if(evt.getSource().equals(cancelButton)) {

            dispose();

        }

    }// ends actionPerformed()

    boolean checkInfo() {

        String fn = fnTextField.getText();
        String ln = lnTextField.getText();
        String id = idTextField.getText();
        String handicap = handicapTextField.getText();

        if(id.equals("") && (fn.equals("") && ln.equals(""))) {
            Main.getControlPanel().showErrorMessage("Please make sure that all players have either their first and last names, or their id number filled in.");

            return false;
        }

        if(fn.equals("") && ln.equals("")) {
            try{
                Integer.parseInt(id);
            }
            catch(NumberFormatException e) {
                if(!fn.equals("") && !ln.equals("")) {
                    Main.getControlPanel().showErrorMessage("Please make sure that " + fn + " " + ln + "'s ID is an integer number.");
                } else {
                    Main.getControlPanel().showErrorMessage("Please make sure that each player's ID is an integer number.");
                }

                return false;
            }
        }

        if(!handicap.equals("")) {
            try{
                Float.parseFloat(handicap);
            }
            catch(NumberFormatException e) {
                Main.getControlPanel().showErrorMessage("Please make sure that " + fn + " " + ln + "'s handicap is numeric.");

                return false;
            }
        }

        return true;

    }// ends checkInfo()

    /**
     * Used for an anonymous ActionListener in the 
     * PlayerPanel.
     * 
     * @return cancelButton
     */
    JButton getCancelButton() {

        return cancelButton;

    }// ends getCancelButton()

    /**
     * Used for an anonymous ActionListener in the 
     * PlayerPanel.
     * 
     * @return okButton
     */
    JButton getOkButton() {

        return okButton;

    }// ends getOkButton()

    @Override
    public void mouseClicked(MouseEvent evt) {

        ;

    }// ends mouseClicked()

    @Override
    public void mouseEntered(MouseEvent evt) {

        if(evt.getComponent().getClass().getName().equals("javax.swing.JMenuItem")) {

            evt.getComponent().setBackground(new Color(120,145,170));

        }

    }// ends mouseEntered()

    @Override
    public void mouseExited(MouseEvent evt) {

        if(evt.getComponent().getClass().getName().equals("javax.swing.JMenuItem")) {

            evt.getComponent().setBackground(new Color(238,238,238));

        }

    }// ends mouseExited()

    @Override
    public void mousePressed(MouseEvent evt) {

        ;

    }// ends mousePressed()

    @Override
    public void mouseReleased(MouseEvent evt) {

        ;

    }// ends mouseReleased()

    /**
     * Repopulates fnPopup if necessary.
     */
    private void resetFirstNamePopup() {

        fnPopup.setVisible(false);
        fnPopup.removeAll();

        if(!(fnTextField.getText().equals("") && lnTextField.getText().equals(""))) {

            JMenuItem item = null;
            String[] names = DBHSDatabaseIntermediary.getFirstNames(fnTextField.getText(), lnTextField.getText());
            for(int i=0; i < names.length; i++) {

                item = new JMenuItem(names[i]);
                item.addActionListener(fnActionListener);
                item.addMouseListener(NewPlayerDialog.this);
                fnPopup.add(item);

            }

            if(names.length > 0 && !names[0].equals("")) {
                fnPopup.setVisible(true);
            }
            fnPopup.grabFocus();

        }

    }// ends resetFirstNamePopup()

    /**
     * Repopulates lnPopup if necessary.
     */
    private void resetLastNamePopup() {

        lnPopup.setVisible(false);
        lnPopup.removeAll();

        if(!(fnTextField.getText().equals("") && lnTextField.getText().equals(""))) {

            JMenuItem item = null;
            String[] names = DBHSDatabaseIntermediary.getLastNames(fnTextField.getText(), lnTextField.getText());
            for(int i=0; i < names.length; i++) {

                item = new JMenuItem(names[i]);
                item.addActionListener(lnActionListener);
                item.addMouseListener(NewPlayerDialog.this);
                lnPopup.add(item);

            }

            if(names.length > 0 && !names[0].equals("")) {
                lnPopup.setVisible(true);
            }
            lnPopup.grabFocus();

        }

    }// ends resetLastNamePopup()

}// ends Class

共有1个答案

傅献
2023-03-14

我将对话框中的模态类型更改为DOCUMENT_MODAL并且一切正常。

 类似资料:
  • 介绍 (Introduction) 弹出菜单表示一个菜单,可以在组件内的指定位置动态弹出。 Class 声明 (Class Declaration) 以下是javax.swing.JPopupMenu类的声明 - public class JPopupMenu extends JComponent implements Accessible, MenuElement 类构造函数

  • 主要内容:1 Java JPopupMenu的介绍,2 Java JPopupMenu的声明,3 Java JPopupMenu的构造方法,4 Java JPopupMenu的案例1,5 Java JPopupMenu的案例21 Java JPopupMenu的介绍 PopupMenu可以在组件内的特定位置动态弹出。它继承了JComponent类。 2 Java JPopupMenu的声明 我们来看一下javax.swing.JPopupMenu类的声明。 3 Java JPopupMenu的构

  • 问题内容: 我想自定义外观,因此我创建了一个自定义类,扩展了JPopupMenu类,从而覆盖了该方法,就像我对需要自定义的任何组件所做的那样。 我唯一正确知道的问题是我无法透明。我虽然足够,但我错了。 我该如何做透明的? 问题答案: 弹出菜单的问题在于它可能被实现为顶级容器(Window),并且窗口是不透明的,无论您用setOpaque()设置了什么值(不透明)。但是窗口也可以做成半透明的。 您可

  • 我的应用程序中有一个弹出菜单,我想用一个自定义的菜单替换它,这样它就能与应用程序的其他部分相匹配。从本质上说,我不想在弹出窗口中使用普通的菜单项,而是想重用应用程序中其他地方已经存在的组件,该组件允许您以“分页”的方式而不是子菜单来导航项的层次结构。因此,如果单击列表中包含子项的项,则将显示下一页,用所单击项的子项列表替换列表中的当前项。 当运行上面的示例时,您将看到,如果在单击内部面板后关闭和打

  • 我想添加一种在中滚动菜单项的方法,就像在中滚动项列表一样。 假设我有10个菜单项。我希望一次只显示5个菜单项,我将使用底部或顶部的垂直滚动按钮来显示未列出的菜单项,并隐藏刚才看到的菜单项。 有可能吗?我使用的是JIDE软件的,点击它时会显示。我试图保持放置的命令栏的外观,因此除非真的有必要,否则我不想用替换它。

  • 我有一个扩展JPopupMenu的类MyPopupMenu。在这个弹出菜单中,我添加了一个带有ActionListener的JMenuItem,它调用一个需要几分钟才能返回的进程。我想在按下这个项目后立即关闭弹出菜单。我在MyPopupMenu类中的方法是这样的: 这不管用。在我按下“一”项后,进程开始,但弹出菜单保持打开状态,直到进程返回(几分钟)。是否可以使弹出菜单消失,但进程继续运行?