定位 JDialog 以显示在 Swing 中的 JTextField 下方?

har*_*tps 5 java swing


我想将 JDialog 框放置在 JFrame 的 JTextField 下方,当对话框打开时,我的 JFrame 不应该能够移动——也就是说,它不应该是可拖动的。有什么建议么?

dac*_*cwe 2

public class DialogTest {
    public static void main(String[] args) {

        final JFrame frame = new JFrame("Frame");
        JTextField field = new JTextField("Click me to open dialog!");
        field.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                JTextField f = (JTextField) e.getSource();
                Point l = f.getLocationOnScreen();

                JDialog d = new JDialog(frame, "Dialog", true);
                d.setLocation(l.x, l.y + f.getHeight());
                d.setSize(200, 200);
                d.setVisible(true);
            }
        });
        frame.add(field);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 100);
        frame.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)