2013-02-21 65 views
1

我有一个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?)

感谢您的帮助, 大通

它在做什么 What it does 尺寸为它应该做的 enter image description here

+0

为了更好地帮助越早,张贴[SSCCE(http://sscce.org/)。 – 2013-02-21 05:13:21

+0

如果将textarea放入JScrollPane中,该怎么办? – StanislavL 2013-02-21 05:32:58

+0

你可以发布启动JTextArea的代码吗?从我的头顶开始,我将设置textarea的列数,而不是给它任何水平权重。请记住,权重用于评估的额外可用空间比 – 2013-02-21 10:07:53

回答

2

如果你没有确切地以4/5和1/5的比例固定,你可以使用BorderLayout并将按钮放到中间,并将文本区域放到东部。为您提供

边框布局将宽显示文本区域 - 指定首选大小(宽度,可以设置高度,你想要的,边界布局会忽略它的任何数字)。其余的空间然后被按钮面板占用。

您可以了解更多有关边界布局在这里:http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

+0

这是一个很好的计划(即除去每个组件所需的首选宽度/高度后) - 我本来是没有一个JPannel为底部的所有按钮,但正如我现在所做的那样,这将起作用。如果我将首选宽度设置为getWidth()/ 5,它将实现正确的比例。 – csga5000 2013-02-21 13:03:34

+0

@ csga5000在实际显示组件之前,getWidth()将为0。 – 2013-02-21 13:19:17

+0

@JakubZaverka元件尺寸不依赖于它的可见性,但这样的事实:一个部件已被其LayoutManager的布局或不(例如,如果包()被调用的方含框架上) – 2013-02-21 15:04:21