如何在JFrame中围绕特定JPanel设置边框?

时间:2017-03-03 12:24:51

标签: java swing layout-manager gridbaglayout border-layout

我很难解释,但我会尽我所能。

我基本上一直在尝试在JLabel内的JTextFieldJButtonJPanel组件周围添加边框,但边框会根据大小进行扩展。

这是我的代码:

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

这是结果: Image 1: Outcome

这就是我想要的边框: Image 2: What I want the look to be like.

希望图像解释更多,我会回复并提供所需的更多问题。

2 个答案:

答案 0 :(得分:6)

默认情况下,框架具有BorderLayout。添加到没有约束的边框布局的组件放在CENTER中。边框布局中心的组件将拉伸到可用空间的整个宽度和高度。

可以通过更改:

来修复
    loginFrame.pack();

要:

    loginFrame.setLayout(new GridBagLayout());
    loginFrame.pack();

由于添加到没有约束的GridBagLayout的单个组件将居中。

其他提示:

此处不会显示GUI,就像在屏幕截图中一样。相反,组件更紧密,标题边框紧贴它们。

第一个可以通过更改:

来修复
    gridBagConstraints.insets = new Insets(0, 0, 0, 0);

要:

    gridBagConstraints.insets = new Insets(5, 5, 5, 5);

可以通过将边框设为将EmptyBorderTitledBorder组合在一起的复合边框来修复第二个。

    Border border = new CompoundBorder(
            new TitledBorder("Login"), 
            new EmptyBorder(40, 50, 40, 50));
    loginPanel.setBorder(border);
    //loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));

答案 1 :(得分:3)

为您的框架使用第二个GridBagLayout:

    ...
    loginFrame.setLayout(new GridBagLayout());
    loginFrame.add(loginPanel, new GridBagConstraints(
        0, 0,
        1, 1, 
        0.0, 0.0, 
        GridBagConstraints.CENTER, GridBagConstraints.NONE, 
        new Insets(0, 0, 0, 0), 40, 20));
    ...

JFrame默认使用BorderLayout - 其CENTER组件将使用所有可用空间。使用GridBagLayout可以更好地控制空间使用。