我想到的最簡單的解決方案是用於主面板的BorderLayout
。將textarea
添加到NORTH
/PAGE_START
。製作包含庫存按鈕(WEST
/LINE_START
)和位置標籤(EAST
/LINE_END
)的另一個BorderLayout
。加上SOUTH
/PAGE_END
的主BorderLayout
。然後只需添加一個BoxLayout
垂直對齊主BorderLayout
的CENTER
包含兩個按鈕。 Here's a tutorial爲標準佈局管理器。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Example {
public Example() {
JTextArea textArea = new JTextArea("There is a locked door");
textArea.setRows(5);
textArea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
textArea.setEditable(false);
WhiteButton button1 = new WhiteButton("Go find a key") {
@Override
public Dimension getMinimumSize() {
return new Dimension(200, 25);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 25);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(200, 25);
}
};
WhiteButton button2 = new WhiteButton("Attempt to force the door open");
button2.setMargin(new Insets(0, 60, 0, 60));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.add(button1);
buttonPanel.add(Box.createVerticalStrut(5));
buttonPanel.add(button2);
WhiteButton inventoryButton = new WhiteButton(
new ImageIcon(new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB)));
JLabel locationLabel = new JLabel("Location: 0");
locationLabel.setVerticalAlignment(JLabel.BOTTOM);
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(inventoryButton, BorderLayout.WEST);
southPanel.add(locationLabel, BorderLayout.EAST);
JPanel mainPanel = new JPanel(new BorderLayout(0, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.add(textArea, BorderLayout.NORTH);
mainPanel.add(buttonPanel);
mainPanel.add(southPanel, BorderLayout.SOUTH);
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
private class WhiteButton extends JButton {
public WhiteButton() {
setBackground(Color.WHITE);
}
public WhiteButton(String text) {
this();
setText(text);
}
public WhiteButton(ImageIcon icon) {
this();
setIcon(icon);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
}
}
哪個組件的首選大小應該得到尊重?我想到的最簡單的解決方案是主面板的「BorderLayout」。將textarea添加到'NORTH'。製作另一個包含庫存按鈕('WEST')和位置標籤('EAST')的'BorderLayout'。將其添加到主「BorderLayout」的「SOUTH」中。然後,將一個「BoxLayout」垂直對齊到包含兩個文本框的主BorderLayout的「CENTER」中。標準版面管理器的[總結](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)。 –
關於唯一能夠完成UI的佈局是'GridBagLayout'和'GroupLayout'。後者通常旨在供IDE中的GUI生成器使用。除了這兩個,我通常會將佈局結合起來,爲UI的每個部分使用最佳佈局。對於底部部分,我將使用帶有「BorderLayout」的單個面板,並將該圖標添加到「LINE_START」,並將標籤添加到「LINE_END」。要獲得5px縮進,請使用底部面板上的'EmptyBorder'。 –
不要指望它,不要把你的雞蛋放在一個籃子 - 咆哮的狗會在這裏吠叫,但在幾年後,你會回來「哦,我的Windows 11不運行這個東西!」 – gpasch