Swing:使用 jeditor 窗格,在窗口中键入,导致我的显示向下移动
这是我的问题,当我开始打字并到达 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 //
Here is my problem, when I start typing and reach the vertical edge of my JEditorPane, the jeditor pane extends in height along with my typing until the end of the window. My textareas below the jeditor eventually disappear. Basically, everything shifts down and I want the jeditor pane to stay at a static height.
Why would the jeditor pane move? How do I prevent it?
I have seen this before but don't remember how to fix it. I think it is related to how I use the Layout managers. I could use a different layout but I am more familiar with GridBag. I tried preferred height and it works initially but like I said...after typing the height changes. Strange.
Here is the code (it is scala, but think of it as pseudo code).
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 //
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦将编辑器封装在
JScrollPane
中,设置编辑器的首选大小将不会产生任何效果。您需要设置滚动窗格本身的首选大小(可能通过获取包装组件的首选大小)。如果您使用的是
GridBagLayout
,您可能还会发现,将具有固定大小的元素(例如标签)的GridBagContraints
上的权重设置为 0 有助于增加可用于需要伸展空间的组件的空间量。Setting the preferred size of the editors won't have any effect once it's wrapped inside a
JScrollPane
. You need to set the preferred size of the scroll pane itself (perhaps by getting the wrapped component's preferred size).If you're using a
GridBagLayout
, you might also find that setting the weights on theGridBagContraints
to 0 on elements that have a fixed size (such as labels) can help increase the amount of space available for the components that need room to stretch.