Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

java - Resize the button of login screen

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...