1

当我在 a 中添加 aJtextFieldJPopupMenu,显示弹出窗口时我无法编辑文本。有谁知道为什么?这是一个代码示例:

 public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPopupMenu popup = new JPopupMenu();
        JTextField field = new JTextField("My text");
        popup.insert(field, 0);
        popup.setVisible(true);
    }
4

2 回答 2

2

对我来说似乎工作正常:

查看此示例(右键单击内容窗格上的任意位置以使弹出窗口可见:

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class Main {

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPopupMenu popup = new JPopupMenu();
        final JTextField field = new JTextField(20);
        field.setText("My text");
        popup.insert(field, 0);
        popup.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        field.requestFocusInWindow();
                        field.selectAll();
                    }
                });
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {

            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {

            }
        });
        ((JComponent) frame.getContentPane()).setComponentPopupMenu(popup);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().initUI();
            }
        });
    }
}
于 2013-04-29T10:58:07.433 回答
1

避免任何猜测

显示弹出窗口时,我无法编辑文本。有谁知道为什么?

JPopup 嵌套的 JPopupMenu 必须有一个父级,我的代码示例(为什么会有硬编码 frame.setLocation(150, 100);)

  1. 在这种形式下工作正常,JPopup 接受 JFrames 坐标

  2. 在 Swing Action 中更改此代码

//popupMenu.setVisible(true);
popupMenu.show(frame, (frame.getHeight() / 4), (frame.getWidth() / 4));

popupMenu.setVisible(true);
//popupMenu.show(frame, (frame.getHeight() / 4), (frame.getWidth() / 4));

然后 PopupMenuListener 触发和事件,但 JMenuItems 也没有重新绘制

从代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PopupSample {

    private JPopupMenu popupMenu = new JPopupMenu();
    private javax.swing.Timer timer = null;
    private JFrame frame = new JFrame("Popup Example");

    public PopupSample() {
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("Selected: "
                        + actionEvent.getActionCommand());
            }
        };
        PopupMenuListener popupMenuListener = new PopupMenuListener() {
            @Override
            public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
                System.out.println("Canceled");
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
                System.out.println("Becoming Invisible");
            }

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
                System.out.println("Becoming Visible");
            }
        };
        popupMenu.addPopupMenuListener(popupMenuListener);
        JSeparator jSeparator = new JSeparator(JSeparator.VERTICAL);
        jSeparator.setPreferredSize(new Dimension(2, 100));
        jSeparator.setBackground(Color.red);
        popupMenu.add(jSeparator);
        JMenuItem cutMenuItem = new JMenuItem("Cut");
        cutMenuItem.addActionListener(actionListener);
        popupMenu.add(cutMenuItem);
        cutMenuItem.setBorder(null);
        JMenuItem copyMenuItem = new JMenuItem("Copy");
        copyMenuItem.addActionListener(actionListener);
        popupMenu.add(copyMenuItem);
        JMenuItem pasteMenuItem = new JMenuItem("Paste");
        pasteMenuItem.addActionListener(actionListener);
        pasteMenuItem.setEnabled(false);
        popupMenu.add(pasteMenuItem);
        popupMenu.addSeparator();
        JMenuItem findMenuItem = new JMenuItem("Find");
        findMenuItem.addActionListener(actionListener);
        popupMenu.add(findMenuItem);
        JTextField text = new JTextField("text");
        popupMenu.add(text);
        MouseListener mouseListener = new JPopupMenuShower(popupMenu);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addMouseListener(mouseListener);
        frame.setLocation(150, 100);
        frame.setSize(350, 250);
        frame.setVisible(true);
        start();
    }

    private void start() {
        timer = new javax.swing.Timer(1000, updateCol());
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        //popupMenu.setVisible(true);
                        popupMenu.show(frame, (frame.getHeight() / 4), (frame.getWidth() / 4));
                    }
                });
            }
        };
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                PopupSample popupSample = new PopupSample();
            }
        });
    }

    static class JPopupMenuShower extends MouseAdapter {

        private JPopupMenu popup;

        public JPopupMenuShower(JPopupMenu popup) {
            this.popup = popup;
        }

        private void showIfPopupTrigger(MouseEvent mouseEvent) {
            if (popup.isPopupTrigger(mouseEvent)) {
                popup.show(mouseEvent.getComponent(), mouseEvent.getX(),
                        mouseEvent.getY());
            }
        }

        @Override
        public void mousePressed(MouseEvent mouseEvent) {
            showIfPopupTrigger(mouseEvent);
        }

        @Override
        public void mouseReleased(MouseEvent mouseEvent) {
            showIfPopupTrigger(mouseEvent);
        }
    }
}
于 2013-04-29T11:40:38.653 回答