JTextField 仅显示带有 GridBagLayout 的狭缝,我为类似问题找到的解决方案没有用

JTextField only shows a slit with GridBagLayout, solutions i found for similar problems didnt work

所以有一个 JFrame,其中包含 2 个 JPanel 和一个使用 GridBagLayout 的 Button。但问题在于也使用 GridBagLayouts 的面板。 JPanel p_addIng 有一个 3x2 网格,第一列有两个 JTextField,第一个的大小很好,但第二个太细了。

前面的一些免责声明:ing 代表成分,gbc 代表 GridBagConstraints,所有带有 b_var 的东西都意味着它是一个 swing 组件 b = JButton,tf = JTextField,p = JPanel,cb = JCombobox。

由于其他帖子,我已经尝试过的内容: gbc_addIng.fill = GridBagConstraints.HORIZONTAL; gbc_addIng.weightx = 1; 我还在某处看到可以使用 pack() 方法,但这是针对 JFrame 的,并没有采取任何措施来解决问题。 这是代码:

//making a new panel
//initializing the new Panel
p_addIng = new JPanel();
p_addIng.setName("Adding an Ingredient");
p_addIng.setPreferredSize(ingPanelDim);
p_addIng.setLayout(new GridBagLayout());
GridBagConstraints gbc_ing = new GridBagConstraints(0, 0, 1, 1, 1, 0,
                                                    GridBagConstraints.CENTER, GridBagConstraints.BOTH
                                                    new Insets(0,0,0,0),10, 10);

//the components of the panel
//TextField to set the name of the Ingredient
gbc_ing.gridx = 0; //the position of the component on the panel
gbc_ing.gridy = 0;
tf_ingName = new JTextField();
tf_ingName.setPreferredSize(new Dimension(ingPanelDim.width / 2, ingPanelDim.height/2));
tf_ingName.addActionListener(this);
p_addIng.add(tf_ingName, gbc_ing); //adding the component to the Panel
//endOfTextFieldIngName
gbc_ing.gridx = 1;
gbc_ing.gridy = 0;
//button to add the ingredient
tf_amnt = new JTextField("Choose amount");
tf_amnt.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
tf_amnt.addActionListener(this);
p_addIng.add(tf_amnt, gbc_ing);
//endOfButtonToAddIng
//button to add the ingredient
gbc_ing.gridx = 2;
gbc_ing.gridy = 0;
UnitOfMeasurement[] un = {UnitOfMeasurement.g, UnitOfMeasurement.kg, UnitOfMeasurement.Stück, UnitOfMeasurement.L, UnitOfMeasurement.ml, UnitOfMeasurement.cm};
cb_measurement = new JComboBox<UnitOfMeasurement>(un);
cb_measurement.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
cb_measurement.addActionListener(this);
p_addIng.add(cb_measurement, gbc_ing);
//endOfButtonToAddIng
//button to add the ingredient
gbc_ing.gridx = 0;
gbc_ing.gridy = 1;
b_addIng = new JButton("Add Ingredient");
b_addIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
b_addIng.addActionListener(this);
p_addIng.add(b_addIng, gbc_ing);
//endOfButtonToAddIng
//button to remove the ingredient
gbc_ing.gridx = 2;
gbc_ing.gridy = 1;
b_removeIng = new JButton("Remove Ingredient");
b_removeIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
b_removeIng.addActionListener(this);
p_addIng.add(b_removeIng, gbc_ing);
//endOfButtonToRemoveIng
//
frame.pack();
frame.add(p_addIng,gbc_frame);
//

如愿,这里是功能齐全的 class,唯一缺少的是枚举 UnitOfMeasurement,它包含 cb_meausrement 列表中显示的元素:


package WindowDesign;

import Food.UnitOfMeasurement;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

public class StartingWindow implements ActionListener {

    //window dimension
    private static final int WIDTH = 500;
    private static final int HEIGHT = 200;
    //

    //the JFrame
    private JFrame frame;
    //JComponents
    //adding an Ingredient
    private JPanel p_addIng;
    private Dimension ingPanelDim = new Dimension(WIDTH/2, HEIGHT/2);
    private JButton b_addIng; //a button to add an Ingredient
    private JButton b_removeIng;
    private JTextField tf_ingName;
    private JTextField tf_amnt;
    private JComboBox<UnitOfMeasurement> cb_measurement;
    //

    //adding a Recipe
    private JPanel p_addRecipe;
    private JButton b_addRecipe;    // add a Recipe to the list of Recipes
    private JButton b_removeRecipe;
    private JButton b_buildRecipe;
    private JButton b_clearRecipe;
    private JTextField tf_recipeName;
    private JTextField tf_ingredient;
    private JComboBox<UnitOfMeasurement> cb_measurementR;
    //

    //

    private JButton b_findCombination; //find the right combination of things to buy
    private JButton b_exit; //exit the program

    public StartingWindow(String name) {
        frame = new JFrame(name);
        frame.setLayout(new GridBagLayout());
        //constraints
        GridBagConstraints gbc_frame = new GridBagConstraints();
        frame.setSize(WIDTH, HEIGHT);
        frame.setBackground(Color.LIGHT_GRAY);
        gbc_frame.ipadx = 10;
        gbc_frame.ipady = 10;
        gbc_frame.weightx = 1;
        gbc_frame.fill = GridBagConstraints.BOTH;
        //Adding the Panels
        gbc_frame.gridx = 0;
        gbc_frame.gridy = 0;
        implementIngredientPanel(gbc_frame);

        gbc_frame.gridx += 1;
        gbc_frame.gridy = 0;
        implementRecipePanel(gbc_frame);
        //

        gbc_frame.gridx = 0;
        gbc_frame.gridy = 1;
        implementStartingPanel(gbc_frame);

        /*



        b_exit = new JButton("exit");
        b_exit.addActionListener(this);

         */

        /*
        frame.add(b_findCombination);
        frame.add(b_addIng);
        frame.add(b_addRecipe);
        frame.add(b_exit);
         */

        frame.setResizable(false);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.requestFocus();
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }
    //helper function to organize code
    private void implementIngredientPanel(GridBagConstraints gbc_frame){
        //making a new panel
        //initializing the new Panel
        p_addIng = new JPanel();
        p_addIng.setName("Adding an Ingredient");
        p_addIng.setPreferredSize(ingPanelDim);
        p_addIng.setLayout(new GridBagLayout());
        GridBagConstraints gbc_ing = new GridBagConstraints(0, 0, 1, 1, 1, 0,
                                                            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                                            new Insets(0,0,0,0),10, 10);

        //the components of the panel
        //TextField to set the name of the Ingredient
        gbc_ing.gridx = 0; //the position of the component on the panel
        gbc_ing.gridy = 0;
        tf_ingName = new JTextField();
        tf_ingName.setPreferredSize(new Dimension(ingPanelDim.width / 2, ingPanelDim.height/2));
        tf_ingName.addActionListener(this);
        p_addIng.add(tf_ingName, gbc_ing); //adding the component to the Panel
        //endOfTextFieldIngName
        gbc_ing.gridx = 1;
        gbc_ing.gridy = 0;
        //button to add the ingredient
        tf_amnt = new JTextField("Choose amount");
        tf_amnt.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
        tf_amnt.addActionListener(this);
        p_addIng.add(tf_amnt, gbc_ing);
        //endOfButtonToAddIng
        //button to add the ingredient
        gbc_ing.gridx = 2;
        gbc_ing.gridy = 0;
        UnitOfMeasurement[] un = {UnitOfMeasurement.g, UnitOfMeasurement.kg, UnitOfMeasurement.Stück, UnitOfMeasurement.L, UnitOfMeasurement.ml, UnitOfMeasurement.cm};
        cb_measurement = new JComboBox<UnitOfMeasurement>(un);
        cb_measurement.setPreferredSize(new Dimension(ingPanelDim.width/4, ingPanelDim.height/2));
        cb_measurement.addActionListener(this);
        p_addIng.add(cb_measurement, gbc_ing);
        //endOfButtonToAddIng
        //button to add the ingredient
        gbc_ing.gridx = 0;
        gbc_ing.gridy = 1;
        b_addIng = new JButton("Add Ingredient");
        b_addIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
        b_addIng.addActionListener(this);
        p_addIng.add(b_addIng, gbc_ing);
        //endOfButtonToAddIng
        //button to remove the ingredient
        gbc_ing.gridx = 2;
        gbc_ing.gridy = 1;
        b_removeIng = new JButton("Remove Ingredient");
        b_removeIng.setPreferredSize(new Dimension(ingPanelDim.width/2, ingPanelDim.height/2));
        b_removeIng.addActionListener(this);
        p_addIng.add(b_removeIng, gbc_ing);
        //endOfButtonToRemoveIng
        //
        frame.pack();
        frame.add(p_addIng,gbc_frame);
        //
    }

    private void implementRecipePanel(GridBagConstraints gbc_frame){
        //Adding a Panel to add
        p_addRecipe = new JPanel();
        p_addRecipe.setName("Adding an Ingredient");
        p_addRecipe.setPreferredSize(new Dimension(WIDTH/2, HEIGHT/2));
        p_addRecipe.setLayout(new GridBagLayout());
        GridBagConstraints gbc_recipe = new GridBagConstraints();


        //the components:
        //add Button to Add a Recipe
        b_addRecipe = new JButton("Add Recipe");
        b_addRecipe.addActionListener(this);
        p_addRecipe.add(b_addRecipe, gbc_recipe );
        //

        frame.add(p_addRecipe, gbc_frame);
    }

    private void implementStartingPanel(GridBagConstraints c){
        b_findCombination = new JButton("go shopping!");
        b_findCombination.addActionListener(this);
        c.fill = GridBagConstraints.HORIZONTAL;

        frame.add(b_findCombination, c);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {

    }
}

这是一个工作示例,可能是也可能不是您要的...

package Whosebug;

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

public class IngredientJFrame {

    public static void main(String... args) {

        IngredientJFrame ingredientJFrame = new IngredientJFrame();
        ingredientJFrame.init();
    }

    public void init() {

        JFrame jFrame = new JFrame();

        //making a new panel
        //initializing the new Panel
        JPanel p_addIng = new JPanel();
        p_addIng.setLayout(new GridBagLayout());

        GridBagConstraints gbc_ing = new GridBagConstraints();

        JLabel l_addIng = new JLabel("Add Ingredient");
        gbc_ing.gridx = 0;
        gbc_ing.gridy = 0;
        p_addIng.add(l_addIng, gbc_ing);

        JTextField tf_ingName = new JTextField();
        gbc_ing.gridx = 1;
        gbc_ing.gridy = 0;
        gbc_ing.fill = GridBagConstraints.HORIZONTAL;
        p_addIng.add(tf_ingName, gbc_ing); //adding the component to the Panel

        JLabel tf_amnt = new JLabel("Choose amount");
        gbc_ing.gridx = 0;
        gbc_ing.gridy = 1;
        gbc_ing.fill = GridBagConstraints.NONE;
        p_addIng.add(tf_amnt, gbc_ing);

        JComboBox cb_measurement = new JComboBox();
        gbc_ing.gridx = 1;
        gbc_ing.gridy = 1;
        gbc_ing.fill = GridBagConstraints.HORIZONTAL;
        p_addIng.add(cb_measurement, gbc_ing);

        JButton b_addIng = new JButton("Add Ingredient");
        gbc_ing.gridx = 0;
        gbc_ing.gridy = 2;
        gbc_ing.fill = GridBagConstraints.NONE;
        p_addIng.add(b_addIng, gbc_ing);

        JButton b_removeIng = new JButton("Remove Ingredient");
        gbc_ing.gridx = 1;
        gbc_ing.gridy = 2;
        p_addIng.add(b_removeIng, gbc_ing);

        jFrame.pack();
        jFrame.add(p_addIng);
        jFrame.setSize(500, 500);
        jFrame.setVisible(true);
    }
}

GridBayLayout (GBL) 和 GridBagConstraints (GBC) 功能强大,但很难正确使用,而且容易搞砸。

这里有一些关于我如何使用这两个 类 和其他制作基于 Swing 的技巧的提示 UI:

  • 文本一般应使用 JLabel 显示。这应该会立即帮助您格式化。
  • 一般来说,对于 GBL 和 GBC,我们通常不会在我们的组件上设置实际大小。我们允许组件相互反应并根据需要填充 space。通常在其他 JPanel 中使用 JPanel 以获得我们正在寻找的正确流程。
  • 我使用 GBC 的方式很可能不是很好,但我讨厌一遍又一遍地重新制作那个对象。因此,我在我的示例中重复使用了它,您在重复使用该对象时必须小心,因为很容易设置一个特定的字段而稍后忘记 'unset' 它。东西插入。
  • 通常一个组件有一个 anonymous inner class 实际监听该组件。

but the second one is way too thin.

当组件无法以其首选尺寸显示时使用 GridBagLayout,然后以其最小尺寸显示。 JTextField 的最小大小为 0,这就是您所看到的。

您有几个不同的问题:

p_addIng.setPreferredSize(ingPanelDim);

您人为地限制了面板的大小,导致文本字段以最小大小显示。删除该语句。

此外,您似乎试图让文本字段和组合框填充与按钮相同的 space。如果你想这样做,那么你需要让按钮跨越两列:

//gbc_ing.gridx = 2;
gbc_ing.gridx = 1;
gbc_ing.gridwidth = 2;

综上所述,您似乎忽略了使用布局管理器的要点。您不应该尝试设置每个组件的首选大小。您定义组件并让布局管理器完成工作。

例如,在创建 JTextField 时,您应该使用:

JTextField textField = new JTextField(10);

给出文本字段应该有多大的建议。