Alinhamento de botões em uma janela

10 respostas
guisantogui

galera to implementando uma calculadora, para mim testar meus conhecimentos com interface gráfica.

mas os meus botões estão espalhados pela janela e naum tenho a menor ideia de como organiza-los.

alguem poderia me ajudar o cod vai abaixo.

Classe Janela

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



public class Janela extends JFrame{
    private JLabel total, n1 ,n2;
    private JButton zero, um, dois, tres, quatro, cinco, seis, sete, oito, nove;
    private JButton somar, dividir, subtrair, multiplicar;
    private JTextField x, y;


    public Janela(){

        super("Calculadora");

        JPanel painel = new JPanel();
        
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBounds(100,150,420,500);

        painel.setLayout(new GridLayout(10,5));
        this.getContentPane().add(painel);

        JPanel painelN1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelN1);

        n1 = new JLabel("Insira o 1° Numero");
        painelN1.add(n1);

        JPanel painelX = new JPanel();
        painel.add(painelX);

        x = new JTextField(5);
        painelX.add(x);

        JPanel painelN2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelN2);

        n2 = new JLabel("Insira o 2° Numero");
        painelN2.add(n2);

        JPanel painelY = new JPanel();
        painel.add(painelY);

        y = new JTextField(5);
        painelY.add(y);
        

        JPanel painelSoma = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelSoma);

        somar = new JButton("+");
        painelSoma.add(somar);


        JPanel painelSubtrai = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelSoma);

        subtrair = new JButton("-");
        painelSoma.add(subtrair);

        JPanel painelMultiplica = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelMultiplica);

        multiplicar = new JButton("*");
        painelMultiplica.add(multiplicar);


        JPanel painelDivide = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelDivide);

        dividir = new JButton("/");
        painelDivide.add(dividir);


        JPanel painel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel1);

        um = new JButton("1");
        painelSoma.add(um);


        JPanel painel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel2);

        dois = new JButton("2");
        painelSoma.add(dois);


        JPanel painel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel3);

        tres = new JButton("3");
        painelMultiplica.add(tres);


        JPanel painel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel4);

        quatro = new JButton("4");
        painelDivide.add(quatro);


        JPanel painel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel5);

        cinco = new JButton("5");
        painelSoma.add(cinco);


        JPanel painel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel6);

        seis = new JButton("6");
        painelSoma.add(seis);


        JPanel painel7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel7);

        sete = new JButton("7");
        painelMultiplica.add(sete);


        JPanel painel8 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel8);

        oito = new JButton("8");
        painelDivide.add(oito);


        JPanel painel9 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel9);

        nove = new JButton("9");
        painelDivide.add(nove);

    }
}

PS.: jah tentei todas as combinações possiveis no gridLayout :?

10 Respostas

AlissonGuj

vc estah criando aonde essa calculadora?

guisantogui

Como assim?

no NetBeans ou Eclipse? NetBeans

eu fiz um novo projeto!
eh isso?

AlissonGuj

ok

vc esta criando ela no netbeans e não sabe como organizar o seus botões??

tipo a forma que eles podem ser organizados?

guisantogui

Naum eu qria eh colocar eles tipo um perto do outro e organizar numa ordem determinada por mim!

sabe como fazer isso?

guisantogui

pq to usando o GridLayout e o FlowLayout dae ele ficam mto longe um do outro.

guisantogui

Atualizei a classe olhe como ficou.

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



public class Janela extends JFrame{
    private JLabel total, n1 ,n2, resultado;
    private JButton zero, um, dois, tres, quatro, cinco, seis, sete, oito, nove;
    private JButton somar, dividir, subtrair, multiplicar;
    private JTextField x, y;


    public Janela(){

        super("Calculadora");

        JPanel painel = new JPanel();
        
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBounds(100,150,420,500);

        painel.setLayout(new GridLayout(5,5,5,5));
        this.getContentPane().add(painel);

        JPanel painelN1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelN1);

        n1 = new JLabel("Insira o 1° Numero");
        painelN1.add(n1);

        JPanel painelX = new JPanel();
        painel.add(painelX);

        x = new JTextField(8);
        painelX.add(x);

        JPanel painelN2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelN2);

        n2 = new JLabel("Insira o 2° Numero");
        painelN2.add(n2);

        JPanel painelY = new JPanel();
        painel.add(painelY);

        y = new JTextField(8);
        painelY.add(y);
        

        JPanel painelSoma = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelSoma);

        somar = new JButton("+");
        painelSoma.add(somar);


        JPanel painelSubtrai = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelSubtrai);

        subtrair = new JButton("-");
        painelSubtrai.add(subtrair);

        JPanel painelMultiplica = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelMultiplica);

        multiplicar = new JButton("*");
        painelMultiplica.add(multiplicar);


        JPanel painelDivide = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelDivide);

        dividir = new JButton("/");
        painelDivide.add(dividir);


        JPanel painel0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel0);

        zero = new JButton("0");
        painel0.add(zero);


        JPanel painel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel1);

        um = new JButton("1");
        painel1.add(um);


        JPanel painel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel2);

        dois = new JButton("2");
        painel2.add(dois);


        JPanel painel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel3);

        tres = new JButton("3");
        painel3.add(tres);


        JPanel painel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel4);

        quatro = new JButton("4");
        painel4.add(quatro);


        JPanel painel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel5);

        cinco = new JButton("5");
        painel5.add(cinco);


        JPanel painel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel6);

        seis = new JButton("6");
        painel6.add(seis);


        JPanel painel7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel7);

        sete = new JButton("7");
        painel7.add(sete);


        JPanel painel8 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel8);

        oito = new JButton("8");
        painel8.add(oito);


        JPanel painel9 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painel9);

        nove = new JButton("9");
        painel9.add(nove);

        JPanel painelTot = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelTot);

        total = new JLabel("TOTAL");
        painelTot.add(total);

        JPanel painelResult = new JPanel(new FlowLayout(FlowLayout.LEFT));
        painel.add(painelResult);

        resultado = new JLabel("");
        painelTot.add(resultado);


    }
        private class Acoes implements ActionListener{

            public void actionPerformed(ActionEvent e) {

            }


        }

}
guisantogui

depois eu vou colocar eventos ainda :slight_smile:

AlissonGuj

Eu ja fiz varias calculadoras e tipo a distancia do FlowLayout eh boa para uma calculadora

na verdade isso serve mais como uma dica do que como uma resposta

flw

abrass

:slight_smile:

guisantogui

Alguem mais?

ViniGodoy

Assim não serve?
http://www.guj.com.br/posts/list/140986.java

Criado 17 de outubro de 2009
Ultima resposta 17 de out. de 2009
Respostas 10
Participantes 3