JDialog允许用户仅更改对话框的宽度 [英] JDialog allow user to only change width of the dialog

查看:110
本文介绍了JDialog允许用户仅更改对话框的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道是否可以限制用户调整JDialog的大小?

Does anyone know if it is possible to limit how the user resizes a JDialog?

我知道我可以调用方法setResizible(boolean)并且禁用或启用用户调整JDialog的大小,但有没有办法限制用户更改窗口的高度,但允许他更改宽度?

I know i can call the method setResizible(boolean) and that disables or enables the user from resizing the JDialog, but is there a way to restrict the user from changing the height of the window but allowing him to change the width?

我正在创建的对话框看起来很有趣,如果它垂直增长但水平增长该组件可能对用户有利。

The dialog I'm creating looks funny if it grows vertically but horizontally growing the component could be a benefit to the user.

提前致谢。

推荐答案

您可以添加 ComponentListener 到JDialog,如果高度发生变化,请检入 componentResized 。这可以通过以下方式扩展JDialog类来实现:

You could add a ComponentListener to the JDialog, and check in componentResized if height changed. This could be implemented by extending the JDialog class this way:

public class Dialog extends javax.swing.JDialog {

    public Dialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        final int h = getHeight();
        addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent e) {
                Rectangle b = getBounds();
                if (b.height != h) {
                    b.height = h;
                    setBounds(b);
                }
                super.componentResized(e);
            }
        });
    }
}

这篇关于JDialog允许用户仅更改对话框的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆