2012-08-06 167 views
0

请看看下面的代码如何将一个窗口连接到另一个窗口

Main.Java

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


public class Main extends JFrame 
{ 
    private JButton ok; 

    public Main() 
    { 
     ok = new JButton("OK"); 
     ok.addActionListener(new ButtonAction()); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new FlowLayout()); 
     panel.add(ok); 

     getContentPane().add(panel,"South"); 

     this.setVisible(true); 
     this.setSize(new Dimension(200,200)); 
     this.validate(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    public static void main(String[]args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      new Main(); 
     } 
     catch(Exception e) 
     { 

     } 
    } 

    private class ButtonAction implements ActionListener 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      Dialog d = new Dialog(); 
      d.setVisible(true); 
     } 
    } 

} 

Dialog.java

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


public class Dialog extends JDialog 
{ 
    private JButton done; 

    public Dialog() 
    { 
     done = new JButton("Done"); 

     this.add(done); 

     this.setSize(new Dimension(400,200)); 
    } 

} 

在这里,我想将对话框窗体“附加”到主窗体中。这意味着,当我点击Main.Java中的确定按钮时,对话窗体将被附加到主窗体的右侧。所以,当我移动主窗体时,对话框也会移动。但是,对话框形式应该是独立的,这意味着当我单击对话框形式的“x”按钮时,只有该形式存在,而不是主要形式。

如何在点击按钮时将此对话框窗体添加到主窗体的右侧?请帮忙!

+1

在ButtonAction的actionPerformed,你应该设置新建对话框的位置,你想主框架的角落,它连接至。 – Vulcan 2012-08-06 18:14:44

+0

我不认为这是答案。这是最简单的事情。如果用户移动窗口,对话框将不会移动 – 2012-08-06 18:21:31

+0

这是一个开始。添加一个WindowListener来跟踪位置,并添加一个ComponentListener来跟踪大小。 – Vulcan 2012-08-06 18:25:00

回答

2

答案不是MouseListener,而是ComponentListener。我设法通过使用该监听器的“componentMoved()”方法来做到这一点。

Main.java

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

public class Main extends JFrame implements ComponentListener, ActionListener 
{ 
    private JButton ok; 
    private Dialog dialog; 

    public Main() 
    { 
     ok = new JButton("OK"); 
     ok.addActionListener(this); 

     dialog = new Dialog(); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new FlowLayout()); 
     panel.add(ok); 

     getContentPane().add(panel,"South"); 

     this.addComponentListener(this); 

     this.setVisible(true); 
     this.setSize(new Dimension(200,200)); 
     this.validate(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public static void main(String[]args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      new Main(); 
     } 
     catch(Exception e){} 
    } 

    public void actionPerformed(ActionEvent ae) 
    { 
     dialog.setVisible(true); 
    } 

    @Override 
    public void componentHidden(ComponentEvent arg0) {} 

    @Override 
    public void componentMoved(ComponentEvent arg0) 
    { 
     int x = this.getX() + this.getWidth(); 
     int y = this.getY(); 

     dialog.setDialogLocation(x, y); 
    } 

    @Override 
    public void componentResized(ComponentEvent arg0) {} 

    @Override 
    public void componentShown(ComponentEvent arg0) {} 
} 

Dialog.java

import java.awt.Dimension; 

import javax.swing.JButton; 
import javax.swing.JDialog; 


public class Dialog extends JDialog 
{ 
    private JButton done; 

    public Dialog() 
    { 
     done = new JButton("Done"); 

     this.add(done); 

     this.setSize(new Dimension(400,200)); 
    } 

    public void setDialogLocation(int x, int y) 
    { 
     this.setLocation(x, y); 
    } 

} 
0

我不知道任何内置函数,你可以只说“dialog.moveWithThisOtherWindow(otherWindow)”或其他一些这样的事情,它只是发生。你将不得不编写代码来自己做这件事。

在父窗体上创建鼠标侦听器或鼠标适配器。在鼠标侦听器中的“鼠标移动”事件中,移动子窗体。当然,家长必须要处理孩子。根据你创建窗口的方式,你可能需要某种“注册”功能,孩子可以打电话向父母标识自己。

+0

“*您可能需要某种”注册“功能,孩子可以调用以向父母标识自己。*”这就是为什么构造函数JDialog(Window所有者) '存在。 – 2012-08-06 18:34:47

+0

我在想,这个“粘在一起”的孩子窗口可能不会也是一个内置的“形式和紧密结合”感的孩子。我想我应该更加明确。 – Jay 2012-08-06 18:39:01

+0

好的,但如何获得右角的位置? – 2012-08-06 18:54:31

相关问题