更改Java按钮GridBayLayout的大小

更改Java按钮GridBayLayout的大小,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,你好,我有个大问题。我想使=按钮2*高度和0按钮2*宽度其他按钮应该只是正常大小,这就是我尝试了许多组合,但我得到奇怪的大小 我明白了吗 我想得到类似的东西,我发现在webonly按钮布局 public void someMethod() Container cp = getContentPane(); cp.setLayout(new BorderLayout()); JPanel wyswietlacz = new JPanel(); JTextField t

你好,我有个大问题。我想使=按钮2*高度和0按钮2*宽度其他按钮应该只是正常大小,这就是我尝试了许多组合,但我得到奇怪的大小

我明白了吗

我想得到类似的东西,我发现在webonly按钮布局

public void someMethod()
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    JPanel wyswietlacz = new JPanel();
    JTextField txt = new JTextField("123");
    txt.setPreferredSize(new Dimension(getWidth() - 10, 35));
    txt.setAlignmentX(JTextField.RIGHT_ALIGNMENT);
    wyswietlacz.add(txt);
    JPanel opcje = new JPanel();
    String[] etykiety = { "C", ".", "/", "*", "7", "8", "9", "-", "4", "5",
            "6", "+", "1", "2", "3", "=", "0", "+/-" };

    JButton[] przyciski = new JButton[18];
    for (int i = 0; i < przyciski.length; i++)
        przyciski[i] = new JButton(etykiety[i]);

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    opcje.setLayout(gridbag);
    for (int i = 0; i < przyciski.length; i++) {
        if (((i + 1) % 4) == 0) {

            c.gridwidth = GridBagConstraints.REMAINDER;
        } else {
            c.gridwidth = GridBagConstraints.RELATIVE;

        }

        if (i == 15) {
            c.gridheight = 2;

            c.fill = GridBagConstraints.HORIZONTAL;
        }
        if (i == 16)
            c.gridy = GridBagConstraints.SOUTH;

        if (i == 16) {
            c.gridwidth = 2;
            c.fill = GridBagConstraints.HORIZONTAL;
        }

        makebutton(przyciski[i], gridbag, c, opcje);
    }

    add(wyswietlacz, BorderLayout.NORTH);
    add(opcje, BorderLayout.CENTER);

}

protected void makebutton(JButton button, GridBagLayout gridbag,
        GridBagConstraints c, JPanel jp) {
    gridbag.setConstraints(button, c);
    jp.add(button);
}

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

我看不出有什么问题,请阅读

然后

源代码

import java.awt.*;
import javax.swing.*;

public class GridBagButtons {

    private static final Insets insets = new Insets(0, 0, 0, 0);

    public static void main(final String args[]) {
        final JFrame frame = new JFrame("GridBagLayout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridBagLayout());
        JButton button;
        // Row One - Three Buttons
        button = new JButton("One");
        addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Two");
        addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Three");
        addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        // Row Two - Two Buttons
        button = new JButton("Four");
        addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Five");
        addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        // Row Three - Two Buttons
        button = new JButton("Six");
        addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Seven");
        addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        frame.setSize(500, 200);
        frame.setVisible(true);
    }

    private static void addComponent(Container container, Component component, int gridx, int gridy,
            int gridwidth, int gridheight, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
                anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

    private GridBagButtons() {
    }
}
我会使用NestedLayout,而不是GrigBagLayout、SpringLayout或MigLayout
我看不出有什么问题,请阅读

然后

源代码

import java.awt.*;
import javax.swing.*;

public class GridBagButtons {

    private static final Insets insets = new Insets(0, 0, 0, 0);

    public static void main(final String args[]) {
        final JFrame frame = new JFrame("GridBagLayout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridBagLayout());
        JButton button;
        // Row One - Three Buttons
        button = new JButton("One");
        addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Two");
        addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Three");
        addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        // Row Two - Two Buttons
        button = new JButton("Four");
        addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Five");
        addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        // Row Three - Two Buttons
        button = new JButton("Six");
        addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        button = new JButton("Seven");
        addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        frame.setSize(500, 200);
        frame.setVisible(true);
    }

    private static void addComponent(Container container, Component component, int gridx, int gridy,
            int gridwidth, int gridheight, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
                anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

    private GridBagButtons() {
    }
}
我会使用NestedLayout,而不是GrigBagLayout、SpringLayout或MigLayout
问题是您没有在GridBagConstraints上设置weightx和weighty。将它们设置为1.0将为每个组件平均分配水平/垂直额外空间。

问题在于您没有在GridBagConstraints上设置weightx和weighty。将它们设置为1.0将为每个组件平均分配水平/垂直额外空间。

尝试使用此代码示例,并询问可能出现的任何问题:

import java.awt.*;
import javax.swing.*;

public class GridBagTest
{
    private String[] buttonText = { "C", ".", "/", "*", "7", "8", "9", "-", "4", "5",
            "6", "+", "1", "2", "3", "=", "0", "+/-" };
    private JButton[] button = new JButton[18];
    private int counter = 0;

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());

        JPanel northPanel = new JPanel();
        northPanel.setLayout(new BorderLayout(2, 2));
        JTextField tfield = new JTextField();
        northPanel.add(tfield, BorderLayout.CENTER);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.PAGE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.insets = new Insets(2, 2, 2, 2);
        for (int i = 0; i < button.length; i++)
        {
            System.out.println("Button Text : " + buttonText[i]);
            button[i] = new JButton(buttonText[i]);
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                    gbc.gridx = j;
                    gbc.gridy = i;
                    centerPanel.add(button[counter++], gbc);
            }
        }
        gbc.gridx = 0;
        gbc.gridy = 3;
        centerPanel.add(button[counter++], gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        centerPanel.add(button[counter++], gbc);
        gbc.gridx = 2;
        gbc.gridy = 3;
        centerPanel.add(button[counter++], gbc);
        gbc.gridx = 3;
        gbc.gridy = 3;  
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        centerPanel.add(button[counter++], gbc);
        int count = counter;
        System.out.println(button[--count].getText());
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridheight = 1;
        gbc.gridwidth = 2;
        centerPanel.add(button[counter++], gbc);
        gbc.gridwidth = 1;
        gbc.gridx = 2;
        gbc.gridy = 4;
        centerPanel.add(button[counter++], gbc);

        contentPane.add(northPanel, BorderLayout.PAGE_START);
        contentPane.add(centerPanel, BorderLayout.CENTER);

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

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagTest().createAndDisplayGUI();
            }
        });
    }
}
以下是输出:


尝试使用此代码示例,并询问可能出现的任何问题:

import java.awt.*;
import javax.swing.*;

public class GridBagTest
{
    private String[] buttonText = { "C", ".", "/", "*", "7", "8", "9", "-", "4", "5",
            "6", "+", "1", "2", "3", "=", "0", "+/-" };
    private JButton[] button = new JButton[18];
    private int counter = 0;

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());

        JPanel northPanel = new JPanel();
        northPanel.setLayout(new BorderLayout(2, 2));
        JTextField tfield = new JTextField();
        northPanel.add(tfield, BorderLayout.CENTER);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.PAGE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.insets = new Insets(2, 2, 2, 2);
        for (int i = 0; i < button.length; i++)
        {
            System.out.println("Button Text : " + buttonText[i]);
            button[i] = new JButton(buttonText[i]);
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                    gbc.gridx = j;
                    gbc.gridy = i;
                    centerPanel.add(button[counter++], gbc);
            }
        }
        gbc.gridx = 0;
        gbc.gridy = 3;
        centerPanel.add(button[counter++], gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        centerPanel.add(button[counter++], gbc);
        gbc.gridx = 2;
        gbc.gridy = 3;
        centerPanel.add(button[counter++], gbc);
        gbc.gridx = 3;
        gbc.gridy = 3;  
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        centerPanel.add(button[counter++], gbc);
        int count = counter;
        System.out.println(button[--count].getText());
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridheight = 1;
        gbc.gridwidth = 2;
        centerPanel.add(button[counter++], gbc);
        gbc.gridwidth = 1;
        gbc.gridx = 2;
        gbc.gridy = 4;
        centerPanel.add(button[counter++], gbc);

        contentPane.add(northPanel, BorderLayout.PAGE_START);
        contentPane.add(centerPanel, BorderLayout.CENTER);

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

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagTest().createAndDisplayGUI();
            }
        });
    }
}
以下是输出:


谢谢我更正代码:我得到了这样的东西![]也许定制MigLayout更好更简单,抱歉,但我对他们的许可证不太了解,对这个GridBag有什么想法吗:从默认库?@Robert Kilar,我不知道为什么,抱歉,我给你发了GridBagLayout如何工作的完整规范inc代码示例,我不想调试你的代码,没有理由,嗯,我们这里没有任何类型的编码支持,在这个论坛上,我们可以向您发送正确的信息并向您显示正确的方向,谢谢我更正了代码:我得到了类似的东西![]也许定制MigLayout更好更简单,抱歉,但我对他们的许可证不太了解,对这个GridBag有什么想法吗:从默认库?@Robert Kilar,我不知道为什么,抱歉,我给你发了GridBagLayout如何工作的完整规范inc代码示例,我不想调试你的代码,没有理由,嗯,我们这里没有任何类型的编码支持,在这个论坛上,我们可以给您发送正确的信息,并向您显示正确的方向,谢谢,现在它可以工作了,但前提是我使用正确的窗口大小,否则最后一行将适合窗口。@RobertKilar您说的太宽是什么意思?左边是JFrame的尺寸500300,右边是180,208@RobertKilar看起来您仅为某些按钮设置了weightx/weighty。我知道你想让按钮填满整个区域。谢谢,现在它可以工作了,但只有当我使用正确的窗口大小时,否则最后一排就适合窗口了。@RobertKilar,你说太宽是什么意思?左边是JFrame的尺寸500300,右边是180,208@RobertKilar看起来您仅为某些按钮设置了weightx/weighty。我知道你想让按钮填满整个区域。@RobertKilar:不客气,请保持微笑:-这也是一个奇特的GridBagLayout示例。1+@RobertKilar:非常欢迎你,保持微笑:-这也是一个奇特的GridBagLayout示例。1+请确保someMethod不是绘画方法!请确保someMethod不是绘画方法!