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

}

ダイアログ.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 で [OK] ボタンをクリックすると、メイン フォームの右側にダイアログ フォームが追加されます。そのため、メイン フォームを移動すると、ダイアログも移動します。ただし、ダイアログ フォームは独立している必要があります。つまり、ダイアログ フォームの [x] ボタンをクリックすると、メイン フォームではなく、そのフォームのみが存在します。

ボタンがクリックされたときに、このダイアログ フォームをメイン フォームの右側にアタッチするにはどうすればよいですか? 助けてください!

4

2 に答える 2

2

The answer is not MouseListener, but it is ComponentListener. I managed to do it with using that listener's "componentMoved()" method.

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

}
于 2012-08-08T18:21:31.527 に答える
0

I'm not aware of any built-in function that you can just say "dialog.moveWithThisOtherWindow(otherWindow)" or some such and it just happens. You would have to write code to do this yourself.

Create a mouse listener or mouse adapter on the parent form. In the "mouse moved" event in the mouse listener, move the child form. Of course the parent would have to have a handle to the child. Depending how you create the windows, you may need some sort of "register" function that the child can call to identify himself to the parent.

于 2012-08-06T18:23:56.347 に答える