How to centralize a component using null layout?

1

My button I want to center is 100 pixels wide. the Jframe is 350 wide. how to centralize?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ComboBoxLUL;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Igor
 */
public class PainelPrincipal extends JPanel {

    private JComboBox jcbGenero;

    private JComboBox jcbCasado;

    private JButton btnSalva;

    private JLabel lblNome;

    private JLabel lblIdade;

    private JLabel lblCpf;

    private JTextField txtCpf;

    private JTextField txtNome;

    private JTextField txtIdade;

    private final String[] genero = {"Masculino ", "Feminino"};

    private final String[] estCivil = {"Casado", "Solteiro"};

    // private Integer[] genero = {1,2,3};// essa forma funciona. int nao. mas o resultado é o msm.

    PainelPrincipal() {
        this.setLayout(null);

        txtNome = new JTextField();
        txtNome.setBounds(45, 90, 120, 25);
        this.add(txtNome);
        ///////////
        txtCpf = new JTextField();
        txtCpf.setBounds(45, 50, 120, 25);
        this.add(txtCpf);
        ///////////
        txtIdade = new JTextField();
        txtIdade.setBounds(45, 130, 120, 25);
        this.add(txtIdade);

        //////////
        lblCpf = new JLabel("CPF");
        lblCpf.setBounds(10, 50, 120, 25);
        this.add(lblCpf);
        //////////

        lblIdade = new JLabel("Idade");
        lblIdade.setBounds(10,90,120,25);
        this.add(lblIdade);

        lblNome = new JLabel("Nome");
        lblNome.setBounds(10, 130, 120, 25);
        this.add(lblNome);

        jcbGenero = new JComboBox(genero);
        jcbGenero.setBounds(170, 50, 120, 25);
        this.add(jcbGenero);

        jcbCasado = new JComboBox(estCivil);
        jcbCasado.setBounds(170, 90, 120, 25);
        this.add(jcbCasado);

        btnSalva = new JButton("Salvar");
        btnSalva.setBounds(175, 175, 100, 25);
        this.add(btnSalva);

        btnSalva.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
              //@@@@@@@@@@@@@@@@@@@@@@
            }

        });
        this.add(btnSalva);

    }

}
    
asked by anonymous 01.03.2017 / 03:24

1 answer

3

Just use a bit of math: if the screen is 350px full width and the component has 100, for it to occupy a center portion, the horizontal position of it on the screen is:

  

(DateData / 2) - (ComponentDirst / 2)

Applying this calculation to the reported data results:

  

350/2 - 100/2 = 125

This 125 is the horizontal position from where your button should be positioned in setBounds . See:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.*;

/**
 *
 * @author Igor
 */
public class PainelPrincipal extends JPanel {

    private JComboBox jcbGenero;

    private JComboBox jcbCasado;

    private JButton btnSalva;

    private JLabel lblNome;

    private JLabel lblIdade;

    private JLabel lblCpf;

    private JTextField txtCpf;

    private JTextField txtNome;

    private JTextField txtIdade;

    private final String[] genero = {"Masculino ", "Feminino"};

    private final String[] estCivil = {"Casado", "Solteiro"};

    // private Integer[] genero = {1,2,3};// essa forma funciona. int nao. mas o resultado é o msm.
    PainelPrincipal() {
        this.setLayout(null);

        txtNome = new JTextField();
        txtNome.setBounds(45, 90, 120, 25);
        this.add(txtNome);
        ///////////
        txtCpf = new JTextField();
        txtCpf.setBounds(45, 50, 120, 25);
        this.add(txtCpf);
        ///////////
        txtIdade = new JTextField();
        txtIdade.setBounds(45, 130, 120, 25);
        this.add(txtIdade);

        //////////
        lblCpf = new JLabel("CPF");
        lblCpf.setBounds(10, 50, 120, 25);
        this.add(lblCpf);
        //////////

        lblIdade = new JLabel("Idade");
        lblIdade.setBounds(10, 90, 120, 25);
        this.add(lblIdade);

        lblNome = new JLabel("Nome");
        lblNome.setBounds(10, 130, 120, 25);
        this.add(lblNome);

        jcbGenero = new JComboBox(genero);
        jcbGenero.setBounds(170, 50, 120, 25);
        this.add(jcbGenero);

        jcbCasado = new JComboBox(estCivil);
        jcbCasado.setBounds(170, 90, 120, 25);
        this.add(jcbCasado);

        btnSalva = new JButton("Salvar");
        // btnSalva.setPreferredSize(new Dimension(100, 25));
        // JPanel btnPan = new JPanel(new FlowLayout(FlowLayout.CENTER));
        // btnPan.add(btnSalva);
        // this.add(btnPan);
        btnSalva.setBounds(125, 175, 100, 25);
        this.add(btnSalva);

        btnSalva.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                //@@@@@@@@@@@@@@@@@@@@@@
            }

        });
        this.add(btnSalva);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.getContentPane().add(new PainelPrincipal(), BorderLayout.CENTER);
                f.setPreferredSize(new Dimension(350, 350));
                f.pack();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }

}

But as a suggestion, always avoid using absolute layout . It hinders the maintenance of the interface, because any future changes you will have to leave repositioning all components manually, imagine a screen with dozens of these? Not to mention that, depending on the resolution and size of the monitor, your interface may differ from what you have programmed. Read about LayoutManager , there are several native options and a lot of options good of the community, and the best is that you can merge them all.

Here are some links to LayoutManager:

01.03.2017 / 14:56