Adding Components to a Jframe

Asked

Viewed 21 times

0

Hello, I’m trying to build a simple interface to implement a minefield. The end result should be something like a grid of buttons, very simple. The problem I’m having is that when I create a class instance, only one button appears as in the following image:resultado da execução. follows the code I’m writing.

package ms;

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameBasico extends JFrame {
    JPanel painelMinas = new JPanel();
    int x = 9; int y = 9;
    MyJToggleButton[][] botoes = new MyJToggleButton[x][y];
    FrameBasico(){        
        this.setExtendedState(MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);        
        painelMinas.setSize(this.getSize());        
        painelMinas.setBackground(Color.red);                
        colocarBotoes();        
        this.add(painelMinas);                        
    }

    public void colocarBotoes(){        
        for(int ipslon = 0; ipslon < y; ipslon++){
            for(int xis = 0; xis < x; xis++){
                botoes[xis][ipslon] =  new MyJToggleButton();                                                                               
                painelMinas.add(botoes[xis][ipslon]);                                
                botoes[xis][ipslon].setVisible(true);                      
            }
        }        
        this.setVisible(true);        
    }

    public static void main(String args[]){
       FrameBasico f = new FrameBasico();
    }
}
  • Julian, your code is not executable, please provide a code that is [mcve] so that we can test the problem.

  • I imagine your problem is doing what I didn’t do at this link, if it is, confirm in the comments.

  • If it is similar, gridLayout is used in the link code. I wanted to try to avoid the use of layouts, so I tried to position the buttons with the setLocation()... As for the minimum example, Oce referred to having a main class?

  • You should not use absolute values, layouts exist precisely to facilitate this kind of problem that Oce is having. I recommend you see the other question, from a look at the layout la suggested, it meets and does what Voce intends.

  • really, seeing the other question I realize that solution is better. Can you recommend me al tuturial on layouts ? I still find it very difficult to understand how they work and how to use them

  • You have their documentation: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.