2012-06-12 56 views
0

所以我的問題是,我曾與Jspinners有過。是我用一個標題邊框圍繞我的jspinner嗎?然而在gui中,由於js較小,所以裁減標題文本。那麼我怎麼能夠根據標題的長度強制gui進行rezise,或者我怎樣才能讓jspinner更長時間地爲我提供足夠的空間來適應整個標題?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 
+0

我遇到的時候JSpinner的的TitledBorder甚至超過這個例子之前這個問題。我也試過.setMaximumSize(object.getPreferredSize());在微調器,childPanel和mainPanel上。似乎也沒有解決這個問題。 – whitewolfpgh

+0

一個')'也許還可以利用Nimbus的外觀和感覺,而不是用XxxBorders困擾,'B)'我建議,只有在你將永遠爲背景或前景設置的情況下,通知應如此艱難和棘手爲Nimbus L&F設置這兩個值 – mKorbel

+0

1)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 2)Swing的一位大師詛咒'TitledBorder' - 它有怪癖和問題。 –

回答

4

只需添加的JSpinner到一個JPanel,並且邊框添加到面板:

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

編輯:另外請注意,使用BorderFactory優於建立邊境實例:

只要有可能,這FAC tory將分發對共享邊界實例的引用。

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

+0

thx爲使用borderfactory提示:)。並感謝你的解決方案的工作,爲什麼我沒有想到它!?!?! – whitewolfpgh

+2

+1作爲參考,「我們建議您將組件放在'JPanel'中,並在'JPanel'上設置邊界」 - ['setBorder()'](http://docs.oracle.com/javase/ 7 /文檔/ API /的javax /擺動/ JComponent.html#setBorder%28javax.swing.border.Border%29)。 – trashgod