Create graphical interface using Miglayout

Asked

Viewed 1,150 times

2

I need to create this graphical interface:

inserir a descrição da imagem aqui

The last code I tried was this:
(but the settings are not correct)

frame.setLayout(new MigLayout("wrap","[grow]","[grow][]"));
 panelInfo.setLayout(new MigLayout(""));
 panelInfo.add(new JLabel("tessssssstteeetsssssssss"), "grow, pushx, pushy, wrap");

 //panelMenu.setLayout(new MigLayout("fill","[] [] []"));
 panelMenu.setLayout(new MigLayout(""));
 panelMenu.add(anterior,"align center");
 panelMenu.add(proximo,"align center");
 panelMenu.add(backMenu,"align right");

 frame.getContentPane().add(panelInfo);
 frame.getContentPane().add(panelMenu);
  • Do you have to use Miglayout? To fix components in the footer I’m much more using Borderlayout and putting them in the South panel.

1 answer

4


I would make a composition of Layouts, to fix the components in the footer would use Borderlayout and put a panel at PAGE_END.

inserir a descrição da imagem aqui

The panel PAGE_END would also be of the Borderlayout type, to fix the Exit button on the right (LINE_END), and would have another panel to place the two navigation buttons, these in the CENTER position. For the panels of the buttons, I would use Miglayout to make them responsive.

The structure would look like this:

inserir a descrição da imagem aqui

The end result would be as follows:

inserir a descrição da imagem aqui

Code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import net.miginfocom.swing.MigLayout;
import java.awt.Color;
import java.awt.Dimension;

public class ComposicaoDeLayouts extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ComposicaoDeLayouts frame = new ComposicaoDeLayouts();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ComposicaoDeLayouts() {
        setMinimumSize(new Dimension(400, 300));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JPanel pnlCenter = new JPanel();
        pnlCenter.setBackground(Color.WHITE);
        contentPane.add(pnlCenter, BorderLayout.CENTER);

        JPanel pnlSouth = new JPanel();
        contentPane.add(pnlSouth, BorderLayout.SOUTH);
        pnlSouth.setLayout(new BorderLayout(0, 0));

        JPanel pnlBotoesNavegacao = new JPanel();
        pnlSouth.add(pnlBotoesNavegacao, BorderLayout.CENTER);
        pnlBotoesNavegacao.setLayout(new MigLayout("", "[1,grow 2][100px][100px][1,grow 1]", "[40px]"));

        JButton btnAnterior = new JButton("<< Anterior");
        pnlBotoesNavegacao.add(btnAnterior, "cell 1 0,grow");

        JButton btnProximo = new JButton("Proximo >>");
        pnlBotoesNavegacao.add(btnProximo, "cell 2 0,grow");

        JPanel pnlBotaoSair = new JPanel();
        pnlSouth.add(pnlBotaoSair, BorderLayout.EAST);
        pnlBotaoSair.setLayout(new MigLayout("", "[100px]", "[40px]"));

        JButton btnSair = new JButton("Sair");
        pnlBotaoSair.add(btnSair, "cell 0 0,grow");
    }
}
  • Thanks @Math hadn’t thought about the composition of Layouts, I haven’t tried yet but this is what I wanted :) regarding the use of MigLayout was more to learn to use it...

  • 1

    @jsantos1991 Miglayout is very flexible and really worth learning, but I don’t usually do anything at hand, I use the Windowbuilder plugin in Eclipse to create the screens. And although he is one of the most complete of the Layout Managers, it is almost always necessary to make a composition of Layout to get the desired result.

Browser other questions tagged

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