Итак, есть JFrame, который содержит 2 JPanels и одну кнопку, используя GridBagLayout. Но проблема заключается в панелях, которые также используют GridBagLayouts. JPanel p_addIng имеет сетку 3x2 с двумя JTextFields в первом столбце, один из которых имеет размер первого размера, но второй слишком тонкий.
Некоторые отказ от ответственности впереди: ing обозначает ингредиент, gb c для GridBagConstraints все с b_var означает его компонент свинга 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);
//
Здесь, по желанию, полностью функциональный класс, единственное, чего не хватает, это перечисление 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) {
}
}