繁体   English   中英

我不知道如何安排这些按钮,如链接所示

[英]I can't figure out how to arrange these buttons as seen in the link

package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame; 
import javax.swing.JPanel;

public class GridBagLayoutEx2
{
     public GridBagLayoutEx2()
    {
    JFrame frame = new JFrame("GridBagLayoutEx2");
    frame.setVisible(true);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();

    JButton b1 = new JButton("1");
    JButton b2 = new JButton("2");
    JButton b3 = new JButton("3");
    JButton b4 = new JButton("4");
    JButton b5 = new JButton("5");

    gbc.insets = new Insets(2, 2, 2, 2);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridheight = 5;
    gbc.gridwidth = 3;
    gbc.fill = GridBagConstraints.VERTICAL;
    panel.add(b1, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    panel.add(b2, gbc);

    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    panel.add(b3, gbc);

    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.gridheight = 1;
    panel.add(b4, gbc);

    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.gridwidth = 3;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(b5, gbc);

    frame.add(panel);
    //frame.pack();
}

public static void main(String[] args) {
    new GridBagLayoutEx2();
}

}

在此处输入图片说明

大家好 我已经尝试解决这个GUI了一段时间了。 尽管我看了很多GUI教程,但经过无数次尝试和错误后,我放弃了。 在上图中,您可以看到我正在尝试制作的布局。 提前致谢...

考虑使用MigLayout管理器。 对于单位类型,我选择了厘米。 MigLayout提供了几种单位类型可供选择。

以下示例非常适合您的图像:

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class ButtonsEx extends JFrame {

    public ButtonsEx() {

        initUI();

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }

    private void initUI() {

        JPanel pnl = new JPanel(new MigLayout());
        pnl.add(new JButton("Button"), "spany 2, grow, w 4cm, h 4cm");
        pnl.add(new JButton("Button"), "spanx 2, w 4cm, h 2cm, grow");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm, wrap");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm, wrap");

        add(pnl);

        pack();
    }    

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ButtonsEx ex = new ButtonsEx();
                ex.setVisible(true);
            }
        });
    }
}

MigLayout示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM