Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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
Java 多个组件的复杂布局_Java_Swing_User Interface_Layout Manager_Miglayout - Fatal编程技术网

Java 多个组件的复杂布局

Java 多个组件的复杂布局,java,swing,user-interface,layout-manager,miglayout,Java,Swing,User Interface,Layout Manager,Miglayout,我正在设计一个GUI,其中包含如下所示的组件。但是在第二行,我想管理组件之间的空间。例如,在第二行中,我想将JLabel“移动到时间”和JTextField放在一起,而不需要太多的间距。正如我所看到的,现在MigLayout将JTextField与第一行的第二个组件对齐。然后,第二行中的JButton“Move”应与第一行中的第二个组件对齐。我如何做到这一点?下面是我的代码。我在MigLayout上查阅了许多备忘单和快速入门指南,但我还没找到。有人能推荐我的代码片段中的更改吗?多谢各位 ` `

我正在设计一个GUI,其中包含如下所示的组件。但是在第二行,我想管理组件之间的空间。例如,在第二行中,我想将
JLabel
“移动到时间”和
JTextField
放在一起,而不需要太多的间距。正如我所看到的,现在
MigLayout
JTextField
与第一行的第二个组件对齐。然后,第二行中的
JButton
“Move”应与第一行中的第二个组件对齐。我如何做到这一点?下面是我的代码。我在MigLayout上查阅了许多备忘单和快速入门指南,但我还没找到。有人能推荐我的代码片段中的更改吗?多谢各位

`

`

实际上,我打算让下面的GUI完全包含所有按钮和文本字段,还有其他布局更适合吗?如果是,是否可以指向推荐布局的示例代码段?多谢各位


< P>你可以考虑两种基本方法: 一种是使用高度灵活的布局管理器,如
MigLayout
。我对它不熟悉,但我可以推荐:

    JPanel helper = new JPanel(  );
    GridBagLayout gbl_helper = new GridBagLayout();
    gbl_helper.columnWidths = new int[]{200, 7, 200, 60, 18, 0};
    gbl_helper.rowHeights = new int[]{20, 20};
    gbl_helper.columnWeights = new double[]{1.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_helper.rowWeights = new double[]{0.0, 0};
    helper.setLayout(gbl_helper);

    GridBagConstraints gbc1 = new GridBagConstraints();
    gbc1.fill = GridBagConstraints.HORIZONTAL;
    gbc1.gridwidth = 2;
    gbc1.anchor = GridBagConstraints.NORTHWEST;
    gbc1.insets = new Insets(5,5,5,5);
    gbc1.gridx = 0;
    gbc1.gridy = 0;
    JButton button1 = new JButton( "Add puncta coordinates from CSV" );
    helper.add( button1, gbc1 );

    GridBagConstraints gbc2 = new GridBagConstraints();
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    gbc2.gridwidth = 2;
    gbc2.anchor = GridBagConstraints.NORTHWEST;
    gbc2.insets = new Insets(5,5,5,5);
    gbc2.gridx = 2;
    gbc2.gridy = 0;
    JButton button2 = new JButton( "Add track coordinates from CSV" );

    helper.add( button2, gbc2 );

    GridBagConstraints gbc3 = new GridBagConstraints();
    gbc3.insets = new Insets(0, 0, 0, 5);
    gbc3.gridx = 0;
    gbc3.gridy = 1;
    JLabel label = new JLabel( "Move to time:" );
    helper.add( label, gbc3 );

    JTextField tMoveTime = new JTextField();
    tMoveTime.setColumns(15);
    GridBagConstraints gbc4 = new GridBagConstraints();
    gbc4.anchor = GridBagConstraints.WEST;
    gbc4.insets = new Insets(0, 0, 0, 5);
    gbc4.gridx = 1;
    gbc4.gridy = 1;
    helper.add( tMoveTime, gbc4);

    JButton bMoveTime = new JButton("Move");
    GridBagConstraints gbc5 = new GridBagConstraints();
    gbc5.insets = new Insets(0, 0, 0, 5);
    gbc5.anchor = GridBagConstraints.NORTHEAST;
    gbc5.gridx = 3;
    gbc5.gridy = 1;
    helper.add( bMoveTime, gbc5 );

另一种方法是使用子面板将复杂布局划分为简单布局,每个子面板都有自己的布局管理器:


它必须是
miglaway
?@c0der不,它不必是,但事实上我是从miglaway开始的。事实上,我正在尝试使用所有按钮和字段制作完整的GUI(更新问题并发布完整的GUI图片)。我从MigLayout开始,你能推荐什么?另外,你能为推荐的布局发布一些有用的代码片段吗?谢谢
    JPanel helper = new JPanel(  );
    GridBagLayout gbl_helper = new GridBagLayout();
    gbl_helper.columnWidths = new int[]{200, 7, 200, 60, 18, 0};
    gbl_helper.rowHeights = new int[]{20, 20};
    gbl_helper.columnWeights = new double[]{1.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_helper.rowWeights = new double[]{0.0, 0};
    helper.setLayout(gbl_helper);

    GridBagConstraints gbc1 = new GridBagConstraints();
    gbc1.fill = GridBagConstraints.HORIZONTAL;
    gbc1.gridwidth = 2;
    gbc1.anchor = GridBagConstraints.NORTHWEST;
    gbc1.insets = new Insets(5,5,5,5);
    gbc1.gridx = 0;
    gbc1.gridy = 0;
    JButton button1 = new JButton( "Add puncta coordinates from CSV" );
    helper.add( button1, gbc1 );

    GridBagConstraints gbc2 = new GridBagConstraints();
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    gbc2.gridwidth = 2;
    gbc2.anchor = GridBagConstraints.NORTHWEST;
    gbc2.insets = new Insets(5,5,5,5);
    gbc2.gridx = 2;
    gbc2.gridy = 0;
    JButton button2 = new JButton( "Add track coordinates from CSV" );

    helper.add( button2, gbc2 );

    GridBagConstraints gbc3 = new GridBagConstraints();
    gbc3.insets = new Insets(0, 0, 0, 5);
    gbc3.gridx = 0;
    gbc3.gridy = 1;
    JLabel label = new JLabel( "Move to time:" );
    helper.add( label, gbc3 );

    JTextField tMoveTime = new JTextField();
    tMoveTime.setColumns(15);
    GridBagConstraints gbc4 = new GridBagConstraints();
    gbc4.anchor = GridBagConstraints.WEST;
    gbc4.insets = new Insets(0, 0, 0, 5);
    gbc4.gridx = 1;
    gbc4.gridy = 1;
    helper.add( tMoveTime, gbc4);

    JButton bMoveTime = new JButton("Move");
    GridBagConstraints gbc5 = new GridBagConstraints();
    gbc5.insets = new Insets(0, 0, 0, 5);
    gbc5.anchor = GridBagConstraints.NORTHEAST;
    gbc5.gridx = 3;
    gbc5.gridy = 1;
    helper.add( bMoveTime, gbc5 );
    JPanel helper = new JPanel(  );
    helper.setLayout(new BoxLayout(helper, BoxLayout.Y_AXIS));
    getContentPane().add(helper);

    JPanel topPanel = new JPanel();
    helper.add(topPanel);
    JButton button1 = new JButton( "Add puncta coordinates from CSV" );
    topPanel.add(button1);
    JButton button2 = new JButton( "Add track coordinates from CSV" );
    topPanel.add(button2);

    JPanel bottomPanel = new JPanel();
    helper.add(bottomPanel);
    bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));

    JPanel bottomRIght = new JPanel();
    bottomPanel.add(bottomRIght);
    JLabel label = new JLabel( "Move to time:" );
    bottomRIght.add(label);
    JTextField tMoveTime = new JTextField();
    bottomRIght.add(tMoveTime);
    tMoveTime.setColumns(15);

    JPanel bottomleftPanel = new JPanel();
    bottomPanel.add(bottomleftPanel);
    bottomleftPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    JButton bMoveTime = new JButton("Move");
    bottomleftPanel.add(bMoveTime);