gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:19:24 25 4
gpt4 key购买 nike

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();
}

此窗口是通过另一个文件/窗口中的按钮打开的。每当该按钮计时时,窗口就会打开,但我添加的标签或按钮都不在其中。这里有什么问题吗?

最佳答案

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();

关于java - 为什么我的标签或按钮都没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58792943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com