【问题标题】:BoxLayout left margin for TextFieldTextField 的 BoxLayout 左边距
【发布时间】:2015-03-10 12:44:35
【问题描述】:

我正在使用 BoxLayout,并且有 2 个 JTextField。我需要为它们添加一些边距选项,因为它们离窗口边框太近了。

代码:

JPanel top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
top.add(new JLabel("Recipient:"));        
recipient.setMaximumSize( recipient.getPreferredSize() );
top.add(recipient);
top.add(new JLabel("Subject:"));
subject.setMaximumSize( subject.getPreferredSize() );
top.add(subject);
top.add(new JLabel("Message:"));
add("North", top);

如果我添加一个 Horizo​​ntalStrut,它只会影响标签,而不影响 TextFields。谢谢你的建议!

【问题讨论】:

    标签: java swing margin textfield boxlayout


    【解决方案1】:

    在面板中添加Border

    JPanel top = new JPanel();
    top.setBorder( BorderFactory.createEmptyBorder(....) );
    

    阅读 How to use Borders 上的 Swing 教程部分,了解更多信息和示例。

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多