我有一个 GridBagLayout,出于某种原因,我的 JTextArea 决定它不想听 gridbag 的比例,当创建包含布局的面板时,我的文本框会增长到占据屏幕的 1/2。继承人一些代码:
tp = new JTabbedPane();
tp.setFont(Main.f);
//Adds tabs with several buttosn to my tabbed pane
for(Menu menu : FileManager.menus){
JPanel tmp = new JPanel();
int s = (int) Math.ceil(Math.sqrt(menu.products.size()));
tmp.setLayout(new GridLayout(s,s));
for(Product p : menu.products){//Act as
p.setFont(Main.f);
p.addActionListener(this);
tmp.add(p);
}
tp.addTab(menu.name,null,tmp,null);
}
receipt.setBorder(BorderFactory.createEtchedBorder());
//starting up with GridBag
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.ipadx = 0;
gbc.ipady = 0;
//sets up and adds the JTabbedPane
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.8;
gbc.weighty = 0.8;
add(tp,gbc);
//sets up and adds receipt - The one that takes up half the screen
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.2;
receipt.setEditable(false);
receipt.setText("Ticket number: 0\n" +
"Transaction number: 0\n");
receipt.setLineWrap(true);
add(receipt,gbc);
//sets up and adds a JPanel that has a bunch of buttons on it(Uses gridlayout)
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 0.2;
add(buttons,gbc);
buttons.setLayout(new GridLayout(1,8));
createButton(newtable);
createButton(remove);
for(JButton a : quicks)
createButton(a);
createButton(pay);
createButton(exit);
revalidate();
repaint();
当我将 TextArea 更改为空白 JButton 时,它根本不会占用太多空间。基本上,该空间由右侧的对象填充。如何告诉 gridbag 我希望左侧对象跨越屏幕的 4/5,而右侧对象跨越最后 1/5?在这个版本中,我尝试使用加权来做到这一点,在我的原始版本中,我尝试使用单元格来做到这一点,所以左边的对象在 gridx=0 gridwidth = 4,右边的对象是 gridx = 4 gridwidth = 1
两种方法都没有奏效。我也愿意接受其他想法(比如更好的布局或 JTable?)
谢谢你的帮助,蔡斯
它在做什么 尺寸对于它应该做什么