如何将页脚(带文本)添加到此Java应用程序的底部?

如何将页脚(带文本)添加到此Java应用程序的底部?,java,swing,layout,Java,Swing,Layout,这里是一个链接到一个用Java制作的tic-tac-toe游戏。GUI版本的所有代码都包含在链接中。 我在添加相对于其他元素位置的文本时遇到问题。我尝试创建另一个容器,但它不起作用。我想在“状态栏”下方或上方放置一个文本页脚 如果有任何帮助,我们将不胜感激。通过编程将其转换为relativelayout,然后使用布局参数p.addRule(relativelayout.ALIGN_BOTTOM,view.getId())…以下是与您的问题相关的代码(下次您应该将相关代码添加到您的帖子中,而不仅

这里是一个链接到一个用Java制作的tic-tac-toe游戏。GUI版本的所有代码都包含在链接中。

我在添加相对于其他元素位置的文本时遇到问题。我尝试创建另一个容器,但它不起作用。我想在“状态栏”下方或上方放置一个文本页脚


如果有任何帮助,我们将不胜感激。

通过编程将其转换为relativelayout,然后使用布局参数
p.addRule(relativelayout.ALIGN_BOTTOM,view.getId())

以下是与您的问题相关的代码(下次您应该将相关代码添加到您的帖子中,而不仅仅是添加到其他网站的链接):

我在添加相对于其他位置的文本时遇到问题 元素。我已经尝试创建另一个容器,但它不是 工作我想将文本的页脚放在 “状态栏”或其上方

不知道您尝试了什么,但您可以按照一种方法将两个(或根据需要多个)组件包装到一个面板中,并将此组件添加到内容窗格的南部位置。大概是这样的:

    ...
    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);
    ...
注意:该示例使用了,但可能会根据您的需要找到更合适的布局管理器

建议阅读
看看

您是在使用GUI进行游戏还是控制台显示?哪个状态栏?它是GUI版本。有一个名为statusBar的JLabel-它出现在小程序的底部。我无法访问你的游戏:Java正在阻止它。
    ...
    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);
    ...