GridBagLayout布局

admin 2024年10月15日10:27:33GridBagLayout布局已关闭评论23 views字数 1764阅读5分52秒阅读模式
package org.example.blog;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class GridBagLayoutDemo {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("GridBagLayoutDemo");

JPanel wrapPane = new JPanel();
wrapPane.setLayout(new BoxLayout(wrapPane, BoxLayout.Y_AXIS));
wrapPane.setBorder(new EmptyBorder(5, 5, 5, 5));
JScrollPane contentPane = new JScrollPane(wrapPane);
contentPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.getVerticalScrollBar().setUnitIncrement(30);

JPanel settingPanel = new JPanel();
settingPanel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.WEST;
constraints.fill = GridBagConstraints.NONE;
constraints.insets = new Insets(5, 5, 5, 5);

JLabel urlLabel = new JLabel("URL:");
constraints.gridx = 0;
constraints.gridy = 0;
settingPanel.add(urlLabel, constraints);

JTextField urlField = new JTextField();
constraints.gridx = 1;
constraints.weightx = 1.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
settingPanel.add(urlField, constraints);

JLabel apiKeyLabel = new JLabel("API KEY:");
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weightx = 0.0;
settingPanel.add(apiKeyLabel, constraints);

JTextField apiKeyField = new JTextField();
constraints.gridx = 1;
constraints.weightx = 1.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
settingPanel.add(apiKeyField, constraints);

settingPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
settingPanel.setMaximumSize(new Dimension(300, settingPanel.getPreferredSize().height));
wrapPane.add(settingPanel);

frame.setContentPane(contentPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(600, 360));
frame.setVisible(true);
}
});
}
}

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年10月15日10:27:33
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   GridBagLayout布局https://cn-sec.com/archives/3268631.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.