这里是你的问题的相关代码(下次你应该在你的帖子中添加相关代码,而不是仅仅放一个指向其他网站的链接):
public class TTTGraphics2P extends JFrame {
...
private DrawCanvas canvas; // Drawing canvas (JPanel) for the game board
private JLabel statusBar; // Status Bar
public TTTGraphics2P() {
...
statusBar = new JLabel(" ");
statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 15));
statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, 4, 5));
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(statusBar, BorderLayout.PAGE_END); // same as SOUTH
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); // pack all the components in this JFrame
setTitle("Tic Tac Toe");
setVisible(true); // show this JFrame
}
...
}
我在添加相对于其他位置的文本时遇到问题
元素。我已经尝试创建另一个容器,但它不是
在职的。我想在下面放置一个文本页脚
"statusBar" 或以上。
不知道您尝试过什么,但您可以按照Nested Layout 方法将两个(或多个根据需要)组件包装到一个面板中,然后将这个组件添加到内容窗格的南侧位置。像这样的:
...
statusBar = new JLabel(" ");
JLabel someOtherLabel = new JLabel("Some other label!");
JPanel southPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.weightx = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(8,8,8,8);
southPanel.add(statusBar, constraints);
constraints.gridy = 1;
southPanel.add(someOtherLabel, constraints);
Container cp = getContentPane();
cp.setLayout(new BorderLayout()); // default layout manager is actually BorderLayout
cp.add(canvas, BorderLayout.CENTER);
cp.add(southPanel, BorderLayout.SOUTH);
...
注意:该示例使用了GridBagLayout,但可能会根据您的需要找到更合适的布局管理器。
推荐阅读
看看Lesson: Laying Out Components Within a Container