A
A
Artur Martynov2015-12-01 21:02:37
Java
Artur Martynov, 2015-12-01 21:02:37

How to arrange elements in java?

I want to create a calculator in java, but I don't know what method to use to position the components.022fb3730c574658aac3501a9f279741.jpg

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

public class Frame extends JFrame {
  JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bplus,bminus,bmultiplication,
  bsegmentation,bclean,breset,bequils,bcomma;
  JTextField result;
  
  
  public Frame (String s){
    super(s);
    setLayout(new GridLayout(6,4,5,10));
    b1 = new JButton("1");
    b2 = new JButton("2");
    b3 = new JButton("3");
    b4 = new JButton("4");
    b5 = new JButton("5");
    b6 = new JButton("6");
    b7 = new JButton("7");
    b8 = new JButton("8");
    b9 = new JButton("9");
    b0 = new JButton("0");
    bplus = new JButton("+");
    bminus = new JButton("-");
    bmultiplication = new JButton("*");
    bclean = new JButton("CE");
    breset = new JButton("C");
    bequils = new JButton("=");
    bcomma = new JButton(",");
    bsegmentation = new JButton("/");
    result = new JTextField(15);
    add(result);
    add(bplus);
    add(bminus);
    add(bsegmentation);
    add(bmultiplication);
    add(b1);
    add(b2);
    add(b3);
    add(b4);
    add(b5);
    add(b6);
    add(b7);
    add(b8);
    add(b9);
    add(b0);
    add(bcomma);
    add(bclean);
    add(breset);
    add(bequils);
    
  }

}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nirvimel, 2015-12-01
@nirvimel

GridBagLayout ( demo )

S
Sanan Yuzb, 2015-12-01
@Sanan07

MethodsetBounds

E
Evgeny Kornachev, 2015-12-02
@zelan

MigLayout is a good layout manager, there is a big yellow button on the page with a description of how to use it.
The GridLayout you are currently using creates a grid with cells of the same size. When you place an element in a cell, it expands to the full size of the cell.
If the size is not important, and you are not going to stretch the window, then you can turn off the layout and expand the elements yourself by setting the sizes and coordinates.
For example:

public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //реальное закрытие программы, при закрытии окна(нажатии на красный крестик) 
        frame.setSize(400, 400);


        //панель для расположения элементов , в конструкторе передается менеджер раскладки, null - AbsoluteLayout ( для добавления компонентов необходимо указывать координаты и размер)
        JPanel panel = new JPanel(null);

        //установка панели во фрейм в центральную часть (панель будет растягиваться на весь фрейм)
        frame.add(panel, BorderLayout.CENTER);
        //текстовое поле
        JTextField field = new JTextField("Text");

        //установка координаты и размеров (x, y, ширина, высота) для компонента (рабо
        field.setBounds(20,20,200,20);

        //добавление поля на панель
        panel.add(field);


        frame.setVisible(true);
    }

Well, by analogy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question