1

I want my JPanel to be still in the center when I maximize or re-size the JFrame. How can I do that?

Tried:

  jframe.add(panel, BorderLayout.CENTER);
     panel.setAlignmentX(JComponent.CENTER_ALIGNMENT);

But it doesn't work.

Here is my other code:

public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    login frame = new login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public login() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        setVisible(true);
        setBounds(5, 5, 409, 267);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(0, 128, 128));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
            contentPane.setLayout(null);

        panel = new JPanel();
        panel.setBounds(57, 42, 292, 167);
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 255)));
        panel.setLayout(null);
        contentPane.add(panel);

        textField = new JTextField();
        textField.setBounds(71, 35, 192, 26);
        panel.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(71, 72, 192, 26);
        panel.add(passwordField);

        JLabel lblNewLabel = new JLabel("Username:");
        lblNewLabel.setBounds(6, 41, 68, 14);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Password:");
        lblNewLabel_1.setBounds(6, 78, 68, 14);
        panel.add(lblNewLabel_1);

        JButton btnNewButton = new JButton("Login");
        btnNewButton.setBounds(71, 120, 89, 23);
        panel.add(btnNewButton);



        JButton btnNewButton_1 = new JButton("Cancel");
        btnNewButton_1.setBounds(174, 120, 89, 23);
        panel.add(btnNewButton_1);

How will I do that here?

enter image description here

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rohan21
  • 353
  • 5
  • 9
  • 24
  • 3
    1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (to be an MCVE, that code would require a class declaration and imports). – Andrew Thompson Jan 11 '14 at 07:47

3 Answers3

7

You could simply use GridBagLayout on the parent container. It lays all it's components out around the center of the container.

NormalMax

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class CenterComponent {

    public static void main(String[] args) {
        new CenterComponent();
    }

    public CenterComponent() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel content = new JPanel(new GridBagLayout());
                content.setBackground(Color.GREEN);
                content.setBorder(new EmptyBorder(20, 20, 20, 20));
                frame.setContentPane(content);
                frame.add(new LoginPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class LoginPane extends JPanel {

        public LoginPane() {
            setLayout(new GridBagLayout());
            setBorder(new TitledBorder("Login"));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(new JLabel("Username:"), gbc);
            gbc.gridy++;
            add(new JLabel("Password:"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.gridwidth = 2;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);

            gbc.gridx = 1;
            gbc.gridy++;
            gbc.gridwidth = 1;
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.NONE;
            add(new JButton("Login"), gbc);
            gbc.gridx++;
            add(new JButton("Cancel"), gbc);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Don't use a null layout. You can do this with a BoxLayout or a GridLayout, depending on the behavior you want. Check the tutorials for more info: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • @MadProgrammer it wont go center when i maximize the size of jframe – Rohan21 Jan 11 '14 at 05:24
  • @Rohan21 Yes, it does. `GridBagLayout` uses the center of the container to position the child components about. See my answer for a demonstration... – MadProgrammer Jan 11 '14 at 05:33
0

Replace

panel.setLayout(null);

By

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

OR

panel.setLayout(new GridLayout(3,1, 5, 5));
ravibagul91
  • 20,072
  • 5
  • 36
  • 59