user1234
user1234

Reputation: 437

Put text boxes in separate lines

MainClass(){
    JFrame main = new JFrame("Login Form ");
    main.setBounds(350,150,500,500);
    main.setVisible(true);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    name = new JTextField(10);
    pass = new JTextField(10);
    main.setLayout(new GridLayout(0,1));
    JPanel pane = new JPanel();
    main.add(pane);
    main.add(new JLabel("Username: "));
    pane.add(name);
    //main.add(pane);
    pane.add(new JLabel("Password: "));
    pane.add(pass);
    submit = new JButton("Submit");
    pane.add(submit);
    submit.addActionListener(new Handler());
}

I want to separate the text boxes in separate lines after the label username and name text box. I need to control the cursor to a new line.

Upvotes: 4

Views: 22120

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

i want to separate the text boxes in separate lines

Layout

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

class MainClass {

    JTextField name;
    // This should be a JPasswordField!
    JTextField pass;
    JButton submit;

    MainClass(){
        JFrame main = new JFrame("Login Form ");
        // Don't use this nonsense!
        //main.setBounds(350,150,500,500);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        name = new JTextField(10);
        pass = new JTextField(10);
        main.setLayout(new GridLayout(0,1));
        JPanel pane = new JPanel(new GridLayout(0,1));
        main.add(pane);
        pane.add(new JLabel("Username: "));
        pane.add(name);
        pane.add(new JLabel("Password: "));
        pane.add(pass);
        submit = new JButton("Submit");
        pane.add(submit);
        //submit.addActionListener(new Handler());
        main.pack();
        main.setVisible(true);
    }

    public static void main(String[] args) {
        MainClass mc = new MainClass();
    }
}

If I was building a login screen, it might be laid out more along these lines (with the labels right justified and the button in it's own panel - left as an exercise for the reader).

Log-In frame

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

class MainClass {

    JTextField name;
    JPasswordField pass;
    JButton submit;

    MainClass(){
        JFrame main = new JFrame("Login Form ");
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        name = new JTextField(10);
        pass = new JPasswordField(10);

        JPanel gui = new JPanel(new BorderLayout(3,3));
        gui.setBorder(new EmptyBorder(5,5,5,5));
        main.setContentPane(gui);

        JPanel labels = new JPanel(new GridLayout(0,1));
        JPanel controls = new JPanel(new GridLayout(0,1));
        gui.add(labels, BorderLayout.WEST);
        gui.add(controls, BorderLayout.CENTER);

        labels.add(new JLabel("Username: "));
        controls.add(name);
        labels.add(new JLabel("Password: "));
        controls.add(pass);
        submit = new JButton("Submit");

        gui.add(submit, BorderLayout.SOUTH);
        main.pack();
        main.setVisible(true);
    }

    public static void main(String[] args) {
        MainClass mc = new MainClass();
    }
}

Upvotes: 8

arcy
arcy

Reputation: 13123

Use BoxLayout; create a JPanel, use SetLayout to set it to BoxLayout, and set the BoxLayout to PAGE_AXIS. Then things you add go one after another vertically down the 'page'. There are options for alignment, see the API for BoxLayout or the Oracle/Sun/Java tutorial on layout managers.

Upvotes: 1

Related Questions