How do I determine the height of an application window's title bar in a Java SWT application application?

Since Java is platform agnostic, I need to find the Java way.

As a side question, is the title/caption bar height information in the same location as other system/window metrics?

I tried searching for "caption bar" and "title bar", just did not see anything.

有帮助吗?

解决方案

You basically want to calculate the height difference between the "bounds" of your Shell and the "client area" of the Shell:

public static void main(String args[])
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Calculate");
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            Rectangle outer = Display.getCurrent().getActiveShell().getBounds();
            Rectangle inner = Display.getCurrent().getActiveShell().getClientArea();

            System.out.println(outer.height - inner.height);
        }
    });

    shell.pack();
    shell.setSize(400, 200);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!shell.getDisplay().readAndDispatch())
            shell.getDisplay().sleep();
    }
}

Will print 31 (px) on my laptop running Linux Mint.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top