B
B
bublov2017-10-15 17:21:30
Java
bublov, 2017-10-15 17:21:30

Why is JLable not showing up?

I decided to make a chessboard with numbers and Latin letters. I made a board, but the label with numbers is not shown despite the fact that I created it, and also registered setText and setVisible. Point out my mistake.

package chess123;    
import javax.swing.*;

import java.awt.Color;
public class board{
    public static void main (String [] args) {
        for (int j = 1; j<=9; j++) {
        if (j!=9) {
        for (int i = 1; i<=8; i++) {
            JFrame nf = new JFrame();
            nf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            nf.setBounds(i*100, j*100, 100, 100);
            nf.setUndecorated(true);
            nf.setVisible(true);

            if(i % 2 == 0 & j % 2 != 0) {
                nf.getContentPane().setBackground(Color.BLACK);
            }
            else if(i % 2 != 0 & j % 2 == 0) {
                nf.getContentPane().setBackground(Color.BLACK);
            }
            else if (i % 2 == 0 & j % 2 == 0) {
                nf.getContentPane().setBackground(Color.WHITE);
            }
            else if (i % 2 != 0 & j % 2 != 0) {
                nf.getContentPane().setBackground(Color.WHITE);
            }
            if (j == 9) {
                for (int k = 1; k<=8; k++) {
                String l = String.valueOf(k);
                JLabel count = new JLabel();
                count.setText(l);
                count.setVisible(true);
                }       
            }
        }

    }
}
}
}

PS I know that the numbers should be on the side, but I would like to understand the JLabel structure first.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Oparin, 2017-10-15
@losse_narmo

It remains to add a label to the frame

E
Evgeny Kornachev, 2017-10-16
@zelan

First: setVisible(true) for the frame should be called at the very end, when all the components have already been added. Otherwise, the components may simply not be rendered.
Secondly: JFrame component is the main window of the application, it is better to have one.
Thirdly: if you want to add components specifying their coordinates and size (by setting setBounds), then you need to set an absolute layout (layout manager) in the parent component. To do this, you can simply pass null to the container constructor (JFrame nf = new JFrame(null); JPanel nf = new JPanel (null); etc.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question