How to set the location of Jbutton according to the size of a Panel

Asked

Viewed 2,043 times

2

I’d like to define the location of color1 according to the size of a Jpanel, the same way I did with the fundo

public static void janelaPrincipal()
{
    //FRAME
    JFrame janela = new JFrame();
    janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    janela.pack();
    janela.setResizable(false);
    janela.setVisible(true);
    janela.setSize(new Dimension(WIDTH, HEIGHT));   
    janela.setLocation((SCREEN.width / 2) - (WIDTH / 2), (SCREEN.height / 2) - (HEIGHT / 2));

    Container c = janela.getContentPane();
    c.setLayout(new FlowLayout());

    //PAINEIS
    JPanel fundo = new JPanel();

    fundo.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    fundo.setBackground(background);

    //BOTOES
    JButton color1 = new JButton();
    JButton color2 = new JButton();

    color1.setPreferredSize(new Dimension(WIDTHBUTTON, HEIGHTBUTTON));
    color1.setLocation(HEIGHT / 2, WIDTH /2);
    color1.setBackground(RED);

    //ADICIONAR NO FRAME
    c.add(fundo);
    fundo.add(color1);

}

  • You want to keep your Jbutton always in the center of your Jpanel and you’re not getting it, that’s it?

  • No, the center was just an example, I want to position by "pixels" the same way I positioned Jframe on the screen

1 answer

2


The main question should be: Why do you want to position your components absolutely?

Java interfaces have the possibility to run on a wide number of platforms, with different screen resolutions and using different Plafs. For these reasons, the absolute positioning of components is not the best practice. To build a robust GUI that looks the same regardless of the settings of the device you’re running on, I would suggest using a layout manager, or several, together with padding () and margins for white spaces in order to organise the components.

If you still want to manually control the positioning of the button just remove the Layout of the component where you placed the button through the method setLayout(null). After removing the layout you can use the same logic used for Jpanel.

Here is a very simple example of how you can place the button in a specific window position:

import java.awt.*;
import javax.swing.*;

public class MainForm {

    protected JFrame  janela = new JFrame("Teste");
    protected JButton color1 = new JButton("Exemplo");

    public static void main(String st[]) {
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                MainForm mf = new MainForm();
                mf.load();
            }
        });

    }
    public void load() {

    janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        janela.setSize(new Dimension(500, 500));   
        janela.setResizable(false);
    janela.setVisible(true);

    Container c = janela.getContentPane();
    c.setLayout(null); //vamos fazer a gestão manualmente
    color1.setBounds(250, 50, 150, 50); //dimensoes e posicionamento do botão (os dois primeiros valores indicam a posicao absoluta do botao)
    c.add(color1);
    }

}

I also suggest reading https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

Browser other questions tagged

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