Hello,

I've no experience with layouts, and I want to learn it, especially how to place components in the right places. I'll give an example, and I was wondering if you could help me?

Main class:

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

public class Main {

    public static void main(String[]args) {

        final Frame frame = new Frame();
            frame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                }
        } );
    }
}

Frame class

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

public class Frame extends JFrame {
    private JTabbedPane tabs;
    private Administrator tab = new Administrator();

    public Frame() {
        super("TabbedPane");
        tabs = new JTabbedPane();

        getContentPane().add(tabs);
        tabs.addTab("Admin", tab);

        setSize(300,300);
        setVisible(true);
    }

}

Administrator class

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


public class Administrator extends JPanel {
    private JTextField usernameField;
    private JButton logInButton, cancelButton;
    private JPasswordField passwordField;
    private static final String text = "<html> <center><b><font size=+5>ADMIN</font></b> <br><b><font size=+5>CONSOLE</font></b></center> </html>";
    private JLabel textt;


    public Administrator() {

        usernameField = new JTextField(10);
        passwordField = new JPasswordField(10);
        logInButton = new JButton("Log in");
        cancelButton = new JButton("Cancel");

        textt = new JLabel();
        textt.setText(text);

        add(textt);
        add(new JLabel("Username: "));
        add(usernameField);
        add(new JLabel("Password: "));
        add(passwordField);
        add(logInButton);
        add(cancelButton);

        logInButton.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Log in successful");
                }
        });
        cancelButton.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
        });

    }


}

As you see, my window now looks like this:
http://i.imgur.com/8fGAQ.png

I want my layout to look something like this, more structured:
http://i.imgur.com/X0Jaa.png

I clearly dont know how to do that. Could anyone help me, show me some code in my example for example, or anything? I'd really appreciate that :D

Edited 4 Years Ago by Stjerne

here is 2 good links i found: http://docs.oracle.com/javase/tutorial/uiswing/layout/ and http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html I'm sure you know that a layout manger helps structire your components sizes and places them on certain places in the container etc, hwoever you may have to use more then 1 layout manger depending on how you want each component to be laid out. First experiment withs suns tutorials on layout managers and once you get the hang of it use that as the skeleton for you code above.

Here's another good link. My personal favourite is the Box Layout:

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.*;

public class Main extends JFrame {

    void init() {

        // we'll use a main panel and four row-panels

        JPanel basicPanel = new JPanel(); basicPanel.setLayout(
            new BoxLayout(basicPanel, BoxLayout.Y_AXIS));

        JPanel titleRow = new JPanel(); titleRow.setLayout(
            new BoxLayout(titleRow, BoxLayout.Y_AXIS));

        JPanel usernameRow = new JPanel(); usernameRow.setLayout(
            new BoxLayout(usernameRow, BoxLayout.X_AXIS));

        JPanel passwordRow = new JPanel(); passwordRow.setLayout(
            new BoxLayout(passwordRow, BoxLayout.X_AXIS));

        JPanel buttonRow = new JPanel(); buttonRow.setLayout(
            new BoxLayout(buttonRow, BoxLayout.X_AXIS));

        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-= title panel =-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        JLabel lblAdmin = new JLabel("ADMIN"); lblAdmin.setAlignmentX(0.5f);
        lblAdmin.setFont(lblAdmin.getFont().deriveFont(Font.BOLD).deriveFont(50f));

        JLabel lblConsole = new JLabel("CONSOLE"); lblConsole.setAlignmentX(0.5f);
        lblConsole.setFont(lblConsole.getFont().deriveFont(Font.ITALIC).deriveFont(40f));

        titleRow.add(lblAdmin);
        titleRow.add(lblConsole);

        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-= username panel =-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        JLabel lblUsername = new JLabel("username:");
        JTextField txtUsername = new JTextField();
        txtUsername.setMaximumSize(new Dimension(125, 25));

        usernameRow.add(lblUsername);
        usernameRow.add(Box.createRigidArea(new Dimension(10, 0)));
        usernameRow.add(txtUsername);

        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-= password panel =-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        JLabel lblPassword = new JLabel("password:");
        JTextField txtPassword = new JTextField();
        txtPassword.setMaximumSize(new Dimension(125, 25));

        passwordRow.add(lblPassword);
        passwordRow.add(Box.createRigidArea(new Dimension(10, 0)));
        passwordRow.add(txtPassword);

        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-= button panel =-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        JButton btnLogIn = new JButton("Log In");
        JButton btnCancel = new JButton("Cancel");

        btnLogIn.setMinimumSize(new Dimension(80, 30));
        btnLogIn.setMaximumSize(new Dimension(80, 30));

        btnCancel.setMinimumSize(new Dimension(80, 30));
        btnCancel.setMaximumSize(new Dimension(80, 30));

        buttonRow.add(btnLogIn);
        buttonRow.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonRow.add(btnCancel);

        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-= main panel =-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        basicPanel.add(Box.createVerticalGlue());
        basicPanel.add(titleRow);
        basicPanel.add(Box.createRigidArea(new Dimension(0, 30)));
        basicPanel.add(usernameRow);
        basicPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        basicPanel.add(passwordRow);
        basicPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        basicPanel.add(buttonRow);
        basicPanel.add(Box.createVerticalGlue());

        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-= frame stuff =-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        add(basicPanel);

        setSize(240, 320);
        setTitle("Box Layout Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                Main app = new Main();

                app.init();
                app.setVisible(true);
            }           
        });
    }
}

Edited 4 Years Ago by m4ster_r0shi

What do you mean by saying "fix"? Regarding that particular example, if you look at the relevant code, you'll see that it uses a basic JPanel and adds a vertical glue and the button panel to it (in that order). If you want the buttons to be up, you should add the glue after adding the button panel (you'll also have to move the rigid area above the buttons if you don't want them to stick to the title bar):

public final void initUI() {

    //...

    //basic.add(Box.createVerticalGlue()); // <- remove this

    //...

    basic.add(Box.createRigidArea(new Dimension(0, 15))); // <- add this
    basic.add(bottom);
    //basic.add(Box.createRigidArea(new Dimension(0, 15))); // <- remove this

    basic.add(Box.createVerticalGlue()); // <- add this

    //...
}

You can find more about glues and rigid areas in the first link cOrRuPtG3n3t!x mentioned.

Edited 4 Years Ago by m4ster_r0shi

This article has been dead for over six months. Start a new discussion instead.