2013-12-08 66 views
1

我有一個BoxPanel中的對齊的基本問題。當TextAreas中的任何一個進行輸入時,面板中不是面板寬度的其他組件都隨字符一起移動。Swing組件移動爲TextArea填充,在斯卡拉

我的錯誤是什麼?謝謝!

blank TextArea

filled TextArea

val exitButton = new Button { 
    text = "Exit" 
    borderPainted = true 
    enabled = true 
} 

val japaneseTranslation = new TextArea(5, 25) 
val englishTranslation = new TextArea(5,25) 

val translationPanel = new BoxPanel (Orientation.Vertical) { 
    contents += new Label { 
    text = "Translation" 
    font = new Font("Ariel", java.awt.Font.PLAIN, 20) 
    horizontalAlignment = Alignment.Center 
    } 

    contents += new Label { 
    text = "Japanese" 
    font = new Font("ariel", java.awt.Font.PLAIN, 10) 
    } 

    /*contents += new TextField ("Japanese") {                             
    editable = false                                   
    }*/ 
    contents += japaneseTranslation 


    contents += new Label { 
    text = "English" 
    font = new Font("ariel", java.awt.Font.PLAIN, 10) 
    } 
    contents += englishTranslation 

    contents += exitButton 

} 
+2

假設'BoxPanel'基於Swing的'BoxLayout',您可能希望將'japaneseTranslation'和'englishTranslation'的'setAlignmentX'設置爲'Component.LEFT_ALIGNMENT'。不知道Scala中這個常量的等價物。 – tenorsax

+0

您是否測試了下面提供的解決方案?這能解決你的問題嗎? – Simon

+1

@Aqua,這是一個有益的領導,謝謝。 'peer.setAlignment ...'正如在Simon的回答中,顯然是在Scala中的工作等價物。 –

回答

1

中包裝TextArea組件集成到一個ScrollPane幫助:

val exitButton = new Button { 
    text = "Exit" 
    borderPainted = true 
    enabled = true 
} 

val japaneseTranslation = new TextArea(5, 25) 
val englishTranslation = new TextArea(5,25) 

val translationPanel = new BoxPanel (Orientation.Vertical) { 
    contents += new Label { 
    text = "Translation" 
    font = new Font("Ariel", java.awt.Font.PLAIN, 20) 
    horizontalAlignment = Alignment.Center 
    } 

    contents += new Label { 
    text = "Japanese" 
    font = new Font("ariel", java.awt.Font.PLAIN, 10) 
    } 

    /*contents += new TextField ("Japanese") {                             
    editable = false                                   
    }*/ 
    contents += new ScrollPane(japaneseTranslation) 


    contents += new Label { 
    text = "English" 
    font = new Font("ariel", java.awt.Font.PLAIN, 10) 
    } 
    contents += new ScrollPane(englishTranslation) 

    contents += exitButton 

} 

另一種可能性是,由水族在他的評論暗示,要設置的alignmentX您的TextArea組件:

val japaneseTranslation = new TextArea(5, 25) { 
    peer.setAlignmentX(0) 
} 
val englishTranslation = new TextArea(5,25) { 
    peer.setAlignmentX(0) 
} 

下次請發佈SSCCE

+0

抱歉,延遲響應。這兩個解決方案都完全解決了問題Aqua的解決方案看起來很有希望,我試圖實現它,但沒有找到「同行」。我將在下次發佈SSCCE。謝謝! –