1

これが私の問題です。入力を開始してJEditorPaneの垂直端に到達すると、入力とともにjeditorペインがウィンドウの終わりまで高さで伸びます。jeditor の下の私のテキストエリアは最終的に消えます。基本的に、すべてが下に移動し、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 //
4

1 に答える 1

2

エディターの優先サイズを設定しても、JScrollPane. スクロール ペイン自体の優先サイズを設定する必要があります (おそらく、ラップされたコンポーネントの優先サイズを取得することによって)。

を使用している場合、サイズが固定されている要素 (ラベルなど)GridBagLayoutの重みGridBagContraintsを 0 に設定すると、拡大する余地が必要なコンポーネントに使用できるスペースを増やすのに役立つ場合があります。

于 2011-03-30T15:52:14.497 に答える