0

请看下面的代码

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

}

对话框.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));
    }

}

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

单击按钮时,如何将此对话框表单附加到主表单的右侧?请帮忙!

4

2 回答 2

2

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

主.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) {}
}

对话框.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);
    }

}
于 2012-08-08T18:21:31.527 回答
0

我不知道您可以说“dialog.moveWithThisOtherWindow(otherWindow)”或类似的任何内置函数,它就发生了。您必须自己编写代码来执行此操作。

在父窗体上创建鼠标侦听器或鼠标适配器。在鼠标侦听器中的“鼠标移动”事件中,移动子窗体。当然,父母必须对孩子有所了解。根据您创建窗口的方式,您可能需要某种“注册”函数,孩子可以调用该函数来向父母表明自己的身份。

于 2012-08-06T18:23:56.347 回答