2010-10-26 48 views

回答

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

忘記的不可移動框架的第一個時間,現在修好了...... – dacwe 2010-10-26 15:57:29

+0

設置對話框模態不會使不動產... – willcodejavaforfood 2010-10-26 16:03:11

+0

它的工作...謝謝.... – harishtps 2010-10-26 16:06:01

0

使用JDialog.setLocationRelativeTo將其設置在文本字段下方。

+0

那不是下面?! – dacwe 2010-10-26 16:01:58

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座標。