Swing:使用jeditorpane,在窗口中键入,导致我的显示向下移动

时间:2011-03-30 14:16:08

标签: java swing scala jeditorpane gridbaglayout

这是我的问题,当我开始输入并到达我的JEditorPane的垂直边缘时,jeditor窗格的高度随着我的输入一直延伸到窗口的末尾。我在jeditor下面的textareas最终消失了。基本上,一切都向下移动,我希望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 //

1 个答案:

答案 0 :(得分:2)

设置编辑器的首选大小一旦包含在JScrollPane内,就不会有任何效果。您需要设置滚动窗格本身的首选大小(可能通过获取包装组件的首选大小)。

如果您使用的是GridBagLayout,您可能还会发现将GridBagContraints上的权重设置为0对具有固定大小的元素(例如标签)可以帮助增加可用于需要伸展空间的部件的空间。