gpt4 book ai didi

java - 使用 Java Swing 创建带有 for 循环的 GUI

转载 作者:行者123 更新时间:2023-12-04 04:54:08 25 4
gpt4 key购买 nike

我有一个 GUI,从这个 GUI 我选择一个输入文件的尺寸与预订系统(新 GUI)如果飞机有 100 个座位,GUI 将是例如 10x10,如果飞机有 200 个座位,它将是 10x20,所有座位将成为按钮,如果预订,应在其上存储乘客信息。此外,已预订的座位不得再次预订。

问题是 GUI 的尺寸会根据座位号和方向而变化,这意味着在一个版本中,另一个版本可以有很多按钮,而更多的座位意味着 GUI 会更长或更宽,可能 10 厘米 x 10 厘米或 10 厘米 x 20 厘米,或 15 厘米 x 25 厘米...

我正在考虑使用 for 循环来执行此操作,但我不想将按钮从 GUI 中删除。我不知道如何在另一个按钮旁边任意添加一个按钮。我总是使用 Eclipse Window Builder 来构建 GUI,但我想我需要自己编写这个……有人可以帮助我吗?一些提示?谢谢您的时间!

最佳答案

尝试这样的事情:

// The size of the window.
Rectangle bounds = this.getBounds();

// How many rows of buttons.
int numOfRows = 100;

// How many buttons per row.
int numOfColumns = 50;

int buttonWidth = bounds.width / numOfRows;
int buttonHeight = bounds.height / numOfColumns;

int x = 0;
int y = 0;

for(int i = 0; i < numOfColumns; i++){
for(int j = 0; j < numOfRows; j++){
// Make a button
button.setBounds(x, y, buttonWidth, buttonHeight);
y += buttonHeight;
}
x += buttonWidth;
}

这将使所有按钮都适合您的窗口。

这是关于矩形的链接,它们在做这样的事情时非常方便。
http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fgraphics%2FRectangle.html

此外,您仍然可以使用 windowbuilder,事实上我建议您这样做。它确实可以帮助您可视化所需的尺寸。并且您还可以使用代码手动创建内容(按钮、列表、下拉菜单等),并且假设您将变量放在 WindowBuilder 所在的位置,它仍会在“设计”选项卡中显示它们。

希望这有帮助!

编辑:我对如何使用循环制作按钮有点含糊

要制作带有循环的按钮,您将需要一个缓冲区(用于存储一个足够长的临时按钮以添加到列表中)和......一个列表:D。

这是它的外观:

外循环:
// The list to store your buttons in.
ArrayList<Button> buttons = new ArrayList<Button>();

// The empty button to use as a buffer.
Button button;

内部循环('//Make a button' 注释所在的位置):
button = new Button(this, SWT.NONE);
buttons.add(button);

现在您拥有了所有按钮的列表,并且可以轻松访问它们并进行更改,例如像这样更改按钮文本;
buttons.get(indexOfButton).setText("SomeText");

编辑:

看到你是 swt 的新手(我无法让 awt/JFrame 工作)这里是完整的代码。
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ButtonTest extends Shell {

/**
* Launch the application.
* @param args
*/

// Code starts here in main.
public static void main(String args[]) {
try {
Display display = Display.getDefault();
// Creates a window (shell) called "shell"
ButtonTest shell = new ButtonTest(display);
// These two lines start the shell.
shell.open();
shell.layout();

// Now we can start adding stuff to our shell.

// The size of the window.
Rectangle bounds = shell.getBounds();

// How many rows of buttons.
int numOfRows = 5;

// How many buttons per row.
int numOfColumns = 3;

int buttonWidth = bounds.width / numOfRows;
int buttonHeight = bounds.height / numOfColumns;

Button button;

int x = 0;
int y = 0;

for(int i = 0; i < numOfColumns; i++){
for(int j = 0; j < numOfRows; j++){
button = new Button(shell, SWT.NONE);
button.setBounds(x, y, buttonWidth, buttonHeight);
x += buttonWidth;
}
x = 0;
y += buttonHeight;
}



// This loop keeps the shell from killing itself the moment it's opened
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the shell.
* @param display
*/
public ButtonTest(Display display) {
super(display, SWT.SHELL_TRIM);
createContents();
}

/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(800, 800);

}

@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}

另外,我在嵌套循环中犯了一个小错误(并且忘记在每行之后将 x 值重置为 0)。它已在此编辑中修复。

您还需要导入 swt 才能工作。
去这里: http://www.eclipse.org/swt/并为您的操作系统下载最新版本,然后进入 eclipse,右键单击您的项目 > 构建路径 > 配置构建路径 > 库选项卡 > 添加外部 JAR > 找到您下载的 swt 文件并单击。

抱歉回答这么长,希望能帮到你:D

关于java - 使用 Java Swing 创建带有 for 循环的 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17014007/

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