-1

I want to resize the button. How can I do that?

I want to resize my login button to be small and my textinput. I want my log in screen to be display nice in middle.

  • give me your full code please. and tell me clearly. – Kumaresan Perumal Oct 29 '15 at 05:17
  • Try making use of a different layout manager other then `GridLayout`, maybe like `GridBagLayout` – MadProgrammer Oct 29 '15 at 05:19
  • *"and my textinput.."* There is no such variable mentioned in that code. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Oct 29 '15 at 05:20
  • 1
    `setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 29 '15 at 05:20
  • 1
    `Toolkit toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);` amounts to `setLocationRelativeTo(null)`, but better to use `setLocationByPlatform(true)` .. – Andrew Thompson Oct 29 '15 at 05:21
  • I what to resize like this – user3387275 Oct 29 '15 at 05:28
  • check this on how my layout is https://community.oracle.com/message/13376058#13376058 – user3387275 Oct 29 '15 at 05:28
  • the declaration is private static final long serialVersionUID = 1L; private JPanel backPanel = null, midPanel = null,bottomPanel; //JPanel bottomPanel = new JPanel(); private JLabel usernameLabel = null, passwordLabel = null; private JPasswordField passwordField = null; private JTextField usernameText = null; private JButton loginButton = null; – user3387275 Oct 29 '15 at 05:30
  • *"check this on how my layout is "* Not interested in what the broken GUI is like. I repeat.. "Provide ASCII art or a simple drawing of the ***intended* layout** of the GUI at minimum size, and if resizable, with more width and height." 'Intended' means 'what is needed/wanted'. – Andrew Thompson Oct 29 '15 at 05:41

1 Answers1

1

Start by trying different layout managers, other than GridLayout, like GridBagLayout for example:

Login

backPanel = new JPanel(new BorderLayout());
midPanel = new JPanel(new GridBagLayout());
bottomPanel = new JPanel(new GridBagLayout());
//bottomPanel.setLayout(new BorderLayout());
//final FrameTestBase = new FrameTestBase();

loginButton = new JButton(" Login ");
bottomPanel.add(loginButton);

usernameLabel = new JLabel(" Username : ");
passwordLabel = new JLabel(" Password : ");

usernameText = new JTextField(20);
passwordField = new JPasswordField(20);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);

midPanel.add(usernameLabel, gbc);
gbc.gridx++;
midPanel.add(usernameText, gbc);
gbc.gridx = 0;
gbc.gridy++;
midPanel.add(passwordLabel, gbc);
gbc.gridx++;
midPanel.add(passwordField, gbc);

bottomPanel.add(loginButton);
backPanel.add(midPanel);
backPanel.add(bottomPanel, BorderLayout.SOUTH);

Take a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details.

You can easily display a window in the middle of the screen by using frame.setLocationRelativeTo(null);, for example

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LoginExample {

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

    public LoginExample() {
        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);
                frame.add(new LoginPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class LoginPane extends JPanel {
        private final JPanel backPanel;
        private final JPanel midPanel;
        private final JPanel bottomPanel;
        private final JButton loginButton;
        private final JLabel usernameLabel;
        private final JLabel passwordLabel;
        private final JTextField usernameText;
        private final JPasswordField passwordField;

        public LoginPane() {
            backPanel = new JPanel(new BorderLayout());
            midPanel = new JPanel(new GridBagLayout());
            bottomPanel = new JPanel(new GridBagLayout());

            loginButton = new JButton(" Login ");
            bottomPanel.add(loginButton);

            usernameLabel = new JLabel(" Username : ");
            passwordLabel = new JLabel(" Password : ");

            usernameText = new JTextField(20);
            passwordField = new JPasswordField(20);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);

            midPanel.add(usernameLabel, gbc);
            gbc.gridx++;
            midPanel.add(usernameText, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            midPanel.add(passwordLabel, gbc);
            gbc.gridx++;
            midPanel.add(passwordField, gbc);

            bottomPanel.add(loginButton);
            backPanel.add(midPanel);
            backPanel.add(bottomPanel, BorderLayout.SOUTH);

            setLayout(new BorderLayout());
            add(backPanel);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • the button is to far at the botton how can I move it up just under the password textitem – user3387275 Oct 30 '15 at 11:02
  • Seriously? You could try changing the `GridBagConstraints#insets` for the `passwordField` – MadProgrammer Oct 30 '15 at 12:42
  • Or do you mean when the window is resized? In that case I'd move the `bottomPane` to the `midPanel`, but you'd need to use the `GridBagConstraints#gridwidth` property set to `2`, just make sure you add it to the `gridx` cell of `0` and you'll probably want to change the `anchor` property to `GridBagConstraints.CENTER` – MadProgrammer Oct 30 '15 at 12:49
  • Yep, see last comment. Because the button is is currently been added to the `bottomPanel`, which is in the `SOUTH` position, it will always be at the bottom of the screen. You need to add `bottomPanel` in the `midPanel` – MadProgrammer Nov 02 '15 at 05:06