【发布时间】:2020-05-10 05:23:40
【问题描述】:
所以有一个包含 2 个 JPanel 和一个使用 GridBagLayout 的 Button 的 JFrame。但问题在于也使用 GridBagLayouts 的面板。 JPanel p_addIng 有一个 3x2 网格,第一列中有两个 JTextField,第一个的大小很好,但第二个太薄了。
前面有一些免责声明:ing 代表成分,gbc 代表 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) {
}
}
【问题讨论】:
-
使用MigLayout,生活会轻松很多
-
如果你有时间,你能不能再写一个简洁、功能齐全的例子?
-
我刚刚做了 ;) 够了吗?
-
tf_ingName = new JTextField();应该类似于tf_ingName = new JTextField(8); // suggest a size in columns -
以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度 - 以显示额外空间应该如何被使用。
标签: java swing jpanel jtextfield gridbaglayout