Java 在ComponentListener中传递2个对象,并传递这些对象

Java 在ComponentListener中传递2个对象,并传递这些对象,java,swing,jdialog,jcomponent,Java,Swing,Jdialog,Jcomponent,我想使用框架对象,以便对话框相对于父框架移动。我移动父框架,对话框随之移动。我想调用dialog.setLocationRelativeTo(//parent frame object//),这只有在我有父框架对象时才可能 如果有任何方法可以获得此窗口行为,请帮助我。您可以轻松创建一个组件侦听器,该侦听器引用您需要的任何对象 void NewJDialogcallone(JFrame frame) { location = frame.getLocationOnScreen();

我想使用框架对象,以便对话框相对于父框架移动。我移动父框架,对话框随之移动。我想调用
dialog.setLocationRelativeTo(//parent frame object//)
,这只有在我有父框架对象时才可能


如果有任何方法可以获得此窗口行为,请帮助我。

您可以轻松创建一个组件侦听器,该侦听器引用您需要的任何对象

void NewJDialogcallone(JFrame frame) 
{
    location = frame.getLocationOnScreen();
    int x = location.x;
    int y = location.y;
    dialog.setLocation(x, y);
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
    dialog.setAlwaysOnTop(true);
    dialog.addComponentListener(this);
}

public void componentMoved(ComponentEvent e,?????) 
{
    JOptionPane.showConfirmDialog (null,
                                "This is the \"Ok/Cancel\"message dialog box.",
                                "",
                                JOptionPane.OK_CANCEL_OPTION);
}

只需在方法参数
JFrame
前面添加关键字
final

    final Object one = new Object();
    final Object two = new Object();

    ComponentListener listener = new ComponentListener() {
        public void componentHidden(ComponentEvent e) {
            one.toString();
        }
        public void componentMoved(ComponentEvent e) {
            two.toString();
        }
        public void componentResized(ComponentEvent e) {
            one.toString();
        }
        public void componentShown(ComponentEvent e) {
            two.toString();
        }
    };
我还建议避免使用以下方法:

 void NewJDialogcallone(final JFrame frame) 
 ...
因为这对用户体验来说真的很烦人。通常,这是您没有正确实例化对话框的标志,即通过传递正确的框架/对话框所有者

下面是一个不使用setAlwayOnTop()的窗口位置同步示例:


dialog.setAlwaysOnTop(true);将对话框保持在框架上方。我希望它能够抑制这样一种行为:当我移动帧{parent frame}时,父帧前面的Jdialog框也应该相对于该帧移动和定位。你能给我推荐一些吗?非常感谢,这就是我想要的。一个聪明的回答解决了我的问题。我保证,当我把这篇文章贴到任何地方时,我都会给你信用卡,以便把它寄出去。
dialog.setAlwaysOnTop(true);
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    protected void initUI() {
        final JFrame frame = new JFrame();
        frame.setTitle("Test dialog synch");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // On the next line I pass "frame" to the dialog so that the dialog never
            // goes behind the frame, avoiding the need for setAlwaysOnTop
        final JDialog dialog = new JDialog(frame, false);
        dialog.setSize(200, 50);
        frame.addComponentListener(new ComponentAdapter() {

            private Point lastLocation;

            @Override
            public void componentMoved(ComponentEvent e) {
                if (lastLocation == null && frame.isVisible()) {
                    lastLocation = frame.getLocation();
                } else {
                    Point newLocation = frame.getLocation();
                    int dx = newLocation.x - lastLocation.x;
                    int dy = newLocation.y - lastLocation.y;
                    dialog.setLocation(dialog.getX() + dx, dialog.getY() + dy);
                    lastLocation = newLocation;
                }
            }

        });
        frame.setSize(400, 200);
        frame.setVisible(true);
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().initUI();
            }
        });
    }

}