Я создаю графический интерфейс, который включает в себя флажок с использованием Java, Swing и Gridbaglayout для хоста JPanel. Я не могу получить текст, чтобы соответствовать флажке; Он сидит слишком высоко:
Я работаю в Windows, используя OpenJdk 24.0.1 и IntelliJ IDE с монитором 4K. Воспроизводимый пример: < /p>
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
/*=============
/ Declarations
/=============*/
Color bgColor = Color.BLACK;
/*========================
/ Create the master frame
/========================*/
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Checkbox Alignment Test");
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.setSize(200, 50);
frame.getContentPane().setBackground(bgColor);
frame.setLayout(new BorderLayout(0, 0));
/*========================
/ Create the master panel
/========================*/
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setLayout(new GridBagLayout());
mainPanel.setBackground(bgColor);
mainPanel.setPreferredSize(new Dimension(200, 50));
/*==================================================
/ Create the widgets and add them to the main panel
/==================================================*/
Font normFont = new Font("Calibri", Font.PLAIN, 16);
GridBagConstraints gbc = new GridBagConstraints();
JCheckBox cbEnabled = new JCheckBox("Monitoring Enabled");
cbEnabled.setFont(normFont);
cbEnabled.setForeground(Color.BLACK);
cbEnabled.setFont(normFont);
cbEnabled.setVerticalAlignment(SwingConstants.CENTER);
gbc.ipadx = 5;
gbc.ipady = 5;
gbc.insets = new Insets(1, 5, 1, 5);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = .25;
mainPanel.add(cbEnabled, gbc);
/*=====================================================
/ Add the main panel to the frame and make it visible.
/=====================================================*/
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
while (true) {
}
}
}
< /code>
Я попытался изменить настройки для GBC.ipady, GBC.Fill, GBC.Anchor и вертикальный размер самого JCheckbox - кажется, ничего не работает. Чего мне не хватает?
Подробнее здесь: https://stackoverflow.com/questions/797 ... h-checkbox