JDialog:删除标题栏,保留边框 [英] JDialog: Remove titlebar, keep border

查看:496
本文介绍了JDialog:删除标题栏,保留边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从JDialog中删除标题栏,但保留边框?

Is it possible to remove the title bar from a JDialog, but keeping the border?

基本SSCCE如下所示:

The base SSCCE looks like this:

package test;

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

public class SSCCE extends JFrame {
    private JDialog dialog;

    public SSCCE() {
        dialog = new JDialog();
        dialog.setSize(100, 100);
        dialog.add(new JList<>(new String[] { "One", "Two", "Three" }));
        dialog.setUndecorated(true);

        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void showDialog() {
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SSCCE ex = new SSCCE();
                ex.setVisible(true);
                ex.showDialog();
            }
        });
    }
}

运行它,我看到了:

但是我不希望JDialog上的标题栏.标准答案是使用setUndecorated(true).但是后来我也失去了窗口边框,我不想要那样.看起来像这样:

But I don't want the title bar on the JDialog. The standard answer is to use setUndecorated(true). But then I lose the window borders as well, and I don't want that. Looks like this:

我想要的是这样的样机:

What I want is something like this mockup:

我该如何实现?

我尝试过LaF边界之一,但结果却有些奇怪.为了进行测试,我使用了"RootPane.errorDialogBorder",并希望对话框周围有一个红色的粗边框.但是我得到了这个:

I tried going with one of the LaF borders, but I'm getting some strange results. Just for testing I went with "RootPane.errorDialogBorder" and expected to get a fat red border around my dialog. But instead I got this:

在我看来,这就像是"RootPane.frameBorder"边框的未集中版本.为什么我得到那个呢?

That looks to me like the unfocused version of the "RootPane.frameBorder" border. Why did I get that one instead?

推荐答案

它必须以某种方式基于当前的LaF边框样式.

It needs to somehow be based on the current LaF border style.

您已经知道不可能使用实际的对话框/框架边框.

As you already know it is not possible to use the actual dialog/frame border.

似乎您需要一个边框来突出显示弹出窗口,以便它在文本字段中脱颖而出.我认为最接近的方法是使用LAF定义的Border.例如,您也许可以使用在UIManager中定义的"TitledBorder.border"属性,它是一个简单的Border,但对于每个LAF似乎都略有不同.

Seems like you need a Border to highlight the popup so it stands out from the text field. I think the closest you can come is to use a Border that is defined by the LAF. For example you might be able to use the "TitledBorder.border" property defined in the UIManager it is a simple Border but appears to be slightly different for each LAF.

看看 UIManager默认.它显示每个LAF的所有边框

Take a look at UIManager Default. It displays all the Border for each LAF

这篇关于JDialog:删除标题栏,保留边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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