我有一個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