【问题标题】:JTextArea scroll functionalityJTextArea 滚动功能
【发布时间】:2016-05-05 01:48:43
【问题描述】:

我目前有一个 Java 应用程序,它使用 JTextArea 的实例向用户呈现数据。他们按下一个按钮,从数据库中存储的数据就会被填充。

没有垂直滚动条,并且数据库中的表格包含的行数超出了屏幕的显示范围。

如何在此文本区域添加垂直滚动条?

代码的许多部分依赖于写入JTextArea,重构代码以适应打印到另一种类型的容器将花费大量时间。

有没有办法将JTextArea 包装成ScrollPane?

当前代码(显示没有滚动条的 textArea):

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1057, 484);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JTextArea mainTextArea = new JTextArea();

        mainTextArea.setLineWrap(true);
        mainTextArea.setWrapStyleWord(true);
        mainTextArea.setBounds(21, 93, 995, 336);
        frame.getContentPane().add(mainTextArea);
    // (continued...)

我尝试将代码包装在滚动窗格中(既不文本区域也不出现滚动条):

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1057, 484);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JTextArea mainTextArea = new JTextArea();

        mainTextArea.setLineWrap(true);
        mainTextArea.setWrapStyleWord(true);
        mainTextArea.setBounds(21, 93, 995, 336);
        JScrollPane scroll = new JScrollPane (MainTextArea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        frame.getContentPane().add(scroll);
        frame.getContentPane().setVisible(true);
    // (continued...)

【问题讨论】:

  • “我怎样才能在这个文本区域中添加一个垂直滚动条?” 在JScrollPane 中显示它。但请注意,在使用 null 布局时将不起作用。 Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible example 或 Short, Self Contained, Correct Example。 2) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度。 3) 请学习常见的 Java 命名法(命名约定 - 例如EachWordUpperCaseClass、firstWordLowerCaseMethod()、firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并始终如一地使用它。
  • 阅读 How to Use Text Areas 上的 Swing 教程中的部分以获取工作示例。下载代码并将其用作课程的起点。代码将结构合理并遵循 Swing 约定。令人惊讶的是,Andrew 能用你的 10 行代码找到这么多问题。这就是为什么您需要从教程中的演示代码开始学习如何正确编码 Swing。并保留所有 Swing 基础知识的教程链接。
  • frame.getContentPane().setLayout(null); 是你的第一个问题

标签: java swing jscrollpane layout-manager null-layout-manager


【解决方案1】:

你犯的几个错误

frame.getContentPane().setLayout(null);

  • 此语句删除与关联的默认 BorderLayout JFrame 现在它没有任何布局。

  • 因为 JFrame 的布局变成了 null 你需要使用 setBounds() 方法在添加到 JFrame 之前为 JScrollpane 提供,否则它将不可见。

  • 如果您设置类似scroll.setBounds(21, 93, 995, 336); 的内容,您将能够看到添加了 JFrame 的 JScrollPane

注意: frame.getContentPane().setVisible(true); 不会使您的 JFrame 可见,您需要将其更改为 frame.setVisible(true)。

始终尝试在 swing 中使用 Layouts 而不是设置 null 布局。如果使用 null 布局,则需要手动指定 setBounds,这不是在 swing 中设计 UI 的好方法。

不使用任何布局的完整工作代码:

package com.jd.swing;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JFrameExample {`enter code here`
private JFrame frame;

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1057, 484);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    JTextArea MainTextArea = new JTextArea();
    MainTextArea.setLineWrap(true);
    MainTextArea.setWrapStyleWord(true);
    MainTextArea.setBounds(21, 93, 995, 336);
    JScrollPane scroll = new JScrollPane(MainTextArea);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setBounds(21, 93, 995, 336);
    frame.getContentPane().add(scroll);
    frame.setVisible(true);
}

public static void main(String[] args) {
    new JFrameExample().initialize();
}
}

Output Screen

【讨论】:

    【解决方案2】:

    仅供参考,

    如果您的文本区域中有多行,滚动条默认滚动到文本区域的末尾。要使文本区域中的行保持换行并将滚动条保持在文本区域的顶部,以下代码会有所帮助

    MainTextArea.setWrapStyleWord(true);
    MainTextArea.setLineWrap(true);
    DefaultCaret caret = (DefaultCaret) MainTextArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多