【问题标题】:How do I add a footer(with text) to the bottom of this Java application?如何在此 Java 应用程序的底部添加页脚(带文本)?
【发布时间】:2014-03-05 10:19:02
【问题描述】:

这是一个用 Java 制作的井字游戏的链接。 GUI 版本的所有代码都包含在链接中。 http://pervasive2.morselli.unimo.it/~nicola/courses/IngegneriaDelSoftware/java/JavaGame_TicTacToe.html

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

任何帮助将不胜感激。

【问题讨论】:

  • 您是否使用 GUI 来进行游戏或控制台显示?以及哪个状态栏?
  • GUI 版本。有一个名为 statusBar 的 JLabel - 它出现在 Applet 的底部。
  • 我无法访问你的游戏:Java 阻止了它。

标签: java swing layout


【解决方案1】:

这里是你的问题的相关代码(下次你应该在你的帖子中添加相关代码,而不是仅仅放一个指向其他网站的链接):

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

【讨论】:

    【解决方案2】:

    以编程方式将其变为相对布局,然后使用布局参数p.addRule(RelativeLayout.ALIGN_BOTTOM, view.getId());....

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 2023-03-24
      相关资源
      最近更新 更多