如何在此Java应用程序的底部添加页脚(带有文本)? [英] How do I add a footer(with text) to the bottom of this Java application?

查看:51
本文介绍了如何在此Java应用程序的底部添加页脚(带有文本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是Java井字游戏的链接.链接中包含GUI版本的所有代码. http://pervasive2.morselli.unimo.it/~nicola/courses/IngegneriaDelSoftware/java/JavaGame_TicTacToe.html

Here is a link to a tic-tac-toe game made in Java. All of the code for the GUI version is included in the link. http://pervasive2.morselli.unimo.it/~nicola/courses/IngegneriaDelSoftware/java/JavaGame_TicTacToe.html

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

I'm having trouble with adding text relative to the position of other elements. I have tried creating another container but it's not working. I would like to place a footer of text either below the "statusBar" or above it.

任何帮助将不胜感激.

推荐答案

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

Here is the relevant code to your problem (next time you should add relevant code to your post instead of just put a link to some other site):

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"或其上方.

I'm having trouble with adding text relative to the position of other elements. I have tried creating another container but it's not working. I would like to place a footer of text either below the "statusBar" or above it.

不知道您尝试了什么,但是可以遵循嵌套布局方法,然后包装两个(或多个)需要)组件放入一个面板中,并将其添加到内容窗格的南边位置.像这样:

Don't know what have you tried but you can follow a Nested Layout approach and wrap two (or many as needed) components into a panel and add this one at content pane's south position. Something like this:

    ...
    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 ,但是可能会根据您的需求找到更合适的布局管理器.

Note: the example makes use of GridBagLayout but might find a more suitable layout manager based on your needs.

看看课程:布置容器内的组件

这篇关于如何在此Java应用程序的底部添加页脚(带有文本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆