GridBagLayout没有按预期出来 [英] GridBagLayout not coming out as expected

查看:109
本文介绍了GridBagLayout没有按预期出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a GUI that should have buttons on top, a view screen in the middle, and at the bottom, an area with labels and text entry fields which change based on which button is pressed. The main panel on which everything lays is a GridBagLayout. The panel which holds the text entry fields is a CardLayout. I feel as though I've set the GridBagConstraints and everything else as it should be, but it comes up nothing as expected. I imagine there is a problem with how I've set the GridBagConstraints, or possibly an issue with sizing, but I'm not sure. I'll include a picture of what I get when I run it and what I'm trying to get.

代码:

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

        }


}

显示内容:

我想要的东西:

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

I should add that the following picture is what I get before I resize the window to the size of my computer's screen. You can see the label, here, though you can't in the full size version, and they are clumped together, so possibly it's an issue with the way I set my weightings, I just made it so at least every row and column had at least one component with a weighting of 1.

推荐答案

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

Well let's start of by saying that whatever modification you make to your GridBagConstraints instance is persisted to any of the following usages of that instance.
So if you do this gbc.gridwidth = 9 make sure to reset it to gbc.gridwidth = 1, otherwise every element added will have a gridwidth of 9 applied to it. And no need to set gridy = 0 everytime you add a button on the same line, that just adds unnecessary lines of code.

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

Now I have just cleaned up your Window() code to show you a correct use of the GridBagConstraints and removed the duplicate assigns. You will have to add your ActionListeners again

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

我得到了这个布局

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

The layout is probably not exactly what you wanted, but I hope you can continue your work with this example. ;)

这篇关于GridBagLayout没有按预期出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆