我正在创建一个GUI,该GUI的顶部应该有一个按钮,中间是一个视图屏幕,底部是一个带有标签和文本输入字段的区域,该区域根据按下的按钮而变化。放置所有内容的主面板是GridBagLayout。包含文本输入字段的面板是CardLayout。我觉得好像已经设置了GridBagConstraints以及其他所有应有的设置,但是却没有得到预期的效果。我想我如何设置GridBagConstraints可能有问题,或者可能与大小设置有关,但是我不确定。我将附上一张我运行它会得到什么以及想要获得什么的图片。

代码:

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.text.JTextComponent;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.GridBagLayout;
import java.util.concurrent.Executors;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.awt.Insets;

public class Window extends JFrame {

    public Window() {
        Container panel = this.getContentPane();
        panel.setLayout(new GridBagLayout());
        panel.setSize(1000,1000);
        GridBagConstraints gbc = new GridBagConstraints();

        JButton ordersButton = new JButton("Orders");
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(ordersButton, gbc);
        JButton dishesButton = new JButton("Dishes");
        gbc.gridx = 1;
        gbc.gridy = 0;
        panel.add(dishesButton, gbc);
        JButton ingredientsButton = new JButton("Ingredients");
        gbc.gridx = 2;
        gbc.gridy = 0;
        panel.add(ingredientsButton, gbc);
        JButton suppliersButton = new JButton("Suppliers");
        gbc.gridx = 3;
        gbc.gridy = 0;
        panel.add(suppliersButton, gbc);
        JButton staffButton = new JButton("Staff");
        gbc.gridx = 4;
        gbc.gridy = 0;
        panel.add(staffButton, gbc);
        JButton dronesButton = new JButton("Drones");
        gbc.gridx = 5;
        gbc.gridy = 0;
        panel.add(dronesButton, gbc);
        JButton usersButton = new JButton("Users");
        gbc.gridx = 6;
        gbc.gridy = 0;
        panel.add(usersButton, gbc);
        JButton postcodesButton = new JButton("Postcodes");
        gbc.gridx = 7;
        gbc.gridy = 0;
        panel.add(postcodesButton, gbc);
        JButton configurationButton = new JButton("Configuration");
        gbc.gridx = 8;
        gbc.gridy = 0;
        panel.add(configurationButton, gbc);

        JTextField viewScreen = new JTextField();
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.gridwidth = 9;
        gbc.gridheight = 2;
        viewScreen.setPreferredSize(new Dimension(650, 200));
        panel.add(viewScreen, gbc);


        JPanel ordersPanel = new JPanel();
        JPanel dishesPanel = new JPanel();

        ordersPanel.setLayout(new GridBagLayout());
        dishesPanel.setLayout(new GridBagLayout());

        ordersPanel.setSize(new Dimension(900, 500));
        dishesPanel.setSize(new Dimension(900, 500));

        JLabel orders1Label = new JLabel("order 1");
        gbc.gridx = 0;
        gbc.gridy = 0;
        ordersPanel.add(orders1Label, gbc);
        JLabel orders2Label = new JLabel("order 2");
        gbc.gridx = 0;
        gbc.gridy = 1;
        ordersPanel.add(orders2Label, gbc);
        JLabel dishes1Label = new JLabel("dish 1");
        gbc.gridx = 0;
        gbc.gridy = 0;
        dishesPanel.add(dishes1Label, gbc);
        JLabel dishes2Label = new JLabel("dish 2");
        gbc.gridx = 0;
        gbc.gridy = 1;
        dishesPanel.add(dishes2Label, gbc);

        JTextField orders1TextField = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.gridwidth = 4;
        ordersPanel.add(orders1TextField, gbc);
        JTextField orders2TextField = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.gridwidth = 4;
        ordersPanel.add(orders2TextField, gbc);
        JTextField dishes1TextField = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.gridwidth = 4;
        dishesPanel.add(dishes1TextField, gbc);
        JTextField dishes2TextField = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.gridwidth = 4;
        dishesPanel.add(dishes2TextField, gbc);

        orders1TextField.setPreferredSize(new Dimension(400, 20));
        orders2TextField.setPreferredSize(new Dimension(400, 20));
        dishes1TextField.setPreferredSize(new Dimension(400, 20));
        dishes2TextField.setPreferredSize(new Dimension(400, 20));

        JPanel entryFields = new JPanel();
        CardLayout c1 = new CardLayout();
        entryFields.setLayout(c1);
        entryFields.setSize(new Dimension(900, 600));
        gbc.gridheight = 7;
        gbc.gridwidth = 9;
        gbc.weighty = 1;

        gbc.gridy = GridBagConstraints.RELATIVE;
        entryFields.add("ordersPanel",ordersPanel);
        entryFields.add("dishesPanel", dishesPanel);
        c1.show(entryFields, "ordersPanel");
        panel.add(entryFields, gbc);

        ordersButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                c1.show(entryFields, "ordersPanel");
            }
        });


        dishesButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                c1.show(entryFields, "dishesPanel");
            }
        });


        //Display window
        setSize(800,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

        public static void main(String[] args) {
            Window win = new Window();

        }


}


出现什么:

java - GridBagLayout没有按预期出来-LMLPHP

我想要的是:

java - GridBagLayout没有按预期出来-LMLPHP

我应该补充一下,以下图片是在将窗口调整为计算机屏幕大小之前得到的。您可以在此处看到标签,尽管您不能使用完整尺寸的标签,而且它们会聚在一起,所以可能是我设置权重方式的问题,我这样做是因为至少每一行和每一列都有至少一个权重为1。

java - GridBagLayout没有按预期出来-LMLPHP

最佳答案

好吧,我们先说一下,您对GridBagConstraints实例所做的任何修改都会保留到该实例的以下任何用法中。
因此,如果执行此操作,请确保将其重置为gbc.gridwidth = 9,否则每个添加的元素都将应用9的网格宽度。
无需每次在同一行上添加按钮时都设置gbc.gridwidth = 1,这只会添加不必要的代码行。

现在,我刚刚清理了您的gridy = 0代码,向您展示了Window()的正确用法,并删除了重复的分配。
您将不得不再次添加您的GridBagConstraints

public Window() {
    Container panel = this.getContentPane();
    panel.setLayout(new GridBagLayout());
    panel.setSize(1000,1000);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1;
    gbc.weighty = 1;

    gbc.gridy = 0; // First row

    JButton ordersButton = new JButton("Orders");
    gbc.gridx = 0;
    panel.add(ordersButton, gbc);

    JButton dishesButton = new JButton("Dishes");
    gbc.gridx = 1;
    panel.add(dishesButton, gbc);

    JButton ingredientsButton = new JButton("Ingredients");
    gbc.gridx = 2;
    panel.add(ingredientsButton, gbc);

    JButton suppliersButton = new JButton("Suppliers");
    gbc.gridx = 3;
    panel.add(suppliersButton, gbc);

    JButton staffButton = new JButton("Staff");
    gbc.gridx = 4;
    panel.add(staffButton, gbc);

    JButton dronesButton = new JButton("Drones");
    gbc.gridx = 5;
    panel.add(dronesButton, gbc);

    JButton usersButton = new JButton("Users");
    gbc.gridx = 6;
    panel.add(usersButton, gbc);

    JButton postcodesButton = new JButton("Postcodes");
    gbc.gridx = 7;
    panel.add(postcodesButton, gbc);

    JButton configurationButton = new JButton("Configuration");
    gbc.gridx = 8;
    panel.add(configurationButton, gbc);

    JTextField viewScreen = new JTextField();
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 9;
    viewScreen.setPreferredSize(new Dimension(650, 200));
    viewScreen.setMinimumSize(new Dimension(650, 200));
    panel.add(viewScreen, gbc);
    gbc.gridwidth = 1;  // Reset the gridwidth

    // Third row
    JLabel orders1Label = new JLabel("order 1");
    gbc.gridx = 1;
    gbc.gridy = 2;
    panel.add(orders1Label, gbc);
    JLabel orders2Label = new JLabel("order 2");
    gbc.gridx = 1;
    gbc.gridy = 3;
    panel.add(orders2Label, gbc);
    JLabel dishes1Label = new JLabel("dish 1");
    gbc.gridx = 1;
    gbc.gridy = 4;
    panel.add(dishes1Label, gbc);
    JLabel dishes2Label = new JLabel("dish 2");
    gbc.gridx = 1;
    gbc.gridy = 5;
    panel.add(dishes2Label, gbc);

    gbc.gridwidth = 4; // Textfield width
    JTextField orders1TextField = new JTextField();
    gbc.gridx = 2;
    gbc.gridy = 2;
    panel.add(orders1TextField, gbc);
    JTextField orders2TextField = new JTextField();
    gbc.gridx = 2;
    gbc.gridy = 3;
    panel.add(orders2TextField, gbc);
    JTextField dishes1TextField = new JTextField();
    gbc.gridx = 2;
    gbc.gridy = 4;
    panel.add(dishes1TextField, gbc);
    JTextField dishes2TextField = new JTextField();
    gbc.gridx = 2;
    gbc.gridy = 5;
    panel.add(dishes2TextField, gbc);

    orders1TextField.setPreferredSize(new Dimension(400, 20));
    orders2TextField.setPreferredSize(new Dimension(400, 20));
    dishes1TextField.setPreferredSize(new Dimension(400, 20));
    dishes2TextField.setPreferredSize(new Dimension(400, 20));

    //Display window
    setSize(800,600);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}


我得到了这个布局

java - GridBagLayout没有按预期出来-LMLPHP

布局可能不完全是您想要的布局,但是我希望您可以通过该示例继续进行工作。 ;)

10-04 11:10