I'm kinda bad at explaining but I'll do my best.
I've basically been trying to add a border around my JLabel
, JTextField
and JButton
components within my JPanel
, however the border is expanding according to size.
This is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginPanel
{
private JPanel loginPanel = new JPanel();
private JFrame loginFrame = new JFrame();
public LoginPanel()
{
loginPanel.setLayout(new GridBagLayout());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
JTextField textLogin = new JTextField(10);
JPasswordField password = new JPasswordField(10);
JButton login = new JButton("Login");
JButton register = new JButton("Register");
gridBagConstraints.insets = new Insets(0,0,0,0);
gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 0;
loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));
loginPanel.add(new JLabel("E-Mail"), gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(textLogin, gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(new JLabel("Password"), gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(password, gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(login, gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(register, gridBagConstraints);
loginFrame.pack();
loginFrame.add(loginPanel);
loginFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
loginFrame.setLocationRelativeTo(null);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setVisible(true);
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e)
{
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new LoginPanel();
}
});
}
}
This is the outcome:
This is what I want the border to be like:
Hope the images explain more, I'll respond and provide any more questions needed.
A frame has a BorderLayout
by default. A component added to a border layout with no constraint is put in the CENTER
. The component in the center of of a border layout will be stretched to the full width and height of the available space.
It can be fixed by changing:
loginFrame.pack();
To:
loginFrame.setLayout(new GridBagLayout());
loginFrame.pack();
Since a single component added to a GridBagLayout
with no constraint will be centered.
The GUI does not appear here, like in your screenshot. Instead the components are closer together and the titled border is snug up against them.
The first can be fixed by changing:
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
To:
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
The 2nd can be fixed by making the border a compound border that combines an EmptyBorder
with the TitledBorder
.
Border border = new CompoundBorder(
new TitledBorder("Login"),
new EmptyBorder(40, 50, 40, 50));
loginPanel.setBorder(border);
//loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With