GridBagLayout没有按预期出来

时间:2019-02-28 11:22:37

标签: java swing layout-manager gridbaglayout

我正在创建一个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();

        }


}

显示内容:

enter image description here

我想要的东西:

enter image description here

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

enter image description here

2 个答案:

答案 0 :(得分:1)

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

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

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);
}

我得到了这个布局

generated image

布局可能不完全是您想要的,但是我希望您可以继续使用此示例。 ;)

答案 1 :(得分:1)

请考虑将设计分解为更小,更易于布局的容器。
它使代码清晰明了,并具有很高的灵活性:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Window extends JFrame {

    public Window() {

        Container panel = this.getContentPane(); //uses Borderlayout by default

        JPanel buttonsPane = new JPanel(new GridLayout(1, 0));
        JButton ordersButton = new JButton("Orders");
        buttonsPane.add(ordersButton);
        JButton dishesButton = new JButton("Dishes");
        buttonsPane.add(dishesButton);
        JButton ingredientsButton = new JButton("Ingredients");
        buttonsPane.add(ingredientsButton);
        JButton suppliersButton = new JButton("Suppliers");
        buttonsPane.add(suppliersButton);
        JButton staffButton = new JButton("Staff");
        buttonsPane.add(staffButton);
        JButton dronesButton = new JButton("Drones");
        buttonsPane.add(dronesButton);
        JButton usersButton = new JButton("Users");
        buttonsPane.add(usersButton);
        JButton postcodesButton = new JButton("Postcodes");
        buttonsPane.add(postcodesButton);
        JButton configurationButton = new JButton("Configuration");
        buttonsPane.add(configurationButton);
        panel.add(buttonsPane, BorderLayout.PAGE_START); //add to top

        JTextField viewScreen = new JTextField();
        viewScreen.setPreferredSize(new Dimension(650, 200));
        panel.add(viewScreen, BorderLayout.CENTER); //add to center

        JPanel ordersList = new JPanel();
        ordersList.setLayout(new BoxLayout(ordersList, BoxLayout.Y_AXIS));

        JPanel ordersPanel = new JPanel(); //uses FlowLayout by default
        ordersList.add(ordersPanel);
        JLabel orders1Label = new JLabel("order 1");
        ordersPanel.add(orders1Label);
        JTextField orders1TextField = new JTextField(50);
        ordersPanel.add(orders1TextField);

        JPanel ordersPanel1 = new JPanel();
        ordersList.add(ordersPanel1);
        JLabel orders2Label = new JLabel("order 2");
        ordersPanel1.add(orders2Label);
        JTextField orders2TextField = new JTextField(50);
        ordersPanel1.add(orders2TextField);

        JPanel dishesList = new JPanel();
        dishesList.setLayout(new BoxLayout(dishesList, BoxLayout.Y_AXIS));

        JPanel dishesPanel = new JPanel();
        dishesList.add(dishesPanel);
        JLabel dishes1Label = new JLabel("dish 1");
        dishesPanel.add(dishes1Label);
        JTextField dishes1TextField = new JTextField(50);
        dishesPanel.add(dishes1TextField);

        JPanel dishesPanel1 = new JPanel();
        dishesList.add(dishesPanel1);
        JLabel dishes2Label = new JLabel("dish 2");
        dishesPanel1.add(dishes2Label);
        JTextField dishes2TextField = new JTextField(50);
        dishesPanel1.add(dishes2TextField);

        JPanel entryFields = new JPanel();
        CardLayout c1 = new CardLayout();
        entryFields.setLayout(c1);

        entryFields.add("ordersPanel",ordersList);
        entryFields.add("dishesPanel", dishesList);
        c1.show(entryFields, "ordersPanel");
        panel.add(entryFields,BorderLayout.PAGE_END);

        ordersButton.addActionListener(arg0 -> c1.show(entryFields, "ordersPanel"));
        dishesButton.addActionListener(arg0 -> c1.show(entryFields, "dishesPanel"));

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->new Window());
    }
}

enter image description here