- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是我原来问题的具体答案,但如果您想总体上实现类似的结构,那么遵循下面 camickr 的方法可能会更实用。要按照我最初想要的方式做到这一点,您必须重写 JPanel 并为外部容器实现 Scrollable。(归功于用户 Kylar - JTextArea on JPanel inside JScrollPane does not resize properly )
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
public class MyEditor extends JTextPane {
JFrame window;
JScrollPane editScroll;
NumberLines numPane;
HoldMe please;
public static void main(String args[]) {
new MyEditor();
}
MyEditor() {
setOpaque(false);
setEditorKit(new TextWrapKit());
numPane = new NumberLines();
numPane.setPreferredSize(new Dimension(50,100));
please = new HoldMe();
please.setLayout(new BorderLayout());
please.add(this,BorderLayout.CENTER);
please.add(numPane,BorderLayout.WEST);
editScroll = new JScrollPane(please);
editScroll.setPreferredSize(new Dimension(500,500));
editScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
editScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(editScroll);
window.pack();
window.setVisible(true);
}
private class HoldMe extends JPanel implements Scrollable{
public Dimension getPreferredScrollableViewportSize() {
return super.getPreferredSize(); //tell the JScrollPane that we want to be our 'preferredSize' - but later, we'll say that vertically, it should scroll.
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 16;//set to 16 because that's what you had in your code.
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 16;//set to 16 because that's what you had set in your code.
}
public boolean getScrollableTracksViewportWidth() {
return true;//track the width, and re-size as needed.
}
public boolean getScrollableTracksViewportHeight() {
return false; //we don't want to track the height, because we want to scroll vertically.
}
}
private class NumberLines extends JPanel {
NumberLines() {
setBackground(new Color(120,120,120));
setOpaque(false);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
g.fillRect(0,0,this.getWidth(),this.getHeight());
}
}
private class TextWrapKit extends StyledEditorKit {
ViewFactory defaultFactory=new TextWrapFactory();
public ViewFactory getViewFactory() {
return defaultFactory;
}
}
private class TextWrapFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new TextWrapView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
private class TextWrapView extends LabelView {
public TextWrapView(Element elem) {
super(elem);
}
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
}
}
编辑:代码替换为 SCCE,抱歉给您带来麻烦
好吧,它的结构是这样的......
• 顶级 - 扩展 JTextPane
• 将此 JTextPane 放入设置为 BorderLayout CENTER 的 JPanel
• 将另一个 JPanel 放入设置为 WEST 的同一个 JPanel
• 将容纳两者的 JPanel 放入 JScrollPane
• 准备部署(不)
基本上,我无法找到一种方法来做到这一点而不丢失一些关键的东西。我可以把它们全部放在一起,但我会失去滚动。或者我会向后滚动,但随后会丢失文本换行。有一次我换行,从技术上讲它滚动了,但没有达到应有的效果,换句话说,滚动 Pane 没有检测到文本的大小。我需要嵌套的 JPanel(第一个)与 JTextPane 一起滚动
问题区域在下面的构造函数中,抱歉,它太乱了,但我在这里尝试不同的东西时失去了它......可能没有帮助。如果有人能帮我解决这个问题,我将非常感激。
感谢您的阅读。
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import java.awt.BorderLayout;
public class MyEditor extends JTextPane {
JFrame window;
JScrollPane editScroll;
public static void main(String args[]) {
new MyEditor();
}
MyEditor() {
setOpaque(false);
setEditorKit(new TextWrapKit());
JPanel numPane = new JPanel();
JPanel packEdit = new JPanel();
packEdit.setLayout(new BorderLayout());
packEdit.add(this,BorderLayout.CENTER);
packEdit.add(numPane,BorderLayout.WEST);
editScroll = new JScrollPane(packEdit);
editScroll.setPreferredSize(new Dimension(500,500));
editScroll.setViewportView(packEdit);
editScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
editScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(editScroll);
window.pack();
window.setVisible(true);
}
private class TextWrapKit extends StyledEditorKit {
ViewFactory defaultFactory=new TextWrapFactory();
public ViewFactory getViewFactory() {
return defaultFactory;
}
}
private class TextWrapFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new TextWrapView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
private class TextWrapView extends LabelView {
public TextWrapView(Element elem) {
super(elem);
}
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
}
}
最佳答案
extends JTextPane
为什么?
为什么?setEditorKit(new TextWrapKit());
使滚动 Pane 与默认组件一起使用。然后,如果由于某种原因您仍然需要自定义,请一次进行一项更改以确保它仍然有效。如果它不再起作用,那么您就知道问题出在哪里。
packEdit.add(numPane,BorderLayout.WEST);
在我看来,您正在尝试添加行号。执行此操作的标准方法是将组件添加到滚动 Pane 的行标题,而不是使其成为添加到视口(viewport)的面板的一部分。请参阅Text Component Line Number有关此方法的示例。
将来,每个问题都应该发布一个 SSCCE
。您无需 SSCCE
即可获得一个免费(尝试)答案。
关于java - 如何正确地将 JTextPane/JPanel 打包到 JPanel 中到 JScrollPane 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182978/
我有一个 JScrollPane,它的内容 Pane 有一个 JPanel。我向该 JPanel 添加了较小的 JPanel,正如预期的那样,如果我添加太多 JPanel,将会出现一个垂直滚动条。 问
我正在尝试在另一个 JScrollPane 中添加一个 JScrollPane。内部 JScrollPane 只会水平滚动,外部只会垂直滚动。 在这张图中,您可以看到添加 mainPanel 时水平滚
我正在尝试创建一个带有滚动条的容器,并且容器内部有两个内部面板。顶部内面板内还有另一个 JScrollPane。 但目前我遇到的问题是,当我的顶部内面板太长(宽度)时,顶部内面板内的滚动条被禁用,我只
你们能让我知道禁用水平滚动条的最佳方法是什么吗? 我有 宽度:100% 和 高度:280px 的 div。当我们有很长的连续文本(没有任何空格)时,我们会显示一个水平滚动条。 顺便说一句,我正在使用
我注意到这个问题主要出现在 OS 10.5.8 上的 Firefox 3.6.6 上,Safari 上偶尔也会出现这种情况(准备好惊讶的表情 - IE 实际上每次都工作正常 - 什么?!)。 我的网址
所以我有一堆JTable。每个JTable 都位于JScrollPane 内。然后,我将每个 JScrollPane 添加到 JPanel 中。然后,我将此 JPanel 添加到 JScrollPan
我在 JScrollPane 上放置了多个 JPanel。现在我有了它,所以如果你的鼠标在框架之外,那么它就不会拖动 JPanels。 当我在一个方向上移动组件时,我需要让它滚动。 (例如,如果我
有什么区别 JScrollPane.getViewportBorderBounds() and JScrollPane.getViewport() and JscrollPane.getVisible
此应用程序适用于触摸屏。我只需要仅当用户触摸 JScrollPane 区域时 JScrollPane 的滚动条可见。 我是 GUI 和 swing 的新手。这会很有帮助,我不明白是什么,或者如果在其他
出于布局目的,我需要在滚动内容底部和容器底部之间放置 15px 的空间:div class="scroll-pane" . 造型容器 .scroll-pane { padding-bottom:15p
我需要一个可以显示很多图像的程序,并且我需要一个可以滚动的窗口。我阅读了文档并在论坛上进行了搜索,但仍然没有成功。我尝试使用 JScrollPane 和 JFrame,如下所示。 JScrollPan
我今天遇到了这个新事物,但我不知道为什么。例如,当我想在面板中显示某些内容时,我只需将其添加到面板即可;但为什么我不能直接添加表格到滚动 Pane ,为什么我必须调用 setviewportview(
我今天遇到了这个新事物,我不知道为什么。例如,当我想在面板中显示某些内容时,我只需将其添加到面板即可;但是为什么我不能直接添加表格到滚动 Pane ,为什么我必须调用 setviewportview(
我一直在尝试缩小 JScrollPane 的内容宽度,例如。我已将 HorizontalScrollBarPolicy 设置为 NEVER,但这最终导致没有 ScrollBar 出现并且内容不再显
所以,在下面的代码中,我在左侧有一个 JTextArea。右上角的 JScrollPane 看起来不错。使用相同的代码,我还在右下侧添加了一个 JScrollPane,但是尽管代码相同,但保存了首选大
我在 div 上使用 JScrollPane 来滚动它。 div 包含一个自写的 javascript 代码,它从我的 tumblr 提要中获取最后几篇文章。但是,即使滚动 Pane 显示正常,但由于
请看下面的代码块 import java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; import javax.s
我在 Swing 中制作了一个简单的 GUI,其中在 JScrollPane 内、JSPlitPane 内、JPanel 内、....在 JFrame 内有一个大 JPanel(显示大 Buffere
我有一个带滚动条的 JPanel,我想向它添加很多 JLabel。但是滚动条不起作用。我无法使用滚动条,即使面板已满,它也不会滚动。这是我的代码: import java.awt.*; import
我正在尝试在 JScrollPane 中添加 2 个图像。第一个图像是背景,第二个图像与第一个图像重叠。当我运行我的程序时,问题只显示第二张图像! 请帮忙 ImageIcon ii = new Ima
我是一名优秀的程序员,十分优秀!