gpt4 book ai didi

java - GridBagLayout 中的 JTextArea 占用了太多空间

转载 作者:行者123 更新时间:2023-11-30 04:29:34 25 4
gpt4 key购买 nike

我有一个 GridBagLayout,由于某种原因,我的 JTextArea 决定它不想听 gridbag 的 with 比率,并且当创建包含布局的面板时,我的文本框会增长,直到占据 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?)

感谢您的帮助, 蔡斯

它在做什么 What it does它应该做什么的维度 enter image description here

最佳答案

如果您不固定在精确的 4/5 和 1/5 比例,您可以使用 BorderLayout 并将按钮放置在中心,将文本区域放置在东部。

边框布局将显示您提供的文本区域宽度 - 指定首选大小(宽度,您可以将高度设置为您想要的任何数字,边框布局将忽略它)。其余空间由按钮面板占据。

您可以在此处了解有关边框布局的更多信息:http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

关于java - GridBagLayout 中的 JTextArea 占用了太多空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14994939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com