有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JFace应用程序窗口:createContents不工作

我试图创建一个分为三部分的窗口。一个不可调整大小的页眉和页脚,然后是一个内容区域,可以扩展以填充窗口中的剩余区域。首先,我创建了以下类:

public class MyWindow extends ApplicationWindow {
    Color white;
    Font mainFont;
    Font headerFont;

    public MyWindow() {
        super(null);
        }

    protected Control createContents(Composite parent) {
        Display currentDisplay = Display.getCurrent();
        white = new Color(currentDisplay, 255, 255, 255);
        mainFont = new Font(currentDisplay, "Tahoma", 8, 0);
        headerFont = new Font(currentDisplay, "Tahoma", 16, 0);

        // Main layout Composites and overall FillLayout
        Composite container = new Composite(parent, SWT.NO_RADIO_GROUP);
        Composite header = new Composite(container, SWT.NO_RADIO_GROUP);
        Composite mainContents = new Composite(container, SWT.NO_RADIO_GROUP);;
        Composite footer = new Composite(container, SWT.NO_RADIO_GROUP);;
        FillLayout containerLayout = new FillLayout(SWT.VERTICAL);
        container.setLayout(containerLayout);

        // Header
        Label headerLabel = new Label(header, SWT.LEFT);
        headerLabel.setText("Header");
        headerLabel.setFont(headerFont);

        // Main contents
        Label contentsLabel = new Label(mainContents, SWT.CENTER);
        contentsLabel.setText("Main Content Here");
        contentsLabel.setFont(mainFont);

        // Footer
        Label footerLabel = new Label(footer, SWT.CENTER);
        footerLabel.setText("Footer Here");
        footerLabel.setFont(mainFont);

        return container;
    }

    public void dispose() {
        cleanUp();
    }

    @Override
    protected void finalize() throws Throwable {
        cleanUp();
        super.finalize();
    }

    private void cleanUp() {
        if (headerFont != null) {
            headerFont.dispose();
        }
        if (mainFont != null) {
            mainFont.dispose();
        }
        if (white != null) {
            white.dispose();
        }
    }
}

当我这样运行它时,结果是一个空窗口:

public static void main(String[] args) {
    MyWindow myWindow = new MyWindow();
    myWindow.setBlockOnOpen(true);
    myWindow.open();
    Display.getCurrent().dispose();
}

我没有看到三个标签的显示方式有什么不对?肯定调用了createContents代码,我可以在调试模式下在Eclipse中逐步完成它


共 (0) 个答案