0

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

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.

Any help would be appreciated.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
RandomPleb
  • 424
  • 1
  • 5
  • 20

2 Answers2

2

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
    }
    ...
}

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);
    ...

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

Suggested reading

Take a look to Lesson: Laying Out Components Within a Container

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
0

Make it programatically into a relativelayout and then with layout params p.addRule(RelativeLayout.ALIGN_BOTTOM, view.getId());....

Lucian Novac
  • 1,255
  • 12
  • 18