Качели центр jpanel в другой jpanel - PullRequest
0 голосов
/ 14 марта 2019

У меня есть проблема, которую я не могу передать, проблема в том, что я не могу центрировать jpanel с контентом (contentPanel, он имеет SpringLayout) внутри другой jpanel (LoginPanel, главная панель), которая имеет GridBagLayout (для центр контент-панели).

вот так это выглядит, панель контента - это просто точка в центре, я не знаю, чего мне не хватает enter image description here

Вот код:

import javax.swing.*;

import com.berry.BCrypt;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.Arrays;

public class LoginPanel extends JPanel {

    private JLabel user;
    private JLabel pass;
    private JLabel invalidUser;
    private JLabel invalidPass;
    private JTextField inputUser;
    private JPasswordField inputPass;
    private JButton login;
    private JButton signin;

    public JPasswordField getInputPass() {
        return inputPass;
    }
    public JTextField getInputUser() {
        return inputUser;
    }
    public JButton getSigninButton() {
        return signin;
    }
    public JButton getLoginButton() {
        return login;
    }
    public LoginPanel() {
        GridBagLayout gridbag = new GridBagLayout();
        SpringLayout layout = new SpringLayout();

        JPanel contentPanel = new JPanel();
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        contentPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        contentPanel.setLayout(layout);

        user = new JLabel("User");
        contentPanel.add(user);
        pass = new JLabel("Password");
        contentPanel.add(pass);
        inputUser = new JTextField(15);
        contentPanel.add(inputUser);
        inputPass = new JPasswordField(15);
        contentPanel.add(inputPass);
        login = new JButton("Login");
        contentPanel.add(login);
        signin = new JButton("Signin");
        contentPanel.add(signin);
        invalidUser = new JLabel("Invalid User!");
        invalidUser.setVisible(false);
        contentPanel.add(invalidUser);
        invalidPass = new JLabel("Incorrect password!");
        invalidPass.setVisible(false);
        contentPanel.add(invalidPass);
        // user label = user textfield
        layout.putConstraint(SpringLayout.WEST, user, 10, SpringLayout.WEST, contentPanel);
        layout.putConstraint(SpringLayout.NORTH, user, 25, SpringLayout.NORTH, contentPanel);
        layout.putConstraint(SpringLayout.WEST, inputUser, 45, SpringLayout.EAST, user);
        layout.putConstraint(SpringLayout.NORTH, inputUser, 25, SpringLayout.NORTH, contentPanel);
        // password label = password textfield
        layout.putConstraint(SpringLayout.WEST, pass, 10, SpringLayout.WEST, contentPanel);
        layout.putConstraint(SpringLayout.NORTH, pass, 25, SpringLayout.NORTH, user);
        layout.putConstraint(SpringLayout.WEST, inputPass, 45, SpringLayout.EAST, user);
        layout.putConstraint(SpringLayout.NORTH, inputPass, 25, SpringLayout.NORTH, inputUser);
        // login button
        layout.putConstraint(SpringLayout.WEST, login, 20, SpringLayout.WEST, contentPanel);
        layout.putConstraint(SpringLayout.NORTH, login, 50, SpringLayout.NORTH, pass);
        // signin button
        layout.putConstraint(SpringLayout.WEST, signin, 10, SpringLayout.EAST, login);
        layout.putConstraint(SpringLayout.NORTH, signin, 50, SpringLayout.NORTH, inputPass);

        Database db = new Database();



        login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                String userInput = inputUser.getText();
                String userFetched = db.checkUser(userInput);
                char[] passInputchar = inputPass.getPassword();
                String passInput = String.valueOf(passInputchar);
                String passFetched = db.fetchPass(userFetched);

                Boolean passFetchedChecked=false;
                if(!(userFetched==null)) {
                    passFetchedChecked = BCrypt.checkpw(passInput, passFetched);
                }


                if (userFetched == null) {
                    layout.putConstraint(SpringLayout.WEST, invalidUser, 10, SpringLayout.WEST, contentPanel);
                    layout.putConstraint(SpringLayout.NORTH, invalidUser, 25, SpringLayout.NORTH, user);

                    layout.putConstraint(SpringLayout.NORTH, pass, 25, SpringLayout.NORTH, invalidUser);
                    layout.putConstraint(SpringLayout.NORTH, inputPass, 25, SpringLayout.NORTH, invalidUser);

                    invalidUser.setVisible(true);
                } else {
                    layout.putConstraint(SpringLayout.NORTH, pass, 25, SpringLayout.NORTH, user);
                    layout.putConstraint(SpringLayout.NORTH, inputPass, 25, SpringLayout.NORTH, inputUser);
                    invalidUser.setVisible(false);
                }
                if (!passFetchedChecked) {
                    layout.putConstraint(SpringLayout.WEST, invalidPass, 10, SpringLayout.WEST, contentPanel);
                    layout.putConstraint(SpringLayout.NORTH, invalidPass, 25, SpringLayout.NORTH, pass);

                    layout.putConstraint(SpringLayout.NORTH, login, 50, SpringLayout.NORTH, invalidPass);
                    layout.putConstraint(SpringLayout.NORTH, signin, 50, SpringLayout.NORTH, invalidPass);

                    invalidPass.setVisible(true);
                }else {
                    layout.putConstraint(SpringLayout.NORTH, login, 50, SpringLayout.NORTH, pass);
                    layout.putConstraint(SpringLayout.NORTH, signin, 50, SpringLayout.NORTH, inputPass);

                    invalidPass.setVisible(false);
                }
                if ((userFetched!=null) && (passFetchedChecked)) {
                    inputUser.setText("");
                    inputPass.setText("");
                }
            }

        });
        setLayout(gridbag);
        add(contentPanel);
    }

}

Я решил это, мне просто нужно было добавить contentPanel.setPreferredSize (новое измерение (300, 150));

enter image description here

...