【问题标题】:How to set a border around a specific JPanel in the JFrame?如何在 JFrame 中围绕特定 JPanel 设置边框?
【发布时间】:2017-07-23 13:28:12
【问题描述】:

我不太擅长解释,但我会尽力而为。

我基本上一直在尝试在我的JPanel 内的JLabelJTextFieldJButton 组件周围添加边框,但是边框会根据大小扩大。

这是我的代码:

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

这是结果:

这就是我希望边框的样子:

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

【问题讨论】:

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


    【解决方案1】:

    默认情况下,框架具有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"));
    

    【讨论】:

      【解决方案2】:

      为您的框架使用第二个 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,您可以更好地控制空间使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-08
        • 2011-11-30
        • 1970-01-01
        • 1970-01-01
        • 2021-01-16
        • 2019-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多