尝试自学 Java GUI (Swing),应该很容易解决
Trying to teach myself Java GUI (Swing), should be an easy fix
好的,正如标题所说,这应该是一个简单的问题,但我是做 gui 的新手,遇到了一些小问题。
我试图将我的 window 分成 3 个带边框的部分(水平)。到目前为止,我有两个,但是,中间的一个向下延伸到 window 的底部,挡住了底部。我猜这与我对 NORTH、CENTER 和 SOUTH 的使用有关?我附上了 window 的图片和我的一些代码,如果您需要更多,请告诉我!
顶部
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
有五个位置可以显示组件,但每个位置只能有一个组件。查看 How to Use BorderLayout 了解更多详情
根据您的需要,您可以使用 GridBagLayout
,例如...
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);
这将在容器内垂直布置三个组件,但只考虑首选高度,因此不会垂直扩展以填满整个容器
查看 Laying Out Components Within a Container and How to Use GridBagLayout 了解更多详情
别忘了,您可以使用多个布局(通过使用多个容器)来生成您想要的结果
你应该避免使用 setPreferredSize
,让容器和布局管理器来处理它。有关详细信息,请参阅 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?。
您还应该尝试在 JFrame
上使用 pack
以允许 window 围绕内容
好的,正如标题所说,这应该是一个简单的问题,但我是做 gui 的新手,遇到了一些小问题。
我试图将我的 window 分成 3 个带边框的部分(水平)。到目前为止,我有两个,但是,中间的一个向下延伸到 window 的底部,挡住了底部。我猜这与我对 NORTH、CENTER 和 SOUTH 的使用有关?我附上了 window 的图片和我的一些代码,如果您需要更多,请告诉我!
顶部
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
有五个位置可以显示组件,但每个位置只能有一个组件。查看 How to Use BorderLayout 了解更多详情
根据您的需要,您可以使用 GridBagLayout
,例如...
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);
这将在容器内垂直布置三个组件,但只考虑首选高度,因此不会垂直扩展以填满整个容器
查看 Laying Out Components Within a Container and How to Use GridBagLayout 了解更多详情
别忘了,您可以使用多个布局(通过使用多个容器)来生成您想要的结果
你应该避免使用 setPreferredSize
,让容器和布局管理器来处理它。有关详细信息,请参阅 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?。
您还应该尝试在 JFrame
上使用 pack
以允许 window 围绕内容