Java 如何设置对话框';相对于标题宽度的宽度是多少?

Java 如何设置对话框';相对于标题宽度的宽度是多少?,java,swing,width,jframe,jdialog,Java,Swing,Width,Jframe,Jdialog,我有一个JDialog,里面只有几个组件。我想使对话框尽可能小。目前我正在使用pack()。这会产生意想不到的效果,将对话框的宽度减小到无法完全看到标题的程度。我希望对话框的宽度始终足够大,以便标题始终完全可见 我在用秋千。我意识到标题栏的外观/字体是由操作系统决定的。我更喜欢使用swing,所以目前我正计划根据JLabel的字体计算标题字符串的宽度。然后我会将其中一个组件的最小宽度设置为该值 有没有更好的方法来打包JDialog,同时保持其标题可见?1)使用FontMetrics查找标题的宽度

我有一个JDialog,里面只有几个组件。我想使对话框尽可能小。目前我正在使用pack()。这会产生意想不到的效果,将对话框的宽度减小到无法完全看到标题的程度。我希望对话框的宽度始终足够大,以便标题始终完全可见

我在用秋千。我意识到标题栏的外观/字体是由操作系统决定的。我更喜欢使用swing,所以目前我正计划根据JLabel的字体计算标题字符串的宽度。然后我会将其中一个组件的最小宽度设置为该值

有没有更好的方法来打包JDialog,同时保持其标题可见?

1)使用
FontMetrics
查找标题的宽度

2) 向该值中添加一个表示窗口图标和X(关闭)按钮的数字(您应该猜得到)

3) 使用上述值设置对话框的宽度

您无法找到所需的确切宽度大小,但这是一种很好的猜测方法。

public static void adjustWidthForTitle(JDialog对话框)
 public static void adjustWidthForTitle(JDialog dialog)
{
    // make sure that the dialog is not smaller than its title
    // this is not an ideal method, but I can't figure out a better one
    Font defaultFont = UIManager.getDefaults().getFont("Label.font");
    int titleStringWidth = SwingUtilities.computeStringWidth(new JLabel().getFontMetrics(defaultFont),
            dialog.getTitle());

    // account for titlebar button widths. (estimated)
    titleStringWidth += 110;

    // set minimum width
    Dimension currentPreferred = dialog.getPreferredSize();

    // +10 accounts for the three dots that are appended when the title is too long
    if(currentPreferred.getWidth() + 10 <= titleStringWidth)
    {
        dialog.setPreferredSize(new Dimension(titleStringWidth, (int) currentPreferred.getHeight()));

    }
}
{ //确保对话框不小于其标题 //这不是一个理想的方法,但我想不出更好的方法 Font-defaultFont=UIManager.getDefaults().getFont(“Label.Font”); int titleStringWidth=SwingUtilities.computeStringWidth(新的JLabel().getFontMetrics(默认字体), dialog.getTitle()); //说明标题栏按钮宽度。(估计) 标题宽度+=110; //设置最小宽度 维度currentPreferred=dialog.getPreferredSize(); //+10表示标题过长时附加的三个点
如果(currentPreferred.getWidth()+10,您最好使用首选大小作为基础,但请考虑:?感谢您的链接。我不确定如何让布局管理器(如form layout)来完成此任务,但我接受了您关于覆盖getPreferredSizeThanks的建议,以便与我合作;+1。对不起,我不明白“form layout”在此上下文中。JGoodies FormLayout是我的goto布局管理器。我不认为它或我所知道的任何其他布局管理器设置为处理窗口大小调整。链接中的公认答案似乎表明,应该始终使用布局管理器,而不是使用setSize().我的情况是个例外吗?啊,还有待解决。:-)被接受的答案对
setPreferredSize()
;您对
getPreferredSize()的计算提出了异议
基于首选尺寸似乎是合理的。遗憾的是,我想不出一个更好的框架装饰估算。我对整个练习持谨慎态度:我有太多的封闭源代码商业应用程序,具有不可调整大小的框架,在开发者的L&F上可能看起来不错。
JDialog dialog = new JDialog()
    {
        @Override
        public Dimension getPreferredSize()
        {
            Dimension retVal = super.getPreferredSize();

            String title = this.getTitle();

            if(title != null)
            {
                Font defaultFont = UIManager.getDefaults().getFont("Label.font");
                int titleStringWidth = SwingUtilities.computeStringWidth(new JLabel().getFontMetrics(defaultFont),
                        title);

                // account for titlebar button widths. (estimated)
                titleStringWidth += 110;

                // +10 accounts for the three dots that are appended when
                // the title is too long
                if(retVal.getWidth() + 10 <= titleStringWidth)
                {
                    retVal = new Dimension(titleStringWidth, (int) retVal.getHeight());
                }
            }
            return retVal;
        }

    };