PTheCoolGuy
PTheCoolGuy

Reputation: 95

Custom Swing layout

I'm creating a game and to set up the frame the way I want it, I need to some how create a costume layout. I have tried creating multiple panels with different layouts such as Flow and Border and adding panels on the main panel but I still don't get the desired outcome. What I desire is shown in the attached picture. Please tell me if any code segments would help.
Questions: 1. Is it possible to get such layout? (I want it to be fixed.)enter image description here

  1. Under the "button" there is the JLabel you see in the picture. I want its text to start from left, go down the column, and start on the middle, and then to the right column. Is that possible?

Upvotes: 1

Views: 112

Answers (1)

nIcE cOw
nIcE cOw

Reputation: 24616

There is a slight variation, as to what I wrote in comments, since BoxLayout is somethingy, that I never use, since I am not good in it :-)

Though what I did to overcome, this shortcoming of mine, is to take 2 JPanels, one each for JTextField and JButton and the rest is the same, as shown in this example:

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

public class LayoutExample {    

    private static final int GAP = 5;
    private static final int TOTAL_LABELS = 18;

    private JTextField tField;
    private JButton button;
    private JLabel[] labels;

    private void displayGUI () {
        JFrame frame = new JFrame ( "Layout Example" );
        frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JPanel contentPane = getPanel ();
        contentPane.setLayout ( new BorderLayout ( GAP, GAP ) );

        JPanel headerPanel = getPanel ();
        headerPanel.setLayout ( new GridLayout ( 0, 1, GAP, GAP ) );
        JPanel textFieldPanel = getPanel ();
        tField = new JTextField ( 10 );
        textFieldPanel.add ( tField );
        JPanel buttonPanel = getPanel ();
        button = new JButton ( "Button" );
        buttonPanel.add ( button );     
        headerPanel.add ( textFieldPanel );
        headerPanel.add ( buttonPanel );
        contentPane.add (headerPanel, BorderLayout.PAGE_START );

        JPanel centerPanel = getPanel ();
        centerPanel.setLayout ( new GridLayout ( 0, 3, GAP, GAP ) );
        labels = new JLabel [ TOTAL_LABELS ];
        for ( int i = 0; i < labels.length; ++i ) {
            labels [ i ] = new JLabel ( String.valueOf ( i ), JLabel.CENTER );
            centerPanel.add ( labels [ i ] );
        }
        contentPane.add ( centerPanel, BorderLayout.CENTER );


        frame.setContentPane ( contentPane );
        frame.pack ();
        frame.setLocationByPlatform ( true );
        frame.setVisible ( true );
    }

    private JPanel getPanel () {
        JPanel panel = new JPanel ();
        panel.setOpaque ( true );
        panel.setBorder ( BorderFactory.createEmptyBorder ( GAP, GAP, GAP, GAP ) );

        return panel;
    }

    public static void main ( String[] args ) {
        Runnable runnable = new Runnable () {
            @Override
            public void run () {
                new LayoutExample ().displayGUI ();
            }
        };
        EventQueue.invokeLater ( runnable );
    }
}

OUTPUT:

http://i.imgur.com/FMcV9GS.png

Upvotes: 4

Related Questions