在Swing中将JDialog定位到JTextField下方?

5
我想将JDialog框定位在JFrame中一个JTextField的下方,当对话框弹出时,我的JFrame应该无法移动,即不能拖动。有什么建议吗?
3个回答

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);
    }
}

抱歉,出了点小问题 :) - willcodejavaforfood

0
创建一个像这样的模态JDialog。
public class MyDialog extends JDialog
{
  private int width = 50;
  private int height = 50;

  public MyDialog(Frame parent, int x, int y)
  {
    super(parent, "MyTitle", true);
    setBounds(x, y, width, height);
  }

}

模态意味着用户在对话框关闭之前无法与父窗口进行交互。您可以确定所需位置并将x、y坐标传递给构造函数。


0
使用 JDialog.setLocationRelativeTo 将其设置在文本字段下方。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接