jspinner标题为边界被切断

时间:2012-06-12 15:06:47

标签: java swing jspinner titled-border

所以我的问题是,我之前和Jspinners一样。是我用一个标题边框包围我的jspinner。然而在gui它削减标题文本因为js更小。那么我怎样才能根据标题的长度强制gui重新设置,或者如何让jnger更长时间地为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

1 个答案:

答案 0 :(得分:4)

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

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

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

  

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

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