English 中文(简体)
清除 Java岛门乌控制区的规模
原标题:Getting the size of Menu control in Java SWT
  • 时间:2011-05-15 04:29:29
  •  标签:
  • java
  • swt

How do I get the height of a menu bar control in a Java SWT shell window?

看来,不同的非洲顾问办公室在双壳窗口顶端的菜单上具有不同的高度。 因此,如果我能够达到菜单的确切高度,那将是巨大的。 男用二苯乙胺的甲型异构体并没有达到该目标。

最佳回答

页: 1 Rectangle RecBounds ( of the org.eclipse.swt.widgets. Menu。 但问题是,该方法有缺省无障碍环境>。 其签名表明,它曾被用于“public, 有时是先期。 无论如何,解决你的问题可能有许多途径:

  1. Finding the height of the shell and subtracting the height of title area and height of client area. Problem is how to find the title area height?
  2. The second way is as twisted as the first one. Use the above mentioned API with the help of java Reflection. See the below code for more details. Specially the static int getMenuHeight(Menu parent) method.

<><>> 代码>说明: <>strong>/strong>Menu para amount should be the top and the visible menu instance. 因为,对一些非洲顾问办公室来说,暗藏的子器是在传单上建立的。 此外,APICA在Vista机器上测试,在SUN JDK1.6_b18下运行3.62ec。

<>>>>>t>>Code:

import java.lang.reflect.Method;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class MenuTest {

    public static void main (String [] args) 
    {
        Display display = new Display ();
        final Shell shell = new Shell (display);
        final Menu bar = new Menu (shell, SWT.BAR);
        shell.setMenuBar (bar);

        final Composite c = new Composite(shell, SWT.NONE);
        c.setLayout(new GridLayout());
        c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));

        MenuItem fileItem = new MenuItem (bar, SWT.CASCADE);
        fileItem.setText ("&File");
        Menu submenu = new Menu (shell, SWT.DROP_DOWN);
        fileItem.setMenu (submenu);
        MenuItem item = new MenuItem (submenu, SWT.PUSH);
        item.addListener (SWT.Selection, new Listener () {
            public void handleEvent (Event e) {
                System.out.println(shell.getSize().y -  shell.getClientArea().height);
                System.out.println(getMenuHeight(bar));
            }
        });


        item.setText ("Select &All	Ctrl+A");
        item.setAccelerator (SWT.MOD1 +  A );
        shell.setSize (200, 200);
        shell.open ();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }

    static int getMenuHeight(Menu parent)
    {
        try {
            Method m = Menu.class.getDeclaredMethod("getBounds", null);
            m.setAccessible(true);
            Rectangle r = (Rectangle) m.invoke(parent, null);
            return r.height;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
} 
问题回答

为了做到这一点,你将达到菜单和产权条条顶峰,并降低产权酒吧的高度。

int menuTitleHeight = x.getWindow().getHeight() - x.getWindow().getContentPane().getHeight();
int titleHeight = x.getWindow().getHeight() - x.getWindow().getRootPane().getHeight();
int menuHeight = x.getWindow().getHeight() - x.getWindow().getRootPane().getHeight(); 




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签