为什么我的标签或按钮都没有出现?

问题描述 投票:0回答:1
import customwidgets.widgets;

public class CharacterCreation {

    public static void charCreation() {
        Shell charCreate = new Shell(Display.getCurrent());
        //Shell represents a window within the application
        charCreate.setSize(500, 500);

        Label race = new Label(charCreate, 0);
        race.setText("Race");
        race.setLocation(0, 100);

        Label name = new Label(charCreate, 0);
        name.setText("Name");
        name.setLocation(0, 200);

        Label SPname = new Label(charCreate, 0);
        SPname.setText("SPname");
        SPname.setLocation(0, 300);


        Button submit = new Button(charCreate, SWT.PUSH);
        submit.setText("Submit");
        submit.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                try {
                    createCharSheet(race.getText(), name.getText(), SPname.getText());
                    Label success = widgets.createLabel(charCreate, SWT.CENTER, "Character Created!");
                    success.setLocation(400,100);

                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });     
        Button close = widgets.createCloseButton(charCreate);
        close.setLocation(400, 400);
        charCreate.open();  
    }

此窗口由另一个文件/窗口中的按钮打开。每当按下该按钮时,窗口就会打开,但是我添加的标签或按钮都没有。这是什么问题?

java swt
1个回答
0
投票

setLocation不会设置控件的大小,因此它们默认为零大小。您可以改用setBounds

Label race = new Label(charCreate, 0);
race.setText("Race");
race.setBounds(0, 100, 100, 20);

Label name = new Label(charCreate, 0);
name.setText("Name");
name.setBounds(0, 200, 100, 20);

Label SPname = new Label(charCreate, 0);
SPname.setText("SPname");
SPname.setBounds(0, 300, 100, 20);

....

但是,使用位置和边界不是一个好习惯,因为控件大小不会针对不同的字体大小进行调整。而是使用layouts

Shell charCreate = new Shell(Display.getCurrent());

charCreate.setLayout(new GridLayout());

Label race = new Label(charCreate, 0);
race.setText("Race");

Label name = new Label(charCreate, 0);
name.setText("Name");

Label SPname = new Label(charCreate, 0);
SPname.setText("SPname");

....

charCreate.layout();
charCreate.pack();
© www.soinside.com 2019 - 2024. All rights reserved.