Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaSwing创建带有for循环的GUI_Java_Swing_Jradiobutton - Fatal编程技术网

使用JavaSwing创建带有for循环的GUI

使用JavaSwing创建带有for循环的GUI,java,swing,jradiobutton,Java,Swing,Jradiobutton,我有一个GUI,从这个GUI中我选择一个带有预订系统尺寸的输入文件(新GUI),如果飞机有100个座位,GUI将是例如10x10,如果飞机有200个座位,它将是10x20,所有座位都将是按钮,如果已预订,则应在其上存储乘客信息。此外,已预订的座位不得再次预订 问题是GUI的尺寸将根据座位号和方向而变化,这意味着在一个版本中,另一个版本上的按钮可能会更少,更多的座位也意味着GUI将更长或更宽,可能是10厘米x10厘米或10厘米x20厘米,或15厘米x25厘米 我想用for循环来实现这一点,但我不想

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

问题是GUI的尺寸将根据座位号和方向而变化,这意味着在一个版本中,另一个版本上的按钮可能会更少,更多的座位也意味着GUI将更长或更宽,可能是10厘米x10厘米或10厘米x20厘米,或15厘米x25厘米


我想用for循环来实现这一点,但我不想把按钮放在GUI之外。我不知道如何在另一个按钮旁边随意添加一个按钮。我总是使用Eclipse Window Builder来构建GUI,但我想我需要自己编写这个。。。有人能帮我吗?一些提示?谢谢你的时间

网格布局开始。每次需要更改座椅布局时,请重新应用
GridLayout

也就是说,如果你需要100个座位,可以使用新的
GridLayout(20,10)

就我个人而言,我会使用
GridBagLayout
,但对于手头的任务来说,它可能太复杂了

添加/移动新内容有点困难。我个人会重建UI并重新应用模型

至于大小,您可能无法对其进行太多控制,因为它在不同屏幕上的显示方式不同。相反,您应该提供防止UI过大屏幕的方法

这可以通过将座椅窗格放置在
JScrollPane
中来实现。这将允许座椅窗格在尺寸上膨胀和收缩,而不会在屏幕上出现尺寸过大的情况

看看


试试这样的方法:

// 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;
}
现在,您有了所有按钮的列表,可以轻松访问它们并进行更改,例如更改按钮文本,如下所示

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
}
import org.eclipse.swt.swt;
导入org.eclipse.swt.graphics.Rectangle;
导入org.eclipse.swt.widgets.Button;
导入org.eclipse.swt.widgets.Display;
导入org.eclipse.swt.widgets.Shell;
公共类ButtonTest扩展Shell{
/**
*启动应用程序。
*@param args
*/
//代码从这里开始。
公共静态void main(字符串参数[]){
试一试{
Display=Display.getDefault();
//创建一个名为“shell”的窗口(shell)
ButtonTest外壳=新的ButtonTest(显示);
//这两条线是外壳的起点。
shell.open();
shell.layout();
//现在我们可以开始在外壳中添加东西了。
//窗户的大小。
矩形边界=shell.getBounds();
//有多少排按钮。
int numorrows=5;
//每行有多少个按钮。
int numOfColumns=3;
int buttonWidth=bounds.width/numorrows;
int buttonHeight=bounds.height/numof列;
按钮;
int x=0;
int y=0;
对于(int i=0;i
此外,我在嵌套循环中有一点输入错误(忘记了在每行之后将x值重置为0)。这在这次编辑中得到了修复

您还需要导入swt才能使其工作。 转到此处:下载操作系统的最新版本,然后进入eclipse,右键单击项目>构建路径>配置构建路径>库选项卡>添加外部JAR>找到下载的swt文件并单击


很抱歉回答这么长,希望它能帮上忙:D

是的,Window Builder是一个很棒的插件,可以帮助我可视化东西,但在Window Builder中,我自己手动放置了所有按钮,比如说我自己放置了20个按钮……但我不能总是这样做……它需要像你给出的for循环那样自动化,我也在考虑类似于是,谢谢,我仍在尝试并将要更新:)这是我能想到的,可能我在边界上做了一些错误的事情,主要是错误地调用GUI:/AHA,将“public class GUI_Rec{”更改为“public class GUI_Rec extensed JFrame{”。这解决了调用GUI的问题。(我认为还有更多问题需要解决,lemme见)我也可以使用swt,只要它有效:)当我在谷歌上搜索它说类矩形在awt库中时,我添加了
扩展Jframe
,并在
frame.setVisible(true);
,左上角打开了一个小窗口,但没有任何按钮要在awt中添加按钮,您需要添加(按钮)但是,当我做这周的时候,会发生一些事情…所以,鉴于swt也很好,我将举一个简单的例子。使用JtoggleButton作为预订系统
buttons.get(indexOfButton).setText("SomeText");
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
}