0

所以我的问题是,我以前在 Jspinners 上遇到过。是我用带标题的边框包围我的 jspinner。然而,在 gui 中,它削减了标题文本,因为 js 更小。那么如何根据标题的长度强制 gui 重新调整,或者如何让 jspinner 更长,我想提供足够的空间来容纳整个标题?

JTextField name= new JTextField(9);
name.setBorder(new TitledBorder("Name"));
//setup spinner date format
SimpleDateFormat datePattern = new SimpleDateFormat("MM/dd/yyyy");
JSpinner dob = new JSpinner(new SpinnerDateModel());
dob.setEditor(new JSpinner.DateEditor(dob, datePattern.toPattern()));
dob.setBorder(new TitledBorder("Date of Birth"));
JPanel childPanel = new JPanel();
childPanel.setLayout(new FlowLayout());
childPanel.add(name);
childPanel.add(dob);
childPanel.setBorder(new TitledBorder("Child Info"));

然后:

JPanel mainPanel = new JPanel(); 
mainPanel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));        
mainPanel.add(childPanel);
childPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
//there are more items in this panel, just omitted them, that's why using BoxLayout
4

1 回答 1

4

只需将 JSpinner 添加到 JPanel,并将边框添加到面板:

    JPanel dobPanel = new JPanel();
    dobPanel.add(dob);
    dobPanel.setBorder(BorderFactory.createTitledBorder("Date of Birth"));

编辑:还要注意,使用 BorderFactory 比创建 Border 实例更好:

只要有可能,该工厂将分发对共享 Border 实例的引用。

http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html

于 2012-06-12T16:01:43.410 回答