Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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_Layout - Fatal编程技术网

Java 此结构的最佳布局/嵌套布局

Java 此结构的最佳布局/嵌套布局,java,swing,layout,Java,Swing,Layout,我是Swing新手,我正在尝试创建如下界面: +----------------------------------+ | | |信息文本| | | +--------+----------------+--------+ || JPwdField || | +----------------+ | |按钮|按钮7 |按钮| | 1

我是Swing新手,我正在尝试创建如下界面:

+----------------------------------+
|                                  |
|信息文本|
|                                  |
+--------+----------------+--------+
|| JPwdField ||
|        +----------------+        |
|按钮|按钮7 |按钮|
|   1    +----------------+   3    |
|| JTextField ||
+--------+-------+--------+--------+
|        |       |        |        |
|        |       |        |        |
|按钮|按钮|按钮|按钮|
|   2    |   5   |   6    |   4    |
|        |       |        |        |
+--------+----------------+--------+

其想法是所有按钮都在其“单元格”中展开,因此我已经阅读了嵌套的
GridLayout
,但我在思考如何实现它时遇到了问题。有没有关于如何嵌套布局或更好的方法的建议?

我向您推荐。在我看来,它使用起来非常简单,功能也非常强大

代码可能如下所示:

public JPanel createPanel() {
    JPanel panel = new JPanel(new MigLayout("wrap 4, fill, debug", "align center", "align center"));

    panel.add(new JLabel("Information text"), "spanx 4, align center center");
    panel.add(new JButton("Button 1"), "spany 3");
    panel.add(new JTextField("Password"), "spanx 2");
    panel.add(new JButton("Button 3"), "spany 3");
    panel.add(new JButton("Button 7"), "spanx 2");
    panel.add(new JTextField("JTextField"), "spanx 2");
    panel.add(new JButton("Button 2"), "");
    panel.add(new JButton("Button 5"), "");
    panel.add(new JButton("Button 6"), "");
    panel.add(new JButton("Button 4"), "");

    return panel;
}

创建用户界面有一条硬性规定。您可以通过多种方式使用不同的布局来实现它

请查看所有可用的布局,并根据需要选择合适的布局

您可以使用具有不同布局的多个容器,最后将它们添加到另一个容器中

阅读



您可以尝试使用或使用包含水平布局的垂直布局

就代码可读性的复杂性而言,
GridBagLayout
将作为最佳解决方案。由于此结果最适合于
GridBagLayout
,只要知道如何解决相同的约束条件:-)

import java.awt.*;
导入javax.swing.*;
公共类GridBagLayoutExample{
gbc的私有网格;
私有JButton[]按钮;
public GridBagLayoutExample(){
按钮=新的JButton[7];
gbc=新的GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
}
私有void displayGUI(){
JFrame frame=新JFrame(“GridBagLayout示例”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane=newjpanel(newgridbaglayout());
对于(int i=0;i
输出:


请将您的最大努力添加为,也许我们可以帮助您修复它。这可以通过多个嵌套JPanel上的GirdLayout来实现。这取决于你想得到多严厉。您也可以使用GroupLayouts来实现这一点,但如果您没有以前的布局知识,它们会更难掌握。您还可以尝试提出“最佳方式”问题。他们还想看看你到目前为止都做了些什么。这是创业指南。非常感谢!除了你的代码外,我还使用了一点
ipady
,以使最后一行的按钮达到1和3的高度,并在调整应用程序大小时具有相应的重量。您的代码非常有用,我已经学会了如何使用它,谢谢!
import java.awt.*;
import javax.swing.*;

public class GridBagLayoutExample {

    private GridBagConstraints gbc;
    private JButton[] buttons;

    public GridBagLayoutExample() {
        buttons = new JButton[7];
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
    }

    private void displayGUI() {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new GridBagLayout());
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(Integer.toString(i + 1));          
        }
        JLabel informationLabel = new JLabel("Information Text", JLabel.CENTER);
        addComp(contentPane, informationLabel,
                            0, 0, 1.0, 0.20, 4, 1);
        addComp(contentPane, buttons[0],
                            0, 1, 0.25, 0.60, 1, 3);
        JPasswordField passwordField = new JPasswordField(10);
        addComp(contentPane, passwordField,
                            1, 1, 0.50, 0.20, 2, 1);
        addComp(contentPane, buttons[6],
                            1, 2, 0.50, 0.20, 2, 1);
        JTextField tField = new JTextField(10);
        addComp(contentPane, tField,
                            1, 3, 0.50, 0.20, 2, 1);
        addComp(contentPane, buttons[2],
                            3, 1, 0.25, 0.60, 1, 3);
        addComp(contentPane, buttons[1],
                            0, 4, 0.25, 0.20, 1, 1);
        addComp(contentPane, buttons[4],
                            1, 4, 0.25, 0.20, 1, 1);
        addComp(contentPane, buttons[5],
                            2, 4, 0.25, 0.20, 1, 1);
        addComp(contentPane, buttons[3],
                            3, 4, 0.25, 0.20, 1, 1);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp,
                        int x, int y, double wx, double wy,
                                int gw, int gh) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.weightx = wx;
        gbc.weighty = wy;
        gbc.gridwidth = gw;
        gbc.gridheight = gh;

        panel.add(comp, gbc);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new GridBagLayoutExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}