所以有一个JFrame,它包含两个JPanel和一个使用GridBagLayout的按钮。但问题在于同样使用网格布局的面板。JPanel p_addIng有一个3x2网格,第一列中有两个JTextField,第一列的大小很好,但第二列太薄了。
前面有一些免责声明:ing代表配料,gbc代表GridBagConstraints,所有带有b_var的东西都意味着它是一个swing组件b=JButton,tf=JTextField,p=JPanel,cb=JCombobox。
因为其他帖子,我已经尝试过了:gbc_addIng.fill=GridBagConstraint。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);
//
这里,正如所希望的,是全功能类唯一缺少的是枚举UnitOf测量,其中包含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) {
}
}
但是第二个太薄了。
当使用GridBagLayout时,如果组件无法以其首选大小显示,则会以其最小大小显示。JTextField的最小大小是0,所以这就是您看到的。
你有几个不同的问题:
p_addIng.setPreferredSize(ingPanelDim);
您人为地限制了面板的大小,这导致文本字段以其最小大小显示。删除该声明。
此外,您似乎试图让文本字段和组合框填充与按钮相同的空间。如果要执行此操作,则需要让按钮跨越两列:
//gbc_ing.gridx = 2;
gbc_ing.gridx = 1;
gbc_ing.gridwidth = 2;
以上所说的,你似乎错过了使用布局管理器的要点。您不应该尝试设置每个组件的首选大小。定义组件并让布局管理器执行此工作。
例如,在创建JTextField时,您应该使用:
JTextField textField = new JTextField(10);
给出文本字段应该有多大的建议。
这里有一个工作的例子,可能是也可能不是你所要求的...
package stackoverflow;
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)和GridBagConstraint(GBC)功能强大,但很难正确使用,而且容易搞砸。
以下是我如何使用这两个类的一些建议,以及制作基于Swing的UI的其他技巧:
和我的项目结构中的图像(我测试了它的jsp页面,出现了类似的错误,现在我在一个使用main方法(TestCalender.java)的类中测试): 我看到类似的问题和测试他们的答案,但没有发现我的错误变化!
下面的代码是我解决这个问题的尝试。我使用了基于1的索引。我无法找出错误。我试着调试代码,但没有帮助。我已经被困了两天了。请帮忙。
下面是我的密码 这里是错误 10-30 02:47:02.699 2195-2195/com。srg。ibc。任命申请书W/EGL_genymotion﹕ 未实施eglSurfaceAttrib 10-30 02:47:03.075 2195-2195/com。srg。ibc。任命应用程序E/WindowManager﹕ 活动网。srg。ibc。任命申请。CalendarView已泄漏Window
从操作系统概念 5.8.2使用显示器的餐饮哲学家解决方案 接下来,我们通过对用餐哲学家问题提出一个无死锁的解决方案来说明监控概念。这个解决方案施加了一个限制,即哲学家只有在筷子都可用的情况下才能拿起筷子。为了给这个解决方案编码,我们需要区分我们可能找到哲学家的三种状态。为此,我们引入以下数据结构: 哲学家只有当她的两个邻居不吃饭时,我才能设置变量
本文向大家介绍Pyecharts地图显示不完成问题解决方案,包括了Pyecharts地图显示不完成问题解决方案的使用技巧和注意事项,需要的朋友参考一下 官网给的解释如下: 自从 0.3.2 开始,为了缩减项目本身的体积以及维持 pyecharts 项目的轻量化运行,pyecharts 将不再自带地图 js 文件。 如用户需要用到地图图表,可自行安装对应的地图文件包。 全球国家地图: echarts
我已经搜索了stackoverflow大约一个小时,但没有解决方案可以解决我的问题。 我想运行这段代码: 但是如果我用nodejs运行这个,我会得到这个错误: 我会感谢你的帮助。