Java Swing -GridBagConstraint插入颜色

mccptt67  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(104)

我在使用GridBagConstraints时遇到了一些问题,特别是insets属性。
我有两个组件,我想在主题之间设置一些自定义边距。现在,我所做的是使用约束。插入这样:

polinomioConstraints.insets = new Insets(5, 0, 15 ,0);

checkboxesPanel.add(infoAggiunte, polinomioConstraints);
checkBoxesPanel.add(anotherString, constraintWithoutInsets);

问题是,由于某种原因,一个白色块出现在插入区域,所以在两个组件之间,我发现没有办法改变白色的实际面板的背景

qni6mghb

qni6mghb1#

这是我为别人做的图形用户界面,我添加了黄色背景。

我在Swing组件之间没有看到白色。
下面是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
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.SwingUtilities;

public class MainRegistrationPage {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainRegistrationPage();
            }
        });
    }

    private JTextField firstname;
    private JTextField lastname;
    private JTextField email;
    private JPasswordField password;

    private JFrame frame;

    public MainRegistrationPage() {
        // create user interface
        frame = new JFrame("Registration Page");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createRegistrationPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createRegistrationPanel() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
        mainPanel.setBackground(Color.YELLOW);
        Font font = new Font("Georgia", Font.PLAIN, 14);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(5, 5, 5, 5);

        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel label = new JLabel("First Name:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        firstname = new JTextField(30);
        firstname.setFont(font);
        mainPanel.add(firstname, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        label = new JLabel("Last Name:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        lastname = new JTextField(30);
        lastname.setFont(font);
        mainPanel.add(lastname, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        label = new JLabel("Email:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        email = new JTextField(30);
        email.setFont(font);
        mainPanel.add(email, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        label = new JLabel("Password:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        password = new JPasswordField(30);
        password.setFont(font);
        mainPanel.add(password, gbc);

        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy++;
        JButton submit = new JButton("Submit");
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        mainPanel.add(submit, gbc);

        return mainPanel;
    }

}

相关问题