首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ComponentListener中传递2个对象和这些对象

在ComponentListener中传递2个对象和这些对象
EN

Stack Overflow用户
提问于 2012-05-31 20:25:03
回答 2查看 133关注 0票数 0
代码语言:javascript
复制
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);
}

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-31 20:35:18

您只需要在方法参数JFrame frame前面添加关键字final即可。

代码语言:javascript
复制
 void NewJDialogcallone(final JFrame frame) 
 ...

我还建议避免使用以下代码:

代码语言:javascript
复制
dialog.setAlwaysOnTop(true);

因为它对用户体验真的很烦人。通常,这是你没有正确实例化你的对话框的标志,例如,通过传递正确的Frame/Dialog所有者。

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

代码语言:javascript
复制
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();
            }
        });
    }

}
票数 2
EN

Stack Overflow用户

发布于 2012-05-31 20:33:54

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

代码语言:javascript
复制
    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();
        }
    };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10833565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档