问题描述
好吧,就像标题中所说的那样,这应该是一个简单的问题,但是我对使用gui还是很陌生,我有一个小问题.
我正在尝试将窗口分成3个带边框的部分(水平).到目前为止,我有两个,但是中间的一个向下延伸到窗口的底部,阻塞了底部.我猜这与我使用NORTH,CENTER和SOUTH有关吗?我附上了窗口的图片和一些代码,如果您需要更多,请告诉我!
Alright, like the title says, this should be an easy question but I'm very new to doing gui and I'm having a little problem.
I'm trying to separate my window into 3 bordered sections (horizontal). So far I've got two, however, the one in the middle extends down to the bottom of the window, blocking the bottom section. I'm guessing it has something to do with my use of NORTH, CENTER, and SOUTH? I attached a picture of the window and some of my code, let me know if you need more!
顶部
public NamePanel(){
Dimension size = getPreferredSize();
size.height = 125;
setPreferredSize(size);
//setBorder(BorderFactory.createTitledBorder("Who owes who money?"));
setBorder(BorderFactory.createTitledBorder("Who?"));
JRadioButton button;
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
button = new JRadioButton("Bob");
gc.anchor = GridBagConstraints.CENTER;
gc.weightx = 0.5;
gc.weighty = 0.0;
gc.gridx = 1;
gc.gridy = 0;
add(button, gc);
button = new JRadioButton("Sue");
gc.weighty = 0.5;
gc.gridx = 3;
gc.gridy = 0;
add(button, gc);
中间部分
public ActionsPanel(){
Dimension size = getPreferredSize();
size.height = 75;
setPreferredSize(size);
setBorder(BorderFactory.createTitledBorder("What would you like to do?"));
JRadioButton button;
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
button = new JRadioButton("Add");
gc.anchor = GridBagConstraints.NORTH;
gc.weightx = 0.5;
gc.weighty = 0.0;
gc.gridx = 1;
gc.gridy = 0;
add(button, gc);
底部(隐藏在图片中)
public EntryPanel(){
Dimension size = getPreferredSize();
size.height = 75;
setPreferredSize(size);
setBorder(BorderFactory.createTitledBorder("Enter the transaction"));
JLabel why = new JLabel("Why?: ");
JTextField transName = new JTextField(10);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.anchor = GridBagConstraints.SOUTH;
add(transName, gc);
推荐答案
这几乎是BorderLayout
的工作方式. BorderLayout
有五个可以显示组件的位置,但是每个位置只能占据一个组件.请参阅如何使用BorderLayout 了解更多详细信息
This is pretty much how BorderLayout
works. BorderLayout
has five positions in which components can be displayed, but only one component can occupy each position. Have a look at How to Use BorderLayout for more details
根据需要,可以使用GridBagLayout
,类似...
Depending on what you want, you could use GridBagLayout
, something like...
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
add(namePanel, gbc);
add(actionsPanel, gbc);
add(entryPanel, gbc);
这将在容器内垂直放置三个组件,但仅在此处优先选择高度,因此不会扩展以垂直填充整个容器
Which will layout the three components vertically within the container, but only honor there preferred heights, so the won't expand to fill the entire container, vertically
看看在容器内布置组件和如何使用GridBagLayout 了解更多详情
别忘了,您可以使用多种布局(通过使用多个容器)来生成所需的结果
Don't forget, you can use multiple layouts (through the use of multiple containers) to generate your desired results
您应该避免使用setPreferredSize
,只需要让容器和布局管理器来处理它即可.请参见应避免使用set(首选| Maximum | Minimum)Java Swing中的Size方法?以获取更多详细信息.
You should avoid using setPreferredSize
, just let the container and layout manager take care of it. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details.
您还应该尝试在JFrame
上使用pack
,以允许窗口将其自身包装在内容周围
You should also try using pack
on JFrame
to allow the window to pack itself around the contents
这篇关于尝试自学Java GUI(Swing),应该很容易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!