2013-02-21 66 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