我有一个GridBagLayout,出于某种原因,我的JTextArea已经决定它不想侦听gridbag的比率,并且当创建包含布局的面板时,我的文本框会长大到它占用了屏幕的1/2。下面是一些代码:GridBagLayout中的JTextArea窃取太多空间
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,我希望我的左对象跨越屏幕的四分之五,并且正确的对象跨越最后的1/5?在这个版本中,我尝试使用加权来实现,在我的原始版本中,我尝试使用单元格进行操作,所以左侧对象位于gridx = 0 gridwidth = 4,右侧对象是gridx = 4 gridwidth = 1
这两种方法都无效。我也开到备用的想法(比如更好的布局,或者一个JTable?)
感谢您的帮助, 大通
它在做什么 尺寸为它应该做的
为了更好地帮助越早,张贴[SSCCE(http://sscce.org/)。 – 2013-02-21 05:13:21
如果将textarea放入JScrollPane中,该怎么办? – StanislavL 2013-02-21 05:32:58
你可以发布启动JTextArea的代码吗?从我的头顶开始,我将设置textarea的列数,而不是给它任何水平权重。请记住,权重用于评估的额外可用空间比 – 2013-02-21 10:07:53