2011-03-30 26 views
1

這是我的問題,當我開始鍵入併到達JEditorPane的垂直邊緣時,jeditor窗格隨着我的輸入一直延伸到最後的窗戶。我的textareas下面的jeditor最終消失。基本上,所有東西都會向下移動,我希望jeditor窗格保持靜態高度。Swing:使用jeditor窗格,在窗口中輸入,導致我的顯示屏向下移動

爲什麼jeditor窗格會移動?我如何防止它?

我以前見過這個,但不記得如何解決它。我認爲這與我如何使用佈局管理器有關。我可以使用不同的佈局,但我更熟悉GridBag。我嘗試了首選高度,它最初起作用,但正如我所說的......輸入高度變化後。奇怪。

這裏是代碼(它是scala,但認爲它是僞代碼)。

import java.awt.{ Insets, GridBagConstraints, Dimension } 
import javax.swing._ 
import javax.swing.event._ 
import java.awt._ 
import java.awt.event._ 
import scala.swing.Swing._ 
import scala.swing.{ MainFrame, Panel, SimpleSwingApplication } 
import org.slf4j._ 
import java.io.{ File, FileReader } 
import javax.swing.filechooser.FileNameExtensionFilter 
import org.berlin.syntax.actions._ 
import org.berlin.syntax._ 
import org.berlin.syntax.components._ 

    class MyPanel extends JPanel { 

    val constraints: GridBagConstraints = this.defaultLayoutContraints() 
    val outputLogTextArea = new OutputTextArea 
    val outputStatusTextArea = new StatusTextArea 
    val inputConsoleTextArea = new InputTextArea 
    val labelCaretPos = new javax.swing.JLabel("(0)") 

    val outputTextScrollPane = defaultScroll(new JScrollPane(outputLogTextArea)) 
    val outputStatusScrollPane = defaultScroll(new JScrollPane(outputStatusTextArea)) 
    val inputTextScrollPane = defaultScroll(new JScrollPane(inputConsoleTextArea)) 

    val fileChooser = new JFileChooser 

    { 
     // Constructor 
     this.setLayout(new GridBagLayout) 
     this.add(outputTextScrollPane, constraints) 
     this.add(outputStatusScrollPane, shiftDown(constraints)) 
     this.add(inputTextScrollPane, shiftDown(constraints)) 
     this.add(labelCaretPos, shiftDown(constraints)) 
     fileChooser.setCurrentDirectory(targetInitialDir) 
     fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Current Log Files", "log")) 
     this.outputTextScrollPane.setViewportView(outputLogTextArea) 
     documentTypeChanged("text/plain") 
     new CaretMonitor(outputLogTextArea, labelCaretPos) 

    } // End constructor 

    def defaultLayoutContraints(): GridBagConstraints = { 
     val constraints = new GridBagConstraints 
     val insets = new Insets(2, 2, 2, 2) 
     constraints.insets = insets 
     constraints.anchor = GridBagConstraints.NORTHWEST 
     constraints.gridy = 3 
     constraints.gridx = 1 
     constraints.weightx = 1 
     constraints.weighty = 1 
     constraints.fill = GridBagConstraints.BOTH 
     return constraints 
    } 

    def defaultScroll(s: JScrollPane): JScrollPane = { 
     s.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) 
     s.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) 
     return s 
    } 


    def shiftDown(c: GridBagConstraints): GridBagConstraints = { 
     constraints.gridy = constraints.gridy + 1; 
     return constraints; 
    } 

    protected class OutputTextArea extends JEditorPane { 

     ??????????? 
     this.setPreferredSize(0, maxHeight - 230); 
     this.setCaretPosition(0) 
     this.setEditable(true) 
     this.setFont(new Font("Courier New", Font.PLAIN, 11)) 

     val bundle = java.util.ResourceBundle.getBundle("jsyntaxpane/Bundle") 
     this.setContentType(bundle.getString("SyntaxTester.jEdtTest.contentType")) 
     this.setCaretColor(new java.awt.Color(0, 0, 0)) 
    } 


    protected class StatusTextArea extends JTextArea { 
     this.setColumns(maxTextAreaCols) 
     this.setRows(6) 
     this.setLineWrap(false) 
     this.setCaretPosition(0) 
     this.setEditable(false) 
     this.setFont(new Font("Courier New", Font.PLAIN, 12)) 
    } 


    MyFrame extends JFrame { 
    this.setJMenuBar(coreContentPanel.createMenuBar) 
    this.setLocation(initXPos, initYPos) 
    this.setLayout(new FlowLayout(FlowLayout.CENTER)) 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 
    this.preferredSize = (maxWidth, maxHeight) 
    this.focusable = true 
    this.add(new MyPanel) 
    pack 
    } 

} // End of the Class // 
+0

請發送可執行文件。 – ziggystar 2011-03-30 15:47:38

+0

問題已解決,請參閱下面的回答 – 2011-03-30 18:28:24

回答

2

設置編輯器的首選大小在封裝在JScrollPane內後不會產生任何影響。您需要設置滾動窗格本身的首選大小(可能通過獲取包裝組件的首選大小)。

如果您使用的是GridBagLayout,您可能還會發現,在具有固定大小的元素(如標籤)上將GridBagContraints上的權重設置爲0可以幫助增加需要的組件的可用空間量舒展的空間。

+0

這樣做,謝謝。滾動窗格上的首選大小的集合。 – 2011-03-30 17:04:58

相關問題